|
/* |
|
* This code uses the Social.framework & the AFNetworking library |
|
*/ |
|
|
|
ACAccountStore *store; |
|
ACAccountType *accountType; |
|
|
|
//Create an ACAccountStore instance |
|
store = [[ACAccountStore alloc] init]; |
|
|
|
//Get the FB Account type |
|
accountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; |
|
|
|
//Request access to the account |
|
[store requestAccessToAccountsWithType:accountType |
|
options:@{ |
|
@"YOUR_FB_APP_ID",ACFacebookAppIdKey, |
|
@[@"FB_PERMISSIONS"],ACFacebookPermissionsKey |
|
} |
|
completion:^(BOOL granted, NSError *error){ |
|
//A UI will have been presented to the user |
|
//Giving them the opportuntiy to reject this request |
|
//If we have been approved |
|
if (granted) { |
|
|
|
NSArray *accounts; |
|
ACAccount *account; |
|
SLRequest *request; |
|
AFJSONRequestOperation *operation; |
|
//Get the account |
|
accounts = [[store accountsWithAccountType:accountType]; |
|
|
|
//Accounts is a list of valid fb accounts |
|
//Ideally you should allow the user to chose if there is more than a single account |
|
account = [accounts objectAtIndex:0]; |
|
|
|
//Lets construct a request |
|
request = [SLRequest requestForService:SLServiceTypeFacebook |
|
requestMethod:SLRequestMethodGET, |
|
URL:[NSURL URLWithString:@"http://graph.faceboo.com/me"] |
|
parameters:nil |
|
]; |
|
|
|
//We must set the account we want to use with each request |
|
[request setAccount:account]; |
|
|
|
//At this point we can execute the SLRequest using performRequestWithHandler: method |
|
// or we can extract a NSURLRequest using the preparedURLRequest method and use a 3rd party framework like AFNetworking |
|
|
|
//We use the fantastic AF Networking |
|
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:[request preparedURLRequest]] |
|
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){ |
|
//The JSON variable will contain the currently logged users information! |
|
} |
|
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { |
|
//Hanlde any request errors |
|
}]; |
|
|
|
[[[NSOperationQueue alloc] init] addOperation:operation]; //Ideally we would add this operation to a global operation queue |
|
|
|
} |
|
}]; |