我已经 在我的应用程序中使用 Facebook sdk 3.5.3集成了嵌入式 webview
我希望我的代码能给你一些想法......
-(IBAction)fblogin:(id)sender{
NSArray * arr=[NSArray arrayWithObjects:@"publish_actions",@"email",@"basic_info",@"user_location",@"user_birthday",@"user_likes", nil];
Interest_Status=[NSString stringWithFormat:@"%@",txt_interest.text];
[FBSession setActiveSession:session];
if (FBSession.activeSession.isOpen) {
if ([btn_Login.titleLabel.text isEqualToString:@"Logout"]) {
[btn_Login setTitle:@"Login" forState:UIControlStateNormal];
[btn_Login setImage:[UIImage imageNamed:@"FBLogin.png"] forState:UIControlStateNormal];
FBSessionTokenCachingStrategy *tokenCachingStrategy = [[FBSessionTokenCachingStrategy alloc]
initWithUserDefaultTokenInformationKeyName:nil];
tokenCachingStrategy=nil;
FBSession *seession=[FBSession activeSession];
[seession closeAndClearTokenInformation];
[session close];
[FBSession setActiveSession:nil];
session=nil;
session=[[FBSession alloc]initWithPermissions:arr];
self.ProfileLale.hidden=YES;
}
/*else if ([btn_Login.titleLabel.text isEqualToString:@"Login"]) {
[self updateForSessionChangeForSlot:1];
}*/
}else{
[session openWithBehavior:FBSessionLoginBehaviorForcingWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// this handler is called back whether the login succeeds or fails; in the
// success case it will also be called back upon each state transition between
// session-open and session-close
if (error)
{
NSLog(@"error=%@ \n\n description=%@ \n\n,",error,error.description);
[self switchToNoActiveUser];
}else{
[self updateForSessionChangeForSlot:1];
}
}];
}
}
- (void)updateForSessionChangeForSlot:(int)slot {
if (session.isOpen) {
// fetch profile info such as name, id, etc. for the open session
// Fetch user data
FBRequest *me = [[FBRequest alloc] initWithSession:session
graphPath:@"me"];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
// because we have a cached copy of the connection, we can check
// to see if this is the connection we care about; a prematurely
// cancelled connection will short-circuit here
// if (me != self.pendingRequest) {
// return;
// }
NSLog(@"user=%@",user);
// self.pendingRequest = nil;
// self.pendingLoginForSlot = -1;
// we interpret an error in the initial fetch as a reason to
// fail the user switch, and leave the application without an
// active user (similar to initial state)
if (error) {
NSLog(@"error=%@",error);
NSLog(@"Couldn't switch user: %@", error.localizedDescription);
[self switchToNoActiveUser];
return;
}else{
NSLog(@"user=%@",user);
[btn_Login setTitle:@"Logout" forState:UIControlStateNormal];
[btn_Login setImage:[UIImage imageNamed:@"FBLogout.png"] forState:UIControlStateNormal];
}
}];
} else {
// in the closed case, we check to see if we picked up a cached token that we
// expect to be valid and ready for use; if so then we open the session on the spot
if (session.state == FBSessionStateCreatedTokenLoaded) {
// even though we had a cached token, we need to login to make the session usable
[session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
[self updateForSessionChangeForSlot:slot];
}];
}
}
}
- (void)switchToNoActiveUser {
/*UIAlertView *alter=[[UIAlertView alloc]initWithTitle:@"Message" message:@"No Internet connecton" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alter show];
*/
NSArray * arr=[NSArray arrayWithObjects:@"publish_actions",@"email",@"basic_info",@"user_location",@"user_birthday",@"user_likes", nil];
session = nil;
session=[[FBSession alloc]initWithPermissions:arr];
[btn_Login setTitle:@"Login" forState:UIControlStateNormal];
self.ProfileLale.hidden=YES;
[btn_Login setImage:[UIImage imageNamed:@"FBLogin.png"] forState:UIControlStateNormal];
}