我正在进行Facebook集成,首先我需要进行身份验证,然后我必须获取详细信息。在这个过程中,我正在展示HUD加载器。经过身份验证后,Hud会消失并且不会显示清楚。我的代码如下,如果我做错了,请建议我。
-(IBAction)btn_FacebookClicked:(id)sender
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText =@"Loading";
HUD.dimBackground=YES;
[HUD show:YES];
[self performSelectorInBackground:@selector(FacebookLogin) withObject:nil];
}
-(void)FacebookLogin
{
if(!accountStore)
accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:@{ACFacebookAppIdKey: @"**********", ACFacebookPermissionsKey: @[@"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
facebookAccount = [accounts lastObject];
NSLog(@"Success");
[self userfbDetail];
}else{
// ouch
[HUD hide:YES];
NSLog(@"Fail");
NSLog(@"Error: %@", error);
}
}];
}
}
// Get user facebook details
- (void)userfbDetail{
NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableArray *arr_facebook=[[NSMutableArray alloc]init];
arr_facebook=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSString *firstname=[arr_facebook valueForKey:@"name"];
NSString *user=[NSString stringWithFormat:@"Welcome %@",firstname];
ALERT(user);
[HUD hide:YES];
}];
}