How to integrate Facebook without redirect Safari browser in iOS app with latest FBSDK
Solution 1
yesterday I submit my app to store using FBSDKLoginManager
using three kinds of Login with Facebook , actually Facebook follow the first three conditions automatically
Type -1
if the user already installed the Facebook native app the details will be retrieved from the app.
Type -2
if the user does't have the Facebook app, but he is logged in at the device's Settings, the user details will be taken from those.
Type -3
if none of the condition above is satisfied the user will be automatically redirected to Safari
.
Type -4
if you none of the above three conditions is relevant for you please continue below code.
apple does not reject your app
Objective-C
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior=FBSDKLoginBehaviorWeb; // it open inside the app using popup menu
Swift
var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.Native
FBSDKLoginBehavior Types
typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
{
/*!
@abstract Attempts log in through the native Facebook app. If the Facebook app is
not installed on the device, falls back to \c FBSDKLoginBehaviorBrowser. This is the
default behavior.
*/
FBSDKLoginBehaviorNative = 0,
/*!
@abstract Attempts log in through the Safari browser
*/
FBSDKLoginBehaviorBrowser,
/*!
@abstract Attempts log in through the Facebook account currently signed in through Settings.
If no Facebook account is signed in, falls back to \c FBSDKLoginBehaviorNative.
*/
FBSDKLoginBehaviorSystemAccount,
/*!
@abstract Attemps log in through a modal \c UIWebView pop up
@note This behavior is only available to certain types of apps. Please check the Facebook
Platform Policy to verify your app meets the restrictions.
*/
FBSDKLoginBehaviorWeb,
};
the new concept
(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.
you can get like
Solution 2
When your select login behavior is FBSDKLoginBehaviorWeb
, Then open webView popUp.
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior=FBSDKLoginBehaviorWeb;
PopUp Facebook :
Note : when facebook behavior is FBSDKLoginBehaviorBrowser
then its always redirect on safari browser. Change its behavior to FBSDKLoginBehaviorWeb
.
Use This For Latest Update from FB SDK
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorWeb;
NSArray *userdetails;
[login logInWithReadPermissions:userdetails fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
NSLog(@"Login Error : %@", error.description);
}
else if(result.isCancelled)
{
NSLog(@"Login Cancel By User");
}
else
{
//[self fetchUserDataForFacebookLogin];
//[login logOut];
}
}];
Solution 3
In swift When your select login behavior is Web, Then open webView popUp.
// Try to login with permissions
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
Solution 4
Just open the Facebook Developer site http://facebookdevelopers.com there you will get the whole procedure to integrate facebook in your application without redirecting to Safari browser.
Solution 5
Here i use the code like:
AppDelegate.swift
import UIKit
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func applicationWillResignActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
}
LoginViewController.swift
//MARK: FacebookLogin Button
@IBAction func btnFacebookLoginClicked(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logOut()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web //Without safari Browser
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
if (error != nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email")){
self.getFBUserData()
fbLoginManager.logOut()
}
}else if result.isCancelled {
print("Cancelled")
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
self.facebookUserDict = result as! NSDictionary
print(result)
print(self.facebookUserDict)
print(self.facebookUserDict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as! String)
}
})
}
}
Related videos on Youtube
Comments
-
priyadharshini over 1 year
Hi i am search the answer for avoid new facebooksdk redirect to safari.how to open facebook login view inside the app.if it redirect to safari may reject in app store? help me.. thanks in advance
-
Anbu.Karthik almost 9 yearsI updated my answer please check
-
-
priyadharshini almost 9 yearsFBSDKLoginManager to use share dialog is possible
-
priyadharshini almost 9 years@ Anbu.Karthik:possible to get invited friends list from fbsdk
-
Anbu.Karthik almost 9 years@priya -- // For more complex open graph stories, use
FBSDKShareAPI
// withFBSDKShareOpenGraphContent
/* make the API call */ FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/invitable_friends" parameters:params HTTPMethod:@"GET"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { // Handle the result }]; -
Anbu.Karthik almost 9 years@priya -- need ref this link developers.facebook.com/docs/games/invitable-friends/v2.4
-
lukeswitz over 7 yearsThis is useful, the new login flow is terrible and the analytics back it up. This at least eliminates users already logged into Safari to continue. This their whole new schema is terrible UX.
-
Mahesh Agrawal almost 7 yearsas per latest policy of Facebook, only specific type of apps can only use this behaviour. The answer should have a note on this.
-
Ekta Padaliya about 6 yearsI need your help.