I created a quick sample app that is just a page with a button in it that when clicked launches SFSafariViewController with a URL pointing at my localhost page that I created. The localhost page just has a single link on it pointing at mytestapp://hello. I registered the mytestapp url scheme in my app settings in Xcode by adding it to the "URL Types" section. The plan was to make sure the URL scheme is working before implementing it into my main app that I am building, but nothing is happening when I click the link in the localhost page. The SFSafariViewController loads perfectly, the localhost page loads properly, I click the link and nothing happens.
I have added a simple print statement in the application(:url:options) method in app delegate, but that never gets run.
Here is the ViewController code...
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")
}
}
and the app delegate method here...
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
}
I can't figure out why this isn't working, seems like I did everything I was supposed to, but the print lines in app delegate never get called.
Not sure if you need it or not, but just in case, my localhost page is literally just...
<?php
echo '<a href="mytestapp://hello">Here goes nothing</a>';
Is there something that might not be working in an emulator in xcode and it has to be on an actual device for URL schemes to work? Did I miss a step somewhere? somethings else I have no idea? Any help would be really appreciated. Thank you