2
  • I Want open facebook application at login time that already install in the device for login authentication, but always open in the Safari browser.

- facebook button click

    -(void)loginButtonClicked
        {
            FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

        //    [login setLoginBehavior:FBSDKLoginBehaviorNative];

                [login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
                 {
                     if (error)
                     {
                         NSLog(@"Login process error");
                     }
                     else if (result.isCancelled)
                     {
                         NSLog(@"User cancelled login");
                     }
                     else
                     {
                         NSLog(@"Login Success");
                         if ([result.grantedPermissions containsObject:@"email"])
                         {
                             NSLog(@"result is:%@",result);
                             [self fetchUserInfo];
                         }
                         else
                         {
        //                     [SVProgressHUD showErrorWithStatus:@"Facebook email permission error"];
                         }
                     }
                 }];
            }
        }

-here to fetch user info

        -(void)fetchUserInfo
        {
            if ([FBSDKAccessToken currentAccessToken])
            {
                NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);

                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email"}]
                 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                     if (!error)
                     {
                         NSLog(@"results:%@",result);

                         NSString *email = [result objectForKey:@"email"];
                         NSString *userId = [result objectForKey:@"id"];

                         if (email.length >0 )
                         {
                             NSString *accessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
                             [ref authWithOAuthProvider:@"facebook" token:accessToken
                                    withCompletionBlock:^(NSError *error, FAuthData *authData)
                              {
                                        if (error)
                                        {
                                            NSLog(@"Login failed. %@", error);
                                        }
                                        else
                                        {
                                            NSLog(@"Logged in! %@", authData);

                                            //Start you app Todo
                                        }
                         else
                         {
                             NSLog(@"Facebook email is not verified");
                         }
                     }
                     else
                     {
                         NSLog(@"Error %@",error);
                     }
                 }];

        }
  • I Want to open facebook application for login with facebook.
vipinsaini0
  • 541
  • 1
  • 7
  • 26

3 Answers3

7

From V4.6.0 it won't redirect to fb app. See below

(v4.6.0 - September 10, 2015) In addition, the SDK dialogs such as Login, Like, Share Dialogs automatically determine the best UI based on the device, including SFSafariViewController instead of Safari. Follow the our Preparing for iOS 9 guide.

enter image description here

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
1

Please Use FBLoginView as Facebook SignIn button It automatically detects Facebook app & u can able to login with facebook. FBLoginView class available in Facebook SDK.

SM18
  • 718
  • 7
  • 15
1

Please set info.plist parameters properly, you may have not provided the URLs like this

<array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb776035152513539</string>
            </array>
        </dict>
</array>

and

<key>FacebookAppID</key>
<string>776035152513539</string>

If you are not able to achieve what you want,then you can use these classes.

Header File -

//
//  LxFaceBookHandler.h
//  KickOffSlotMachine
//
//  Created by Prakhar Goyal on 06/08/15.
//  Copyright (c) 2015 LOGICNEXT. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

typedef NS_ENUM(NSInteger,LOGINRESPONSE)
{
    LOGINRESPONSE_ERROR,
    LOGINRESPONSE_CANCEL,
    LOGINRESPONSE_SUCCESS
};

@protocol LxFaceBookHandlerDelegate <NSObject>

-(void)DidLogInWithResponse:(LOGINRESPONSE)type;

@end

@interface LxFaceBookHandler : NSObject

@property(weak,nonatomic)id<LxFaceBookHandlerDelegate>delegate;
-(void)InitFaceBookLogin;

//Called from app Delegate

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

@end

and Implementation file is -

//
//  LxFaceBookHandler.m
//  KickOffSlotMachine
//
//  Created by Prakhar Goyal on 06/08/15.
//  Copyright (c) 2015 LOGICNEXT. All rights reserved.
//

#import "LxFaceBookHandler.h"

@implementation LxFaceBookHandler
@synthesize delegate =  __delegate;

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        __delegate = nil;
    }
    return self;
}

-(void)InitFaceBookLogin;
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             if (__delegate!=nil)
             {
                 if ( [__delegate respondsToSelector:@selector(DidLogInWithResponse:)])
                 {
                     [__delegate DidLogInWithResponse:LOGINRESPONSE_ERROR];
                 }
             }
         }
         else if (result.isCancelled)
         {
             if (__delegate!=nil)
             {
                 if ( [__delegate respondsToSelector:@selector(DidLogInWithResponse:)])
                 {
                     [__delegate DidLogInWithResponse:LOGINRESPONSE_CANCEL];
                 }
             }
         }
         else
         {
             if (__delegate!=nil)
             {
                 if ( [__delegate respondsToSelector:@selector(DidLogInWithResponse:)])
                 {
                     [__delegate DidLogInWithResponse:LOGINRESPONSE_SUCCESS];
                 }
             }

             //             if ([result.grantedPermissions containsObject:@"email"]) {
             //                 // Do work
             //             }
         }
     }];

}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];

}

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

Add FBSDKCoreKit.Framework and FBSDKLoginKit.Framework in your project and set your app Delefate like this -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor=[UIColor blackColor];

 SplashViewController *rVC = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];

    self.mNavController=[[UINavigationController alloc]initWithRootViewController:rVC];
    [self.mNavController setNavigationBarHidden:YES];

    self.window.rootViewController=self.mNavController;
    [self.window makeKeyAndVisible];

    self.handler = [[LxFaceBookHandler alloc]init];



    [self.handler application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [self.handler application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

now you can use these classes as on any event suppose on button click-

- (IBAction)FacebookLoginPressed:(UIButton *)sender
{
    App.handler.delegate  = self;
    [App.handler InitFaceBookLogin];
}