Ios 尝试从oauth url获取令牌时,AppDelegate中未调用open url函数

Ios 尝试从oauth url获取令牌时,AppDelegate中未调用open url函数,ios,swift,oauth-2.0,strava,Ios,Swift,Oauth 2.0,Strava,我试图在iOS-Swift 3上实现OAuth流,以便查询RESTAPI 我在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&am

我试图在iOS-Swift 3上实现OAuth流,以便查询RESTAPI

我在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中的应用程序:open:options:函数

浏览器弹出了,我可以登录到Strava,我看到返回了有效的URL,包括代码中的访问令牌=。。。我想提取的片段,但我不能继续那个部分

我试过:

内置Safari浏览器离开应用程序 使用SFSafariViewController 在iOS 9而不是iOS 11上,我知道苹果在iOS 11上引入了 SFAuthenticationSession。我没有看 但我也需要在11岁之前使用我的应用程序。 我的应用程序已完成启动,选项:函数为 返回true,但我没有实现 应用程序:将使用选项完成启动,如的讨论部分所述。 你知道我会错过什么吗


干杯

所以这实际上是我没有注意到的事情的混合体

我试图在那里使用的URL是错误的:它不应该是 http://but yourApp://因为您希望应用程序处理 回拨。在一些论坛上,我读到Strava只允许http重定向 URI,这让我尝试了这种方式,但实际上这是错误的,因为 国家: 用户将使用授权代码重定向到的URL必须是与应用程序相关联的回调域,或者其子域localhost和127.0.0.1被列为白名单

我们接着讨论下一件事,即:

您应该在设置/My Api应用程序的Strava管理页面上检查您的应用程序的名称。 按照yourApp示例,应该是这样的: 我的错误是,我没有提供有效/匹配的回调域

最后,还应相应地设置plist文件: CbundleurlTypes CFBundleURLName com.yourdomain.yourApp 循环流化床锅炉方案 你的应用程序 它的工作原理就像魅力一样:无论是iOS 9还是iOS 11,或者你使用SFSafariViewController,还是你用UIApplication.shared.openURL等离开应用程序,实际上都无关紧要


祝你好运

这对我来说非常有效。只需添加:当您创建URL访问API时:&redirect\u uri=yourApp://localhost