Login with Facebook for iOS
Solution 1
1) Follow these steps for facebook login instructions: https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/
2) Once you implement the above using facebook sdk 3.x, this will also work for iOS 5 and iOS 6.
Solution 2
1.To support login in ios 5 and ios 6 , you can use FBLoginView Class . it is the simplest way to login into facebook using Facebook sdk .
FBLoginView * pFBLoginViewObj = [[FBLoginView alloc] init];
[pFBLoginViewObj setFrame:self.view.frame];
pFBLoginViewObj.delegate = self;//optional
[self.view addSubview:pFBLoginViewObj];
implement delegate methods if needed .
2.To maintain session you have to make changes in app dalagate file ....like
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if (url != nil)
{
return [[FBSession activeSession] handleOpenURL:url];
}
return NO;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[FBSession activeSession] handleDidBecomeActive];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[[FBSession activeSession] close];
}
Solution 3
First of all we have to add this frameworks in our projects,
after that you can import this methods in appDelegate
#import <FacebookSDK/FacebookSDK.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
after that didFinishLaunchingWithOptions we have to add this method
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
you can call Callback Methods,
#pragma mark - Callback methods
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}
after that in your facebooklogin action method we have to write,
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"public_profile", @"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
//TODO: process error or result.
if (error) {
NSLog(@"Process error");
[self facebookLoginFailed:YES];
} else if (result.isCancelled) {
[self facebookLoginFailed:NO];
} else {
if ([FBSDKAccessToken currentAccessToken]) {
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"id,name,email,first_name,last_name,picture.type(large)" forKey:@"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *fbPhotoUrl = [[[result objectForKey:@"picture"]objectForKey:@"data"]objectForKey:@"url"];
lDefaults = [NSUserDefaults standardUserDefaults];
[lDefaults setObject:@"fbPhotoUrl" forKey:@"fbPhotoUrl"];
[lDefaults synchronize];
NSLog(@"%@",fbPhotoUrl);
}
}];
}
else {
[self facebookLoginFailed:YES];
}
}
}];
if facebook login failed we can write this alert,
- (void)facebookLoginFailed:(BOOL)isFBResponce{
if(isFBResponce){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"pop_attention", nil) message:NSLocalizedString(@"request_error", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"pop_ok", nil) otherButtonTitles: nil];
[alert show];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"pop_attention", nil) message:NSLocalizedString(@"loginfb_cancelled", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"pop_ok", nil) otherButtonTitles: nil];
[alert show];
}
}
after that goto target-->info-->Url Types-->UrlSchmes(add your facebook id).
and more thing we have to do in www.developers.facebook.com go to ypur app-->under AppReview-->Make YourApp public? as YES.
after that we need to add tis code in plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb1801381186770425</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>AddYourID</string>
<key>FacebookDisplayName</key>
<string>YourAppName</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>instagram</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>To access your current location</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>To share </string>
that's it in Objective-C.
In Swift Version
In appdelegate method we need to import this frameworks
import FacebookCore
import FacebookLogin
import FBSDKLoginKit
after that in didFinishLaunchingWithOptions write
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
and call callback method
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
}
after that in your viewcontroller import same frameworks after that write thiese lines of code in your facebook login action
var dict : [String : AnyObject]!
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil {
if(fbloginresult.grantedPermissions.contains("email")) {
self.getFBUserData()
}
}
}
}
after that i am getting user data and storing in Dictionary
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
self.dict = result as! [String : AnyObject]
print(result!)
print(self.dict)
}
})
}
}
iMash
Updated on July 09, 2022Comments
-
iMash almost 2 years
I have application that is already running on iOS 5 as well as iOS 6. Now for next step I want to implement functionality of "Login with Facebook".
My application already has login authentication so the facebook login will be additional one.I knew that iOS 6 has provided new framework for facebook but I have two questions:
- How do I implement this facebook functionality, so that it should work on both iOS 5 and iOS 6 devices? 2.How do I maintain session with facebook login?
-
Patel Jigar over 8 yearswhat about ios 9 objective-c