Reputation: 635
I'm trying to implement an OAuth flow on iOS - Swift 3, in order to query a REST API (Strava).
I do this in my VC handling the auth flow:
@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)
}
In my 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
}
And I added my url scheme in the plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>org.my.bundle.id</string>
<key>CFBundleURLSchemes</key>
<array>
<string>localhost</string>
</array>
</dict>
</array>
However, I can't get the application(_:open:options:)
function in my AppDelegate to be called.
(The browser is popped up allright, I can login to Strava, and I see that the valid URL is returned including the access token in the "code=..." piece, that I would like to extract, but I can't move on to that part.)
I've tried:
application(_:didFinishLaunchingWithOptions:)
function is
returning true and I'm not implementing
application(_:willFinishLaunchingWithOptions:)
as described in the discussion part of the function's docs.Any ideas what I could be missing?
Cheers
Upvotes: 2
Views: 1503
Reputation: 635
So it was actually a mixture of things that I did not notice...
http://
but yourApp://
since you want yourApp to handle the
callback. On some forum I read that Strava only allows http redirect
uris, which made me try it that way, but that's actually wrong, as
the documentation states:URL to which the user will be redirected with the authorization code, must be to the callback domain associated with the application, or its sub-domain,
localhost
and127.0.0.1
are white-listed.
And we got to the next thing, namely:
(My mistake was, that I did not provide a valid/matching callback domain.)
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.yourdomain.yourApp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>yourApp</string>
</array>
</dict>
</array>
And it works like charm: it does not actually matter if it's iOS 9 or 11, or you use SFSafariViewController
VS you leave the app with UIApplication.shared.openURL()
etc...
Good luck ;)
Upvotes: 4