2
votes

I have a very simple question.

In FB tutorial https://developers.facebook.com/docs/mobile/ios/build/ it starts to login inside didFinishLaunchingWithOptions – right after applications's launch.

I need to login on tap, then wait for a callback and send a message on the FB wall. I think Hackbook app example design application is too complicated for this purpose.

What is the simplest way to achieve this?

UPD: I've followed the Hackbook example, but ViewControllers still didn't get a callback :(

yAppDelegate.h:


    #import 
    #import "FBConnect.h"
    
    @interface yAppDelegate : UIResponder 
    {
        Facebook *facebook;
        
    }
    
    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic, retain) Facebook *facebook;
    
    @end

yAppDelegate.m:


    #import "yAppDelegate.h"
    #import "yViewController.h"
    
    static NSString* kAppId = @"350435425024264";
    
    @implementation yAppDelegate
    
    @synthesize window = _window;
    @synthesize facebook;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {    
        yViewController *viewController = [[yViewController alloc] init];
        
        facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:viewController];
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
            facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
            facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
        }
        
        return YES;
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [[self facebook] extendAccessTokenIfNeeded];
    }
    
    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        return [self.facebook handleOpenURL:url];
    }
    
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
        return [self.facebook handleOpenURL:url];
    }

yViewController.h:


    #import 
    #import "FBConnect.h"
    
    @interface yViewController : UIViewController 
    {
        NSArray *permissions;
    }
    
    @property (nonatomic, retain) NSArray *permissions;
    
    @end

yViewController.m


    #import "yViewController.h"
    #import "yAppDelegate.h"
    #import "FBConnect.h"
    
    @interface yViewController ()
    @end
    
    @implementation yViewController
    @synthesize permissions;
    
    - (IBAction)buttonPressed:(UIButton *)sender {
        
        NSLog(@"Button pressed!");
        
        permissions = [[NSArray alloc] initWithObjects:@"offline_access", nil];
        
        yAppDelegate *delegate = (yAppDelegate *)[UIApplication sharedApplication].delegate;
        if (![[delegate facebook] isSessionValid]) {
            [[delegate facebook] authorize:permissions];
        } else {
            //[self showLoggedIn];
        }
        NSLog(@"login!!");
        
    }
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    #pragma mark - FBSessionDelegate Methods
    /**
     * Called when the user has logged in successfully.
     */
    - (void)fbDidLogin {
        NSLog(@"did login");
    }
    
    -(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
        NSLog(@"token extended");
    }
    
    /**
     * Called when the user canceled the authorization dialog.
     */
    -(void)fbDidNotLogin:(BOOL)cancelled {
        NSLog(@"fbDidNotLogin");
    }
    
    /**
     * Called when the request logout has succeeded.
     */
    - (void)fbDidLogout {
        
        NSLog(@"fbDidLogout");
        
        // Remove saved authorization information if it exists and it is
        // ok to clear it (logout, session invalid, app unauthorized)
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    
    }
    
    /**
     * Called when the session has expired.
     */
    - (void)fbSessionInvalidated {
        
        NSLog(@"fbSessionInvalidated");
        
        [self fbDidLogout];
    }
    
    #pragma mark - FBRequestDelegate Methods
    /**
     * Called when the Facebook API request has returned a response.
     *
     * This callback gives you access to the raw response. It's called before
     * (void)request:(FBRequest *)request didLoad:(id)result,
     * which is passed the parsed response object.
     */
    - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
        //NSLog(@"received response");
    }
    
    /**
     * Called when a request returns and its response has been parsed into
     * an object.
     *
     * The resulting object may be a dictionary, an array or a string, depending
     * on the format of the API response. If you need access to the raw response,
     * use:
     *
     * (void)request:(FBRequest *)request
     *      didReceiveResponse:(NSURLResponse *)response
     */
    - (void)request:(FBRequest *)request didLoad:(id)result {
        NSLog(@"-(void)request");
    }
    
    /**
     * Called when an error prevents the Facebook API request from completing
     * successfully.
     */
    - (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
        NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
        NSLog(@"Err code: %d", [error code]);
    }

@end
1

1 Answers

2
votes

Do it in exactly the same way that the FB tutorial shows, just move the login code into a button event or whatever you want to trigger it.