我正在使用 Swift 3 和 Xcode 8 (iOS 10.1)
,我对整个世界都是全新的。 :)
我正在针对外部应用程序进行身份验证以获取访问 token 。用例是:
1) 从我的应用加载外部应用
2) 验证用户并获取访问 token
3) 从外部应用重定向回我的应用
我能做到 1) 和 2),但不能做到 3)。外部应用程序已指定您配置重定向参数,我已经完成了。但是,唉,没有雪茄。
我已经在 Info.plist
中为这两个应用设置了 URL 方案。我在控制台或调试器中没有收到任何错误。
过去 4 天我一直在谷歌搜索和研究,这是我到目前为止提出的解决方案。现在,我看到控制台消息,但外部应用程序不再打开。
AppDelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if (url.scheme == "extapp") {
print("working with extapp scheme")
} else if (url.scheme == "myapp") {
print("redirecting back to app after working with extapp")
}
return true
}
View Controller
@IBAction func loginButton(_ sender: Any) {
let id = text.text
let currentCharacterCount = id.text?.characters.count ?? 0
if currentCharacterCount == 0 {
self.showAlert(text: "Please provide a id.")
} else if currentCharacterCount < 10 {
self.showAlert(text: "Oh! id doesn't seem to be in the correct format. Try again.")
} else {
if (self.firebaseData.callAuth(loggedIn: id!)) {
OpenExtApp()
}
}
}
func OpenExtApp() {
let Url = URL(string: "extapp:///?autostart=\(token)&redirect=myapp://key")
if (UIApplication.shared.canOpenURL(Url!)) {
UIApplication.shared.open(Url!, options: [:])
}
}
非常感谢您以任何方式提供帮助。
最佳答案
问题似乎出在我用来对 redirect
参数进行编码的 addingPercentEncoding
方法上。我没有注意到参数没有被正确编码。事实证明,AllowedCharacters
中的默认过滤器不会对/和 : 进行编码。这是 Apple 的一个已知问题。这解决了编码问题,进而解决了我在打开 extapp 时遇到的问题:
let characterSetTobeAllowed = (CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[] ").inverted)
let redirect = parameter.addingPercentEncoding(withAllowedCharacters: characterSetTobeAllowed)
关于ios - UIApplication.sharedApplication.open() 不加载 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40655055/