Skip to content

Instantly share code, notes, and snippets.

@rsms
Last active February 6, 2022 20:28

Revisions

  1. rsms revised this gist Feb 8, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions MyAppDelegate.m
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,10 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
    // time the app is activated. The call is a no-op if the app has once successfully published
    // the install, so there's no additional performance penalty to calling this often.
    [FBSettings publishInstall:nil];

    // The following line will handle some edge-cases of the authentication flow, such as a user
    // switching back to the app instead of explicitly canceling or follow through with SSO.
    [FBSession.activeSession handleDidBecomeActive];
    }

    - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
  2. rsms revised this gist Feb 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyAppDelegate.m
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
    // where the number of installs can be presented dynamically as part of the ad.
    // We place it here in applicationDidBecomeActive since if for some reasons the logging server
    // can't be reached the first time the app is launched, we will make a new attempt the next
    // time the app is launched. The call is a no-op if the app has once successfully published
    // time the app is activated. The call is a no-op if the app has once successfully published
    // the install, so there's no additional performance penalty to calling this often.
    [FBSettings publishInstall:nil];
    }
  3. rsms revised this gist Feb 7, 2013. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions MyAppDelegate.m
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,17 @@

    @implementation MyAppDelegate

    - (void)applicationDidBecomeActive:(UIApplication *)application {
    // Make sure to log "app installed". This enables you as a developer to track number of
    // installs of your app. It also helps you with promoting your app through ads on Facebook
    // where the number of installs can be presented dynamically as part of the ad.
    // We place it here in applicationDidBecomeActive since if for some reasons the logging server
    // can't be reached the first time the app is launched, we will make a new attempt the next
    // time the app is launched. The call is a no-op if the app has once successfully published
    // the install, so there's no additional performance penalty to calling this often.
    [FBSettings publishInstall:nil];
    }

    - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
    // In the login workflow, the Facebook native application, or Safari will transition back to
    // this applicaiton via a url following the scheme fb[app id]://; the call to handleOpenURL
  4. rsms revised this gist Feb 6, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. rsms created this gist Feb 6, 2013.
    20 changes: 20 additions & 0 deletions MyAppDelegate.mm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #import <FacebookSDK/FacebookSDK.h>

    @implementation MyAppDelegate

    - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
    // In the login workflow, the Facebook native application, or Safari will transition back to
    // this applicaiton via a url following the scheme fb[app id]://; the call to handleOpenURL
    // below captures the token, in the case of success, on behalf of the FBSession object
    return [FBSession.activeSession handleOpenURL:url];
    }

    - (void)applicationWillTerminate:(UIApplication*)application {
    // Close the FB session before quitting
    // this is a good idea because things may be hanging off the session, that need
    // releasing (completion block, etc.) and other components in the app may be awaiting
    // close notification in order to do cleanup
    [FBSession.activeSession close];
    }

    @end
    68 changes: 68 additions & 0 deletions MyViewController.mm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #import <FacebookSDK/FacebookSDK.h>

    @implementation MyViewController

    - (void)viewDidLoad {
    [super viewDidLoad];
    if (!FBSession.activeSession.isOpen) {
    [self signInWithFacebook];
    } else {
    [self isSignedInWithFacebook];
    }
    }

    - (void)signInWithFacebook {
    FBSession* session = [[FBSession alloc] initWithPermissions:@[
    // Sample permissions.
    // See https://developers.facebook.com/docs/reference/login/#permissions
    @"friends_about_me",
    @"friends_location"]];
    // The following line will trigger the following events to take place, which
    // will finally call the completionHandler:
    //
    // 1. If there's a Facebook account registered in the iOS system (i.e. in iOS
    // Settings), then present an OS-native permission dialog. Otherwise...
    //
    // 2. If the Facebook app is installed, activate that app which will present
    // a native permission dialog. Otherwise...
    //
    // 3. Activate Safari with m.facebook.com which will present a permission
    // dialog.
    //
    // In any case your app will be activated when the user either logs in or
    // abort/cancel the login. Your app delegate will be messaged through
    // -application:openURL:sourceApplication:annotation: when this happens.
    //
    [session openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:
    ^(FBSession *session, FBSessionState state, NSError *error) {
    NSLog(@"FB session opened -> %@, %@", session, error);
    switch (state) {
    case FBSessionStateClosedLoginFailed: {
    [[[UIAlertView alloc] initWithTitle:@"Error"
    message:error.localizedDescription
    delegate:nil
    cancelButtonTitle:@"OK"
    otherButtonTitles:nil] show];
    break;
    }
    default: {
    [FBSession setActiveSession:session];
    [self isSignedInWithFacebook];
    break;
    }
    }
    }
    ];
    }

    - (void)isSignedInWithFacebook {
    // You can now talk to Facebook using FBSession.activeSession

    // If a response from Facebook contains an error indicating the user session
    // expired or has been invalidated (e.g. due to user changing her password),
    // you want to do this:
    // [FBSession.activeSession closeAndClearTokenInformation];
    // [self signInWithFacebook];
    }

    @end