Saturday, 15 September 2012

iphone - Get publish permission for Facebook app for iOS with -openWithBehavior:completionHandler: -



iphone - Get publish permission for Facebook app for iOS with -openWithBehavior:completionHandler: -

in application need user sign in facebook, friend list in table view , post on feeds, don't want redirect user anywhere. used -openwithbehavior:completionhandler: ... here code.

-(ibaction)loginaction:(id)sender { [self deletecookies]; // app delegate can access session property dlappdelegate *appdelegate = [[uiapplication sharedapplication]delegate]; // button's job flip-flop session open closed if (appdelegate.session.isopen) { // if user logs out explicitly, delete cached token information, , next // time run applicaiton presented log in ux again; // users close app or switch away, without logging out; // cause implicit cached-token login occur on next launch of application [appdelegate.session closeandcleartokeninformation]; } else { if (appdelegate.session.state != fbsessionstatecreated) { // create new, logged out session. appdelegate.session = [[fbsession alloc] init]; [self updateview]; } // if session isn't open, let's open , nowadays login ux user [appdelegate.session openwithbehavior:fbsessionloginbehaviorforcingwebview completionhandler:^(fbsession *session, fbsessionstate status, nserror *error) { switch (status) { case fbsessionstateopen: // phone call legacy session delegate //now session open corresponding ui changes { fbcachedescriptor *cachedescriptor = [fbfriendpickerviewcontroller cachedescriptor]; [cachedescriptor prefetchandcacheforsession:session]; [fbsession openactivesessionwithallowloginui:no]; [fbsession openactivesessionwithpublishpermissions:[nsarray arraywithobjects:@"publish_stream",@"publish_actions", nil] defaultaudience:fbsessiondefaultaudiencefriends allowloginui:no completionhandler:nil]; } break; case fbsessionstateclosedloginfailed: { // prefer maintain decls near utilize // unpack error code , reason in order compute cancel bool // phone call legacy session delegate if needed //[[delegate facebook] fbdialognotlogin:userdidcancel]; } break; // presently extension, log-out , invalidation beingness implemented in facebook class default: break; // nil in response state transitions } [self updateview]; }]; } }

the user signed in , can retrieve friend list using fql. problem while posting feeds. know need publish permissions it. when uses next code post...

- (ibaction)postaction:(id)sender { dlappdelegate *appdelegate = [[uiapplication sharedapplication]delegate]; if (appdelegate.session.isopen) { [fbsession openactivesessionwithallowloginui:no]; nsmutabledictionary *postparams = [[nsmutabledictionary alloc] initwithobjectsandkeys: @"https://developers.facebook.com/ios", @"link", @"https://developers.facebook.com/attachment/iossdk_logo.png", @"picture", @"facebook sdk ios", @"name", @"build great social apps , more installs.", @"caption", @"the facebook sdk ios makes easier , faster develop facebook integrated ios apps.", @"description", nil]; if ([_posttext.text length]>0) { [postparams setobject:[_posttext text] forkey:@"message"]; } if (([fbsession.activesession.permissions indexofobject:@"publish_actions"] == nsnotfound) || ([fbsession.activesession.permissions indexofobject:@"publish_stream"] == nsnotfound)) { // no permissions found in session, inquire [fbsession.activesession reauthorizewithpublishpermissions: [nsarray arraywithobjects:@"publish_stream",@"publish_actions",nil] defaultaudience:fbsessiondefaultaudiencefriends completionhandler:^(fbsession *session, nserror *error) { if (!error) { // if permissions granted, publish story [self publishstory:postparams]; } }]; } else { // if permissions present, publish story [self publishstory:postparams]; } } } -(void)publishstory:(nsdictionary *)postparams { [fbrequestconnection startwithgraphpath: @"me/feed" parameters:postparams httpmethod:@"post" completionhandler:^(fbrequestconnection *connection, id result, nserror *error) { if (!error) { //tell user worked. uialertview *alertview = [[uialertview alloc] initwithtitle:@"shared:" message:[nsstring stringwithformat:@"sucessfully posted wall."] delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil]; alertview.tag = 101; [alertview show]; } else { uialertview *alertview = [[uialertview alloc] initwithtitle:@"error:" message:error.localizeddescription delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; nslog(@"%@",error); } } ]; }

this code redirects user safari or facebook app. don't want happen.

definitely need publish permissions while logging in. question how?

you have set fbsessionloginbehavior, alter way use:

[session openwithbehavior:fbsessionloginbehaviorwithnofallbacktowebview completionhandler:^(fbsession *session, fbsessionstate status, nserror *error) { // respond session state changes, // ex: updating view }];

i see utilize fbsessionloginbehaviorforcingwebview, want have take enum:

typedef enum { /*! effort facebook login, inquire user credentials if necessary */ fbsessionloginbehaviorwithfallbacktowebview = 0, /*! effort facebook login, no direct request credentials made */ fbsessionloginbehaviorwithnofallbacktowebview = 1, /*! effort webview login; inquire user credentials */ fbsessionloginbehaviorforcingwebview = 2, /*! effort facebook login, prefering scheme business relationship , falling fast app switch if necessary */ fbsessionloginbehaviorusesystemaccountifpresent = 3, } fbsessionloginbehavior;

now solve "definitely need publish permissions while logging in. question how?" may - (id)initwithpermissions:(nsarray*)permissions; session :

nsarray *permissions = @[@"publish_stream", @"publish_actions"]; appdelegate.session = [[fbsession alloc] initwithpermissions:permissions];

iphone ios facebook facebook-graph-api facebook-fql

No comments:

Post a Comment