Login with facebook error on iOS in flutter project

6,168

UPDATE (JULY 2020)

You can use a new package that works for both Android and iOS: flutter_login_facebook

If you want to use the flutter_facebook_login, below is a solution:

UPDATE (APRIL 2020)

  1. Add flutter_facbook_login version 3.0.0 from pub.dev to your flutter project
  2. Create your app on Facebook developers and configure your flutter app accordingly
  3. Change your iOS target version to 9 in Xcode
  4. Open in terminal the Podfile.lock which is located in the iOS folder of your flutter project and change all facebook api versions to 5.8.0. If the Podfile.lock does not exist, run your app once (so the file is created) and after it fires an error edit Podfile.lock then run it again. If your app still fails, run pod install manually form terminal.

There are two problems:

  1. the compatibility between Facebook Login API and Firebase.
  2. iOS update to version 13.x.

In pubspec.yaml file updated the firebase version to latest version. Also, I am using version 1.2.0 for facebook login but still 1.1.1 works.

firebase_auth: ^0.14.0+5
flutter_facebook_login: ^1.2.0
flutter_auth_buttons: ^0.3.1

I opened Runner.xcworkspace in Xcode (located in iOS folder inside flutter project), then File->WorkSpace settings, and selected New Build System option.

For all pods, change iOS deployment version to 8.0 (or above).

Now the application builds successfully and runs on iOS. But I get CancelledByUser each time I click on the Login with Facebook button.

This is a bug when having iOS update 13.x running the facebook login api. I found a turnaround that made the login works.

final facebookLogin = new FacebookLogin();
facebookLogin.loginBehavior = FacebookLoginBehavior.webViewOnly;
Share:
6,168
EddyG
Author by

EddyG

Software developer interested mainly in web and mobile development and databases.

Updated on December 15, 2022

Comments

  • EddyG
    EddyG over 1 year

    I wrote a flutter login with facebook app.

    It works correctly on Android. On iOS simulator and on real iPhone device it does not work. Both having iOS 13.2.

    pub spec.yaml file

      firebase_auth: ^0.6.6
      flutter_facebook_login: ^1.1.1
      flutter_auth_buttons: ^0.3.1
    

    main.dart file

    import 'package:flutter/material.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:flutter_facebook_login/flutter_facebook_login.dart';
    import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return _MyAppState();
      }
    }
    
    class _MyAppState extends State<MyApp> {
      FirebaseAuth _auth = FirebaseAuth.instance;
      bool isLogged = false;
      FirebaseUser myUser;
    
      Future<FirebaseUser> _loginWithFacebook() async {
        var facebookLogin = new FacebookLogin();
        var result = await facebookLogin.logInWithReadPermissions(['email']);
        debugPrint(result.status.toString());
        if (result.status == FacebookLoginStatus.loggedIn) {
          FirebaseUser user =
              await _auth.signInWithFacebook(accessToken: result.accessToken.token);
          return user;
        }
        return null;
      }
    
      void _login() {
        _loginWithFacebook().then((response) {
          if (response != null) {
            myUser = response;
            setState(() {
              isLogged = true;
            });
          }
        });
      }
    
      void _logout() async {
        await _auth.signOut().then((response) {
          setState(() {
            isLogged = false;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Login App',
          home: Scaffold(
            appBar: AppBar(
              title: Text(isLogged ? 'Profile Page' : 'Login App'),
              actions: <Widget>[
                IconButton(
                  onPressed: _logout,
                  icon: Icon(Icons.power_settings_new),
                )
              ],
            ),
            body: Center(
              child: isLogged
                  ? Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text('Name: ' + myUser.displayName),
                        Image.network(myUser.photoUrl),
                      ],
                    )
                  : FacebookSignInButton(
                      onPressed: _login,
                    ),
            ),
          ),
        );
      }
    }
    

    I added the plist file in the Xcode project correctly. And configured the iOS version of the Facebook developer correctly based on the following steps: https://developers.facebook.com/docs/facebook-login/ios

    I changed the project workspace settings in Xcode between Legacy build and new build system. I still get error.

    I tried to add the following to the pod file, and still get error

    pod 'FBSDKCoreKit', '~> 4.44'
    

    And even all proposed solutions in here How to fix build error with FBSDKLoginKit in Xcode

    The error that I get is

    No known class method for selector 'dictionary:setObject:forKey:'
    

    I also tried to change the Firebase version and the Facebook Flutter login version in pub spec file (between 1.1.1 and 1.2.0 because I do not want to use AndroidX) but still getting the error.

    Note that I updated Xcode to latest version, and error was not fixed.

    I changed the iOS deployment version in Xcode for all pods to version 8. And error not fixed!

  • Loïc Fonkam
    Loïc Fonkam about 4 years
    Since my app works both on android and IOS, I had to add code to check if running on IOS and execute facebookLogin.loginBehavior = FacebookLoginBehavior.webViewOnly; .
  • Sina Seirafi
    Sina Seirafi about 4 years
    I did the exact same thing, Apple rejects the app saying Facebook login doesn't work. Apparently, in release mode, it only shows a white page and doesn't work.
  • EddyG
    EddyG about 4 years
    @SinaSeirafi please inform us about the alternative solution if you find one.
  • user2570135
    user2570135 about 4 years
    I am getting the same issue as @SinaSeirafi. Please let me know if you find a solution..
  • EddyG
    EddyG about 4 years
    @user2570135 I found a solution, please read my updated answer
  • EddyG
    EddyG about 4 years
    @SinaSeirafi check my updated answer for a working solution