Facebook 登录自定义按钮

<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"]); 
}]; 
} 
}];
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值