How to redo action before Login to Facebook?

Discussion in 'iPhone/iPad Programming' started by mikezang, Mar 12, 2012.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    In my App, when user select a item in Table view, my App will check if Facebook in login status.

    If already login to Facebook, my app will post item to Facebook wall.

    If not loin to Facebook, Facebook login screen will be shown, after login to Facebook, control will return to my app, but user has to select item again to post to Facebook wall.

    In this case, How can my app post to wall after login to Facebook without select item again?
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Sharing the code you've written so far in an attempt to implement this would help us help you...
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Here is my code when user selects a item, do you have any idea?

    Code:
    -(void)postToFacebook {    
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
            facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
            facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
        }
    
        if (![facebook isSessionValid]) {
            NSArray *permissions =  [NSArray arrayWithObjects:@"read_stream", @"publish_stream", nil];
            [facebook authorize:permissions];
        }
        
        NSString *title = [self event];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       title,  @"message",
                                       nil];
        
        [facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
    }
    
     
  4. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    I'm not familiar with Facebook's graph APIs, but I would guess that

    Code:
    [facebook authorize:permissions];
    will have some kind of callback it will send to a delegate as soon as the user successfully logs on. When your delegate receives the callback, make the attempt to post to the user's wall again.
     
  5. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    That is what I want to know, the callback is as below, I don't know how to post again!
    Code:
    // Pre 4.2 support
    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        return [detailViewController.facebook handleOpenURL:url]; 
    }
    
    // For 4.2+ support
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
        return [detailViewController.facebook handleOpenURL:url]; 
    }
    
     
  6. macrumors newbie

    sundialsoft

    Joined:
    Sep 2, 2010
    Location:
    Scotland
    #6
    Facebook - posting after login

    At the point where your user first makes a selection you can save the selection either as global variables (say in your app delegate) or in nsuserdefaults.
    Once the login is complete you just do a facebook post with the saved data. It's really simple. If you can do one post with data you can do the same thing later after the login with the same data so long as you have preserved it.

    I assume you are using the latest FBConnect which is well documented & easier to follow than earlier versions.

    I'm very familiar with Facebook posts so can assist if you are still stuck.
     
  7. mikezang, Mar 16, 2012
    Last edited: Mar 16, 2012

    thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    Thanks for your suggestion. i already got what I need as below,
    Code:
    -(void)postToFacebook { 
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
            facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
            facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
        }
    
        if (![facebook isSessionValid]) {
            // save IndexPath to use next time 
            NSArray *permissions = *[NSArray arrayWithObjects:@"read_stream", @"publish_stream", nil];
            [facebook authorize:permissions];
        }
        else {
           NSString *title = [self event];// use IndexPath in this method
           NSMutableDictionary *params = = [NSMutableDictionary dictionaryWithObjectsAndKeys:title,  @"message", nil];
    
           [facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
        }
    }
    
     

Share This Page