Skip to content

Instantly share code, notes, and snippets.

@veritech
Created September 19, 2012 20:37
Show Gist options
  • Save veritech/3752113 to your computer and use it in GitHub Desktop.
Save veritech/3752113 to your computer and use it in GitHub Desktop.
How to use iOS 6's social framework
/*
* 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
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment