ezmegy
ezmegy

Reputation: 635

open url function not called in AppDelegate when trying to grab token from oauth url

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:

Any ideas what I could be missing?

Cheers

Upvotes: 2

Views: 1503

Answers (1)

ezmegy
ezmegy

Reputation: 635

So it was actually a mixture of things that I did not notice...

  1. The URL I was trying to use there was wrong: It should not be 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 and 127.0.0.1 are white-listed.

And we got to the next thing, namely:

  1. You should check what you name your application on the Strava admin page at Settings/My Api Application. Following the yourApp example, it should be something like this: Strava My Api Application setup

(My mistake was, that I did not provide a valid/matching callback domain.)

  1. Finally, your plist file should be also set accordingly:
        <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

Related Questions