我创建了一个快速示例应用程序,它只是一个带有按钮的页面,当单击该按钮时,会启动SFSafariViewController,其中一个URL指向我创建的localhost页面。localhost页面上只有一个链接指向mytestapp://你好。我在Xcode的app设置中注册了mytestappurl方案,方法是将其添加到“url类型”部分。我的计划是在将URL方案实现到我正在构建的主应用程序之前,确保它正常工作,但是当我单击localhost页面中的链接时,什么也没有发生。SFSafariViewController加载得很好,localhost页面加载得很好,我点击了链接,什么也没发生。
我在应用程序中添加了一个简单的print语句(:url:选项)方法,但它永远不会运行。
以下是ViewController代码。。。
import UIKit
import SafariServices
class ViewController: UIViewController, SFSafariViewControllerDelegate {
@IBOutlet weak var launchButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func launchTap(_ sender: UIButton) {
guard let url = URL(string: "http://localhost.dcom/test/proof.php") else {
print("Unable to create the URL")
return
}
let authorizationController = SFSafariViewController(url: url)
authorizationController.delegate = self
present(authorizationController, animated: true, completion: nil)
}
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
print("safari completed")
}
}
这里的app delegate方法。。。
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print("app received url: \(url)")
guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true), let message = components.path else {
print("Invalid URL")
return false
}
print("The message received was \(message)")
return true
}
我不明白为什么这不起作用,似乎我做了我应该做的一切,但在应用程序代理打印行从来没有被调用。
不知道你是否需要它,但以防万一,我的localhost页面实际上只是。。。
<?php
echo '<a href="mytestapp://hello">Here goes nothing</a>';