Register your application to launch from a custom URL scheme, and use that with the path success as your callback URL. The callback for the custom URL scheme should send a notification, which will complete the OAuth transaction.
Here's how to create a client and authenticate:
AFOAuth1Client *twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURLURLWithString:@"https://api.twitter.com/1.1/"] key:@"..."secret:@"..."];
// Your application will be sent to the background until the user authenticates, and then the app will be brought back using the callback URL
[self.twitterClient authorizeUsingOAuthWithRequestTokenPath:@"/oauth/request_token"userAuthorizationPath:@"/oauth/authorize"callbackURL:[NSURLURLWithString:@"af-twitter://success"] accessTokenPath:@"/oauth/access_token"accessMethod:@"POST"scope:nilsuccess:^(AFOAuth1Token *accessToken, id responseObject) {
NSMutableURLRequest *request = [self.twitterClient requestWithMethod:@"GET"path:@"statuses/user_timeline.json"parameters:nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperationManager manager] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
[manager.operationQueue addOperation:operation];
} failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];Here's how to respond to the custom URL scheme on iOS:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSNotification *notification = [NSNotificationnotificationWithName:kAFApplicationLaunchedWithURLNotificationobject:niluserInfo:[NSDictionarydictionaryWithObject:url forKey:kAFApplicationLaunchOptionsURLKey]];
[[NSNotificationCenterdefaultCenter] postNotification:notification];
returnYES;
}Joel Chen
AFOAuth1Client is available under the MIT license. See the LICENSE file for more info.