[英]how to login with Facebook with preinstalled facebook app in iOS 9
(IBAction)loginWithfacebookClicked:(id)sender
{
// When Clicked on Facebook Button
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else {
// Open a session showing the user the login UI
[FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
}
使用此邏輯在Facebook按鈕內單擊
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
//***Start : requesting for facebook login with valid permissions***
[login logInWithReadPermissions: @[@"public_profile"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
}
else if(result.isCancelled){
NSLog(@"Cancelled");
}
else {
//Successfully logged in
NSLog(@"Logged in");
//dictionary to represent the data to be fetched from facebook
NSDictionary *params = @{ @"fields":@"name,gender,id,picture"};
//Setting up the request parameters
FBSDKGraphRequest *requestForUserProfileDetails = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:params HTTPMethod:@"GET"];
//***Start : requesting for user profile details***
[requestForUserProfileDetails startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection ,id result, NSError* error){
if(!error)
{
//set login type as facebook
//result contains the json response sent by facebook
_socialLoginType = @"facebook";
_name = [result valueForKey:@"name"];
_gender = [result valueForKey:@"gender"];
_socialLoginKey = [result valueForKey:@"id"];
_profileImage = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"]]]];
}
}];
//***End : requesting for user profile details***
}
}];
//***End : requesting for facebook login with valid permissions***
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.