您的位置:首页 > 其它

Facebook 登录自定义按钮

2016-11-02 14:16 666 查看
<pre name="code" class="objc">  [[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 {
<span style="white-space:pre">	</span> NSInteger slot = 0; FBSDKProfile *profile = notification.userInfo[FBSDKProfileChangeNewKey];
<span style="white-space:pre">	</span> if (profile) { SUCacheItem *cacheItem = [SUCache itemForSlot:slot];
<span style="white-space:pre">	</span>cacheItem.profile = profile;
<span style="white-space:pre">	</span> [SUCache saveItem:cacheItem slot:slot];
}}
// Observe a new token, so save it to our SUCache and update
- (void)_accessTokenChanged:(NSNotification *)notification{
<span style="white-space:pre">	</span>FBSDKAccessToken *token = notification.userInfo[FBSDKAccessTokenChangeNewKey];
<span style="white-space:pre">	</span>if (!token) { [FBSDKAccessToken setCurrentAccessToken:nil];
<span style="white-space:pre">	</span>[FBSDKProfile setCurrentProfile:nil];
<span style="white-space:pre">	</span>}else {
<span style="white-space:pre">		</span>NSInteger slot = 0;
<span style="white-space:pre">	</span>        SUCacheItem *item = [SUCache itemForSlot:slot] ?: [[SUCacheItem alloc] init];
<span style="white-space:pre">		</span>if (![item.token isEqualToAccessToken:token]) {
<span style="white-space:pre">	</span>       <span style="white-space:pre">		</span> item.token = token; [SUCache saveItem:item slot:slot];
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}
- (IBAction)facebookLoginBtnClick:(id)sender {
<span style="white-space:pre">	</span>NSInteger slot = 0; FBSDKAccessToken *token = [SUCache itemForSlot:slot].token;
<span style="white-space:pre">	</span> if (token) {
<span style="white-space:pre">		</span>[self fbAutoLoginWithToken:token];
<span style="white-space:pre">	</span>}else {
<span style="white-space:pre">		</span>[self fbNewLogin];
<span style="white-space:pre">		</span>}
}
- (void)fbAutoLoginWithToken:(FBSDKAccessToken *)token{
// We have a saved token, issue a request to make sure it's still valid.
<span style="white-space:pre">	</span>[FBSDKAccessToken setCurrentAccessToken:token];
<span style="white-space:pre">	</span>FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil];
<span style="white-space:pre">	</span>[request setGraphErrorRecoveryDisabled:YES];
<span style="white-space:pre">	</span>[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.
<span style="white-space:pre">	</span>if (error) {
<span style="white-space:pre">		</span>[[[UIAlertView alloc] initWithTitle:nil message:@"The user token is no longer valid."
<span style="white-space:pre">							</span>delegate:nil
<span style="white-space:pre">							</span>cancelButtonTitle:@"OK"
<span style="white-space:pre">							</span>otherButtonTitles:nil] show];
<span style="white-space:pre">	</span>[SUCache deleteItemInSlot:0];;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}];
}
- (void)fbNewLogin{
<span style="white-space:pre">	</span>FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
<span style="white-space:pre">	</span>[login logInWithReadPermissions: @[@"public_profile",@"email"]
<span style="white-space:pre">			</span>fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
<span style="white-space:pre">				</span>if (error) { DLog(@"Process error");
<span style="white-space:pre">	</span> }else if (result.isCancelled) {
<span style="white-space:pre">		</span>DLog(@"Cancelled");
<span style="white-space:pre">	</span>}else {
<span style="white-space:pre">		</span> DLog(@"Logged in");
<span style="white-space:pre">		</span> FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:result.token.userID
<span style="white-space:pre">										</span>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"]);
}];
}
}];
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: