【问题标题】:open url function not called in AppDelegate when trying to grab token from oauth url尝试从 oauth url 获取令牌时,未在 AppDelegate 中调用打开 url 函数
【发布时间】:2017-11-06 16:08:16
【问题描述】:

我正在尝试在 iOS - Swift 3 上实现 OAuth 流程,以查询 REST API (Strava)。

我在处理身份验证流程的 VC 中执行此操作:

@IBAction func tappedStartAuth(_ sender: Any) {

    let authUrlStr = "http://www.strava.com/oauth/authorize?client_id=12345&response_type=code&redirect_uri=http://localhost/exchange_token&approval_prompt=force&scope=view_private,write"

    // but instead 12345 I have my real cientID of course

    UIApplication.shared.openURL(URL(string:authUrlStr)!)

    // Did not work with SFVC either:
    //safariViewController = SFSafariViewController(url: URL(string: authUrlStr)!)
    //safariViewController?.delegate = self
    //present(safariViewController!, animated: true, completion: nil)

}

在我的 AppDelegate 中:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    print("opened url, [code] checking should follow, but this won't get called")

    // would do some stuff here...

    return true
}

我在 plist 中添加了我的 url 方案:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>org.my.bundle.id</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>localhost</string>
        </array>
    </dict>
</array>

但是,我无法调用 AppDelegate 中的 application(_:open:options:) 函数。

(浏览器弹出好了,我可以登录 Strava,我看到返回的有效 URL 包括“code=...”部分中的访问令牌,我想提取它,但我无法继续进行该部分。)

我试过了:

  • 使用内置 Safari 浏览器(离开应用程序)
  • 使用 SFSafariViewController
  • 在 iOS 9 而不是 11 上(我知道苹果在 11 上引入了 SFAuthenticationSession,as described here。我没看过 还没有,但我也需要我的应用程序在 11 之前)。
  • 我的application(_:didFinishLaunchingWithOptions:) 函数是 返回真,我没有实施 application(_:willFinishLaunchingWithOptions:)function's docs 的讨论部分所述。

有什么我可能会遗漏的想法吗?

干杯

【问题讨论】:

    标签: ios swift oauth-2.0 strava


    【解决方案1】:

    所以它实际上是我没有注意到的东西的混合......

    1. 我尝试使用的 URL 错误:它不应该是 http://yourApp:// 因为您希望您的应用程序处理 打回来。在某个论坛上,我读到 Strava 只允许 http 重定向 uris,这让我尝试那样做,但这实际上是错误的,因为 the documentation 状态:

    用户将使用授权代码重定向到的 URL,必须是与应用程序关联的回调域,或其子域 localhost127.0.0.1 已列入白名单。

    然后我们开始了下一件事,即:

    1. 您应该在 Strava 管理页面的设置/我的 Api 应用程序中检查您为应用程序命名的内容。 在 yourApp 示例之后,它应该是这样的:

    (我的错误是,我没有提供有效/匹配的回调域。)

    1. 最后,您的 plist 文件也应相应设置:
            <key>CFBundleURLTypes</key>
            <array>
                <dict>
                    <key>CFBundleURLName</key>
                    <string>com.yourdomain.yourApp</string>
                    <key>CFBundleURLSchemes</key>
                    <array>
                        <string>yourApp</string>
                    </array>
                </dict>
            </array>
    

    它的作用就像魅力:它实际上是 iOS 9 还是 11,或者你使用 SFSafariViewController VS 你离开应用程序时使用 UIApplication.shared.openURL() 等等......

    祝你好运;)

    【讨论】:

    • 这对我来说非常有效。只需添加:当您创建 URL 以访问 API 时:&amp;redirect_uri=yourApp://localhost
    猜你喜欢
    • 2016-12-13
    • 1970-01-01
    • 2020-07-29
    • 2020-07-21
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多