ios - Flutter 使用 Facebook 登录

标签 ios xcode facebook-graph-api flutter

我正在尝试使用 Facebook 登录我的用户。 因此,我使用这段代码:

 FacebookLoginResult facebookLoginResult = await _handleFBSignIn();
        final accessToken = facebookLoginResult.accessToken.token;
        if (facebookLoginResult.status == FacebookLoginStatus.loggedIn) {
          setState(() {
            isSaving = true;
          });

          final facebookAuthCred =
              FacebookAuthProvider.getCredential(accessToken: accessToken);
          final user =
              await firebaseAuth.signInWithCredential(facebookAuthCred);

          final graphResponse = await http.get(
              'https://graph.facebook.com/v4.0/me?fields=name,first_name,last_name,friends,email&access_token=$accessToken');
          final profile = jsonDecode(graphResponse.body);

          if (profile['id'] != null) {
            setState(() {
              facebookID = profile['id'];
            });
          }

  Future<FacebookLoginResult> _handleFBSignIn() async {
    FacebookLogin facebookLogin = FacebookLogin();
    FacebookLoginResult facebookLoginResult =
        await facebookLogin.logInWithReadPermissions(['email', 'user_friends']);
    switch (facebookLoginResult.status) {
      case FacebookLoginStatus.cancelledByUser:
        print("Cancelled");
        break;
      case FacebookLoginStatus.error:
        print("error");
        break;
      case FacebookLoginStatus.loggedIn:
        print("Logged In");
        break;
    }
    return facebookLoginResult;
  }

这在 Android 上完美运行,但在 ios 上我收到错误。

flutter: Cancelled
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'token' was called on null.
Receiver: null

所以我假设它与我的 info.plist 有关,因为 android 没有这个问题。我查看了 info.plist 但找不到任何内容...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>drinkm8</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(FLUTTER_BUILD_NAME)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>$(FLUTTER_BUILD_NUMBER)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>NSCameraUsageDescription</key>
    <string>Hiermee kunt u een verhaal maken</string>
    <key>NSContactsUsageDescription</key>
    <string>Your prompt</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Always/When in use description</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Can I haz location always?</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>This app needs access to location when open.</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>Hiermee kunt u een verhaal maken</string>
    <key>NSMotionUsageDescription</key>
    <string>Motion Usage Description</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Hiermee kunt u een foto selecteren</string>
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>Hiermee kunt u een foto opslaan</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>location</string>
        <string>remote-notification</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>FirebaseAppDelegateProxyEnabled</key>
    <string>NO</string>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <!-- TODO Replace this value: -->
                <!-- Copied from GoogleService-Info.plist key REVERSED_CLIENT_ID -->
                <string>com.googleusercontent.apps.myappid</string>
                <string>fb4856myappid</string>
            </array>
        </dict>
    </array>
    <key>FacebookAppID</key>
    <string>4856myappid</string>
    <key>FacebookDisplayName</key>
    <string>DRINKM8</string>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fb-messenger-share-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
    </array>
    <key>io.flutter.embedded_views_preview</key>
    <string>YES</string>
</dict>
</plist>

我是否错过了为 ios 安装 FB 的步骤(我只编辑了 info.plist 并将 fb 登录添加到 pubspec yaml)?

提前致谢!

最佳答案

我从 github 了解到这是包或 sdk 中的错误或......

但是对于 IOS,如果你使用这个就可以了:

final fbLogin =FacebookLogin();
fbLogin.loginBehavior = FacebookLoginBehavior.webViewOnly;

关于ios - Flutter 使用 Facebook 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59377718/

相关文章:

ios - 类型 'PlaySoundsViewController' 的值没有成员 'recordedAudio'

iphone - 有没有人创建一个垂直的 UIToolBar?或者某种带有 UIButtons 的垂直菜单?

android - 如何解析 Facebook 数据

ios - UICollectionView 在横向模式下对右侧 w-h 像素滑动无响应

iphone - 取 CGFloat 的绝对值

ios - TabBarItems 并设置它们的图像大小?

javascript - Facebook Javascript SDK FB 未定义

ios - 没有Facebook App不会显示FBWebDialogs presentFeedDialogModallyWithSession

ios - UIAlert 不适用于 NSNotification

ios - Swift 3,使用带有闭包的函数扩展 Objective-C 泛型类