iphone - How to get session login information to my UIWebView by FacebookSDK -
after logging in facebook facebooksdk, , want display page using uiwebview. displayed correctly when login first time. close , restart app, though facebook session still open, can't login session uiwebview.
do have suggestions that, when re-run app, facebook session information?
this code example.
@interface fblogin2viewcontroller () @end @implementation fblogin2viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. if (fbsession.activesession.state == fbsessionstatecreatedtokenloaded) { // to-do, show logged in view nsstring *appid = [fbsession defaultappid]; nslog(@"session token exists %@", appid); } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)onlogin:(id)sender { nsarray *permissions = nil; if (![fbsession activesession]) { fbsession *session = [[fbsession alloc] initwithappid:@"000000000000" permissions:permissions defaultaudience:fbsessiondefaultaudienceeveryone urlschemesuffix:@"fb" tokencachestrategy:nil]; [fbsession setactivesession:session]; } else if( [fbsession activesession].isopen ) { [[fbsession activesession] closeandcleartokeninformation]; fbsession.activesession = nil; } [[fbsession activesession] openwithbehavior:fbsessionloginbehaviorforcingwebview completionhandler:^(fbsession *session, fbsessionstate state, nserror *error) { [self sessionstatechanged:session state:state error:error]; }]; } - (ibaction)onlogout:(id)sender { [fbsession.activesession closeandcleartokeninformation]; fbsession.activesession = nil; } - (ibaction)ongoweb:(id)sender { nsurl *url = [nsurl urlwithstring:@"http://www.facebook.com/xxxxx?fref=xx"]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [_webview loadrequest:request]; _webview.delegate = self; } - (void)sessionstatechanged:(fbsession *)session state:(fbsessionstate) state error:(nserror *)error { switch (state) { case fbsessionstateopen: { } break; case fbsessionstateclosed: case fbsessionstateclosedloginfailed: // once user has logged in, want them // looking @ root view. break; default: break; } if (error) { uialertview *alertview = [[uialertview alloc] initwithtitle:@"error" message:error.localizeddescription delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; } } @end
1)call openfbsessionifavaliable in didfinishlaunchingwithoptions method
//in appdelegate - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { [self openfbsessionifavaliable]; } //in appdelegate - (bool)application:(uiapplication *)application openurl:(nsurl *)url sourceapplication:(nsstring *)sourceapplication annotation:(id)annotation { // attempt extract token url return [[fbsession activesession] handleopenurl:url]; } //in appdelegate - (void)applicationdidbecomeactive:(uiapplication *)application { // restart tasks paused (or not yet started) while application inactive. if application in background, optionally refresh user interface. [[fbsession activesession] handledidbecomeactive]; } //in appdelegate - (void)applicationwillterminate:(uiapplication *)application { // called when application terminate. save data if appropriate. see applicationdidenterbackground:. [[fbsession activesession] close]; } -(void)openfbsessionifavaliable { [fbsession setactivesession:[fbsession new]]; if ([fbsession activesession].state == fbsessionstatecreatedtokenloaded) { [self openfbsessionwithallowloginui:no]; } } - (bool)openfbsessionwithallowloginui:(bool)allowloginui { nsarray *permissions = [[nsarray alloc] initwithobjects: @"email",nil]; return [fbsession openactivesessionwithreadpermissions:permissions allowloginui:allowloginui completionhandler:^(fbsession *session, fbsessionstate state, nserror *error) { [self sessionstatechanged:session state:state error:error]; }]; } - (void)sessionstatechanged:(fbsession *)session state:(fbsessionstate) state error:(nserror *)error { switch (state) { case fbsessionstateopen: if (!error) { nsstring *_facebooktoken = [[fbsession activesession] accesstoken]; nsstring *_facebookemail = [user objectforkey:@"email"]; } break; case fbsessionstateclosed: case fbsessionstateclosedloginfailed: [fbsession.activesession closeandcleartokeninformation]; break; default: break; } if (error) { [fbsession.activesession closeandcleartokeninformation]; /*uialertview *alertview = [[uialertview alloc] initwithtitle:@"error" message:@"please try again" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show];*/ } }
2) on button click of "login via facebook" call below method
[self openfbsessionwithallowloginui:yes];
3) every time when launch app, check facebook session, if found proceed further otherwise show button "login via facebook".session
i hope solve problem.
Comments
Post a Comment