Drew O'Meara Drew O'Meara - 4 months ago 31
iOS Question

Bypass SoundCloud API's SCLoginViewController and instead use openURL

Fellow SoundCloud API/SDK devs,

It seems that the SoundCloud iOS SDK is a little long in the tooth, namely SCLoginViewController. It's clearly in need of updating and has cosmetic issues when it appears.

Instead, I'm looking at simply using openURL on the URL that the SC prepares:

[SCSoundCloud requestAccessWithPreparedAuthorizationURLHandler:^(NSURL* preparedURL) {
[[UIApplication sharedApplication] openURL:preparedURL];
}];


and then catching the result my iOS app's handleURL call (as the URL comes back as

myapp://oauth?code=0a83...


So my question to the SC API dev is: how I get from having that
NSURL
in hand to
[SCSoundCloud account]
returning the appropriate account.

Thanks in advance!!

Answer

Ok, so after a deep dive into the bowels of OAuth2Client (https://github.com/nxtbgthng/OAuth2Client), I have the answer...

The first step of a typical oauth flow is a GET with a param that specifies a redirect URL. Once the user authenticates from this initial GET, the host redirects to the specified URL with an additional URL param, code. On iOS, this typically takes the form of myapp://oauth&code=ZZZZ.

At this point, the app extracts the code and and uses it as a param for a subsequent POST request that then returns the oauth access token (access_token). Using OAuth2Client's flow, when your app gets the URL myapp://oauth&code=ZZZZ via UIApplicationDelegate's application:openURL:sourceApplication:annotation:(id), you need subsequently call:

[[NXOAuth2AccountStore sharedStore] handleRedirectURL:inURL];

This will get OAuth2Client to pull off the code param, form the new POST URL and arguments, internally perform the POST, process the response (digging out access_token etc), and then send the notification NXOAuth2AccountDidChangeAccessTokenNotification.

In the SoundCloud iOS SDK, their SCLoginViewController captures that myapp://oauth&code=ZZZZ step, so that's why you never see that step.