Authentication

Prerequisites

An application will need to prompt a user to authenticate with Nike+ in order for the Nike+ user to grant the application access to their Nike+ data. The prompt for login is done through the use of the OAuth 2.0 authorization framework and will launch Safari from the application. It is recommended to review the following:

Login Notification Handling

Setup notification handling to follow the login flow. Notfications can arrive on any thread, so make sure GUI related tasks are dispatched to the main thread.
// Register for updates and error notifications
- (void)viewDidAppear:(BOOL)animated {
    // Register for login events. NIKELoginStateChangedNotification broadcasts a notification when
    // a user logs in or out.
    [[NSNotificationCenter defaultCenter] addObserverForName:NIKELoginStateChangedNotification
                                                      object:[Nike class]
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification *note){
                                                      
         if ([note.userInfo[NIKELoginStateKey] isEqualToString:NIKEDidLogin]) {
                     // Handle login
         }
         if ([note.userInfo[NIKELoginStateKey] isEqualToString:NIKEDidLogout]) {
                     //Handle logout
         }
     }];
    
    // Make sure and do some error handling
    [[NSNotificationCenter defaultCenter] addObserverForName:NIKELoginErrorNotification
                                                      object:self
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification *note) {
                                                      
        NSLog(@"There was an error during login: %@", note.userInfo[NSUnderlyingErrorKey]);
    };
}

// Remove observers when object is no longer needed
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NIKELoginStateChangedNotification object:[Nike class]];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NIKELoginErrorNotification object:[Nike class]];
}


Handling URL Callback

After the Safari login has completed, the redirect URL will open the application. This call is handled in the UIApplication delegate.
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    // Check to see if the SDK handles the URL, if so, send the URL to the SDK.
    BOOL isNikeURL = [Nike canHandleURL:url sourceApplication:sourceApplication annotation:annotation];
    if (isNikeURL) {
        [Nike handleOpenURL:url];
    }
    return isNikeURL;
}

Login and Logout

Login and logout are seperate methods that are available in the Nike+ iOS SDK. There is additional documentation on these methods that only approved partners have access to.
// Login
// This will result in an inter app launch to Safari, then a redirect back to the application delegate.
[Nike login];

// Logout, this will remove the credentials for the user from the Keychain
[Nike logout];