ios - UIApplication.sharedApplication.open() 不加载 url

标签 ios xcode swift3

我正在使用 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/

相关文章:

ios - 自动调整大小 tableview 单元格扩展高度中的嵌套水平 collectionview 在为行重新加载单元格时使 Collection View 滚动重置

html - 使用cordova插件的图像在 Canvas 上看起来很糟糕

ios - ios 应用程序中所有 View Controller 的相同按钮

ios - Xcode 6 与 Swift 超慢打字和自动完成

swift - 在 Swift 中计算积分的结果

swift - 需要左右按钮在 x 轴上移动 Sprite

ios - 在 XCode 4 中构建和运行 App 时出现警告对话框 "The Service is Invalid"

ios - MvvmCross .dialog 中的 "Failed to create target binding for to"

iphone - Xcode 目标阶段 Python 脚本

objective-c - 如何在我的 OSX 应用程序中运行嵌入式二进制文件?