Last active
February 6, 2022 20:28
-
-
Save rsms/4726728 to your computer and use it in GitHub Desktop.
Barebones example of how to use the Facebook iOS SDK for signing in with Facebook using the "best" approach available (iOS native, Facebook app SSO or Safari SSO.) Get the SDK and check out the getting-started guide at https://developers.facebook.com/ios/ and find some complete sample apps at https://github.com/facebook/facebook-ios-sdk/tree/mas…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <FacebookSDK/FacebookSDK.h> | |
@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 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 { | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
isSignedInWithFacebook implies it should return a BOOL. Probably should just be signedInWithFacebook.