Facebook 登录自定义按钮

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_updateContent:)
                                                 name:FBSDKProfileDidChangeNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_accessTokenChanged:)
                                                 name:FBSDKAccessTokenDidChangeNotification
                                               object:nil];

- (void)_updateContent:(NSNotification *)notification {
	 NSInteger slot = 0; FBSDKProfile *profile = notification.userInfo[FBSDKProfileChangeNewKey];
	 if (profile) { SUCacheItem *cacheItem = [SUCache itemForSlot:slot]; 
	cacheItem.profile = profile;
	 [SUCache saveItem:cacheItem slot:slot]; 
}}
// Observe a new token, so save it to our SUCache and update
- (void)_accessTokenChanged:(NSNotification *)notification{ 
	FBSDKAccessToken *token = notification.userInfo[FBSDKAccessTokenChangeNewKey]; 
	if (!token) { [FBSDKAccessToken setCurrentAccessToken:nil]; 
	[FBSDKProfile setCurrentProfile:nil]; 
	} else { 
		NSInteger slot = 0;
	        SUCacheItem *item = [SUCache itemForSlot:slot] ?: [[SUCacheItem alloc] init]; 
		if (![item.token isEqualToAccessToken:token]) {
	       		 item.token = token; [SUCache saveItem:item slot:slot]; 
		} 
	}
}
- (IBAction)facebookLoginBtnClick:(id)sender { 
	NSInteger slot = 0; FBSDKAccessToken *token = [SUCache itemForSlot:slot].token;
	 if (token) { 
		[self fbAutoLoginWithToken:token]; 
	} else { 
		[self fbNewLogin]; 
		}
}
- (void)fbAutoLoginWithToken:(FBSDKAccessToken *)token{ 
// We have a saved token, issue a request to make sure it's still valid. 
	[FBSDKAccessToken setCurrentAccessToken:token]; 
	FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]; 
	[request setGraphErrorRecoveryDisabled:YES]; 
	[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
// Since we're only requesting /me, we make a simplifying assumption that any error // means the token is bad. 
	if (error) { 
		[[[UIAlertView alloc] initWithTitle:nil message:@"The user token is no longer valid." 
							delegate:nil 
							cancelButtonTitle:@"OK" 
							otherButtonTitles:nil] show]; 
	[SUCache deleteItemInSlot:0];; 
		} 
	}];
}
- (void)fbNewLogin{ 
	FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 
	[login logInWithReadPermissions: @[@"public_profile",@"email"] 
			fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 
				if (error) { DLog(@"Process error");
	 } else if (result.isCancelled) { 
		DLog(@"Cancelled"); 
	} else {
		 DLog(@"Logged in"); 
		 FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:result.token.userID 
										parameters:@{@"fields": @"id,name,email"} 
HTTPMethod:@"GET"]; 
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result,NSError *error) 
{ // Handle the result DLog(@"%@,%@,%@",result[@"id"],result[@"name"],result[@"email"]); 
}]; 
} 
}];
}


 
  

你可能感兴趣的:(ios开发)