【发布时间】: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