Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ADAL iOS
+ (void)getToken:(void (^)(NSString*))completionBlock;
{
ADAuthenticationError *error = nil;
authContext = [ADAuthenticationContext authenticationContextWithAuthority:@"https://login.microsoftonline.com/common"
error:&error];
[authContext acquireTokenWithResource:@"https://graph.windows.net"
clientId:@"<Your Client ID>" // Comes from App Portal
redirectUri:[NSURL URLWithString:@"<Your Redirect URI>"] // Comes from App Portal
completionBlock:^(ADAuthenticationResult *result)
{
if (AD_SUCCEEDED != result.status){
// display error on the screen
[self showError:result.error.errorDetails];
}
else{
completionBlock(result.accessToken);
}
}];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourAppURL];
NSString *authHeader = [NSString stringWithFormat:@"Bearer %@", accessToken];
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// Process Response Here
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment