Skip to content

vksgupta/StockholmNativeHack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

CODE PART 1


BEGIN----------------- Initial Set Up (After Dragging in src file for Facebook SDK)



1. Create new app at developers.facebook.com/apps. Use this app id in the <APP_ID> code below.

2. In app delegate header

#import "FBConnect.h"

3.  Add property for Facebook

@property (nonatomic, retain) Facebook *facebook;

4. Implementation, add
@synthesize facebook;

5. Cleanup
[facebook release];

6. Initialize Facebook instance

header:

FBSessionDelegate

implementation

    facebook = [[Facebook alloc] initWithAppId:@"<APP_ID>"
                                   andDelegate:self];


7. Declare login/logout method (header)

- (void) login;
- (void) logout;



8. Implement the login/logout SDK calls

- (void) login {
    [facebook authorize:nil];
}

- (void) logout {
    [facebook logout];
}



9. Set up welcome label and login/logout buttons in Viewcontroller NIB then define the button action

#import "AppDelegate.h"



    AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    if ([[delegate facebook] isSessionValid]) {
        [delegate logout];
    } else {
        [delegate login];
    }




10. Set up the session delegate handlers and configure how the welcome label and login button are changed when the state changes.


// Implement session delegate methods
- (void)fbDidLogin {
    self.viewController.welcomeLabel.text = @"Welcome ...";
    [self.viewController.authButton setImage:[UIImage imageNamed:@"FBConnect.bundle/images/LogoutNormal.png"] forState:UIControlStateNormal];
}

- (void)fbDidLogout {
    self.viewController.welcomeLabel.text = @"Login to continue";
        [self.viewController.authButton setImage:[UIImage imageNamed:@"FBConnect.bundle/images/LoginWithFacebookNormal.png"] forState:UIControlStateNormal];
}

- (void)fbDidNotLogin:(BOOL)cancelled {
}

- (void)fbDidExtendToken:(NSString*)accessToken
               expiresAt:(NSDate*)expiresAt {
}


- (void)fbSessionInvalidated {
}



11. Set up the entry point back into the app to make sure SSO works correctly

// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url];
}

// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [facebook handleOpenURL:url];
}


and in Info.plist

fb<APP_ID>



END ------------------ Initial Set Up


BEGIN ------------------Personalize (Graph API)

1. Add to delegate handler list
FBRequestDelegate

- (void) apiGraphMe;

/*
 * Graph API: Get the user's basic information, picking the name and picture fields.
 */
- (void)apiGraphMe {

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"name",  @"fields",
                                   nil];
    [facebook requestWithGraphPath:@"me" andParams:params andDelegate:self];
}

- (void)request:(FBRequest *)request didLoad:(id)result {
    self.viewController.welcomeLabel.text = [NSString stringWithFormat:@"Welcome %@", [result objectForKey:@"name"]];
}



- (void)fbDidLogin {
    self.viewController.welcomeLabel.text = @"Welcome ...";
    [self.viewController.authButton setImage:[UIImage imageNamed:@"FBConnect.bundle/images/LogoutNormal.png"] forState:UIControlStateNormal];

    // Personalize
    [self apiGraphMe];
}


END ------------------ Personalize (Graph API)



BEGIN----------------Enable SSO in the app Settings


Enable SSO in App Settings:

FB Messenger - 454638411


END----------------Enable SSO in the app Settings





CODE PART 2




SSO Pitfalls:

BEGIN------------------ Save sessions in persistent storage for better UX

1. Save and clear the session

- (void)fbDidLogin {
    self.viewController.welcomeLabel.text = @"Welcome ...";
    [self.viewController.authButton setImage:[UIImage imageNamed:@"FBConnect.bundle/images/LogoutNormal.png"] forState:UIControlStateNormal];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

- (void)fbDidLogout {
    self.viewController.welcomeLabel.text = @"Login to continue";
        [self.viewController.authButton setImage:[UIImage imageNamed:@"FBConnect.bundle/images/LoginWithFacebookNormal.png"] forState:UIControlStateNormal];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}


2. Check the session. Add to application launching part after Facebook instantiation

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if ([facebook isSessionValid]) {
        [self fbDidLogin];
    }

END------------------ Save sessions in persistent storage for better UX



BEGIN----------------Adding the Bundle ID to get rid of the okay interstitial screen

10. Bundle ID
com.facebook.samples.mobilehack



END----------------Adding the Bundle ID to get rid of the okay interstitial screen





CODE PART 3




BEGIN---------------- Deep Linking

Header:
FBDialogDelegate

- (void) post;

Implementation:
- (void) post {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"I'm using the Hackbook for iOS app", @"name",
                                   @"Hackbook for iOS.", @"caption",
                                   @"Check out Hackbook for iOS to learn how you can make your iOS apps social using Facebook Platform.", @"description",
                                   @"http://www.tunedon.com/texto", @"link",
                                   nil];


    [facebook dialog:@"feed" andParams:params andDelegate:self];
}

in fbDidLogin:

[self.viewController.postButton setHidden:NO];

- (IBAction)postButtonClicked:(id)sender
{
    AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    if ([[delegate facebook] isSessionValid]) {
        [delegate post];
    }

}

PARSE URL:

- (NSDictionary*) parseURLParams:(NSString *)query;


// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    NSString *query = [url fragment];
    NSDictionary *params = [self parseURLParams:query];
    // Check if target URL exists
    NSString *targetURLString = [params valueForKey:@"target_url"];
    if (targetURLString) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Deep Link!"
                              message:[NSString stringWithFormat:@"Incoming: %@", @"Deep Link"]
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil,
                              nil];
        [alert show];
        [alert release];
    }
    return [facebook handleOpenURL:url];
}

/**
 * A function for parsing URL parameters.
 */
- (NSDictionary*)parseURLParams:(NSString *)query {
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    NSMutableDictionary *params = [[[NSMutableDictionary alloc] init]
                                   autorelease];
    for (NSString *pair in pairs) {
        NSArray *kv = [pair componentsSeparatedByString:@"="];
        NSString *val = [[kv objectAtIndex:1]
                         stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [params setObject:val forKey:[kv objectAtIndex:0]];
    }
    return params;
}



END---------------- Deep Linking




OPTIONAL


BEGIN------------------ Extend token to handle offline_access changes

-(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
    [defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [facebook extendAccessTokenIfNeeded];
}


END------------------ Extend token to handle offline_access changes






BEGIN------------------ Handle session expiration


- (void)fbSessionInvalidated {
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Auth Exception"
                              message:@"Your session has expired."
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil,
                              nil];
    [alertView show];
    [alertView release];
    [self fbDidLogout];
}

END------------------ Handle expiration

About

Stockholm Native Mobile Hack

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published