[英]Handle cancel button in facebook iOS using session
I tried to search this but couldn't find anything useful. 我试图进行搜索,但找不到任何有用的东西。
[FBSession setActiveSession:[[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"publish_actions,read_stream,user_hometown,user_birthday,email", nil]]];
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:
^(FBSession *session,FBSessionState state,NSError *error)
{
if(state == FBSessionStateOpen)
{
// use user's detail and post on Facebook
}
}];
Now this works fine for me. 现在这对我来说很好。 But what if user presses close/cancel button before logging into Facebook.
但是,如果用户在登录Facebook之前按下关闭/取消按钮,该怎么办。 I need to execute set of statements if user pressed cancel button.
如果用户按下“取消”按钮,则需要执行一组语句。 How can i do this.
我怎样才能做到这一点。 Any help would be appreciated.
任何帮助,将不胜感激。
You can use Facebook error category and to handle errors refer this link 您可以使用Facebook错误类别,并通过以下链接处理错误
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError* error){
if(!error){
//success do something
}
else{
//Error
if([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled){
//user have pressed on cancel/close button
}
else {
//loging failed
}
}
}];
try this 尝试这个
[FBSession setActiveSession:[[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"publish_actions,read_stream,user_hometown,user_birthday,email", nil]]];
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:
^(FBSession *session,FBSessionState state,NSError *error)
{
if(state == FBSessionStateOpen)
{
// use user's detail and post on Facebook
}
else if(state == FBSessionStateClosed)
{
// if user not authenticated
}
else if(steate == FBSessionStateClosedLoginFailed)
{
}
}];
After calling the login function, eg: 调用登录功能后,例如:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [NSArray arrayWithObjects:@"friends_photos",@"friends_birthday",@"email", nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
You can get the callback in the delegate meth9od , eg: 您可以在委托meth9od中获取回调,例如:
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState)state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if(!error)
{
}
break;
case FBSessionStateClosed:
{
NSLog(@"FBSessionStateClosed");
[FBSession.activeSession closeAndClearTokenInformation];
}
break;
case FBSessionStateClosedLoginFailed:
{
NSLog(@"FBSessionStateClosedLoginFailed :- logged failed");
}
break;
default:
break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@",
[AppDelegate FBErrorCodeDescription:error.code]]
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.