react-native-fbsdk / Facebook iOS SDK - 未定义不是对象(评估'LoginManager.logInWithReadPermissions')

Posted

技术标签:

【中文标题】react-native-fbsdk / Facebook iOS SDK - 未定义不是对象(评估\'LoginManager.logInWithReadPermissions\')【英文标题】:react-native-fbsdk / Facebook iOS SDK - undefined is not an object (evaluating 'LoginManager.logInWithReadPermissions')react-native-fbsdk / Facebook iOS SDK - 未定义不是对象(评估'LoginManager.logInWithReadPermissions') 【发布时间】:2018-11-23 10:27:56 【问题描述】:

我在尝试使用 react-native-fbsdk 时遇到错误。我认为 SDK 安装过程存在一些问题,但我不确定。

安装似乎没有错误,但在尝试使用 SDK 时出现此错误:

undefined is not an object (evaluating
'LoginManager.logInWithReadPermissions')

使用 SDK 的代码:

try 
  var result = await LoginManager.logInWithReadPermissions(["public_profile"]);
  if (result && result.isCancelled) 
    console.log("Login cancelled");
   else 
    console.log(
      "Login success with permissions: " +
      result.grantedPermissions.toString()
    );
  
 catch (e) 
  console.error(e.message);

安装说明:

npm install react-native-fbsdk
react-native link react-native-fbsdk
cd ios
pod init
// Add "pod 'FacebookSDK'" to pod file
pod install
// Configure Info.plist and AppDelegate.m

这里指定了前 2 个命令:https://github.com/facebook/react-native-fbsdk 剩下的流程在这里指定:https://developers.facebook.com/docs/ios/getting-started/#settings

有人知道可能出了什么问题吗?

谢谢

Environment:
- macOs High Sierra
- XCode (10.1)
- FacebookSDK (4.38.0)
- react (16.6.1)
- react-native (0.57.5)
- react-native-fbsdk (0.8.0)

还有一个未解决的问题来自似乎有同样问题的人,但他没有指定他的安装过程或软件包版本。 Facebook SDK react LoginManager.logInWithReadPermissions undefined is not an object

【问题讨论】:

youtube.com/watch?v=hXLca8mnTKA查看此视频可能会帮助您解决问题 【参考方案1】:

你必须运行 react-native link react-native-fbsdk,并检查你的原生项目(ios 或 android)是否链接成功,如果没有,则手动进行。 1. 转到您项目下的库文件,然后右键单击选择“将文件添加到“您的项目”。并选择 react-native-fbsdk =>ios => RCTFBSDK.xcodepro。 2. 将 libRCTFBSDK.a 添加到您的“Linked Franmework and Libraray”中。

它应该可以工作,否则检查你的 SDK 版本,因为最低版本已经改变了一些东西。

【讨论】:

【参考方案2】:

这是我的安装过程。这是工作。

1。安装 react-native-fbsdk

react-native install react-native-fbsdk

2。安装 SDK

将以下行添加到ios/PodFile 并执行pod install

pod 'FBSDKLoginKit'
pod 'FBSDKShareKit'

如果没有 FBSDKShareKit 将构建失败:致命错误:找不到“FBSDKShareKit/FBSDKShareKit.h”文件

3。设置 Info.plist

在 ios/Project Name/Info.plist 中添加以下几行(根据https://developers.facebook.com/docs/ios/getting-started/)

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key> 
        <array> 
            <string>fb369704160457623</string> 
        </array> 
    </dict>
</array> 
<key>FacebookAppID</key> 
<string>Your facebook app id</string>
<key>FacebookDisplayName</key> 
<string>Your app name</string>
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>fbapi</string>
  <string>fb-messenger-share-api</string>
  <string>fbauth2</string>
  <string>fbshareextension</string>
</array>

将 FacebookAppID、FacebookDisplayName 键值替换为您实际使用的值。

4。修改AppDelegate.m

根据https://developers.facebook.com/docs/ios/getting-started/.

//  AppDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h> // Add this line

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

  [[FBSDKApplicationDelegate sharedInstance] application:application
    didFinishLaunchingWithOptions:launchOptions]; // Add this line
  // Add any custom logic here.
  return YES;


// Add following lines
- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url 
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options 

  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
    openURL:url
    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
  ];
  // Add any custom logic here.
  return handled;

5。修改App.js

    在标题处导入 LoginManager。 添加 login() 函数以调用 LoginManger.logInWithReadPermissions()。 在 render() 处添加组件。

App.js 如下

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React,  Component  from 'react';
import  Platform, StyleSheet, Text, View, TouchableOpacity  from 'react-native';
import  LoginManager  from "react-native-fbsdk";

const instructions = Platform.select(
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
);

type Props = ;
export default class App extends Component<Props> 
  login() 
    LoginManager.logInWithReadPermissions(["public_profile"]).then(
      function (result) 
        if (result.isCancelled) 
          console.log("Login cancelled");
         else 
          console.log(
            "Login success with permissions: " +
            result.grantedPermissions.toString()
          );
        
      ,
      function (error) 
        console.log("Login fail with error: " + error);
      
    );
  
  render() 
    return (
      <View style=styles.container>
        <Text style=styles.welcome>Welcome to React Native!</Text>
        <Text style=styles.instructions>To get started, edit App.js</Text>
        <Text style=styles.instructions>instructions</Text>
        <TouchableOpacity onPress=this.login.bind(this)>
          <Text>FBLogin</Text>
        </TouchableOpacity>
      </View>
    );
  


const styles = StyleSheet.create(
  container: 
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  ,
  welcome: 
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  ,
  instructions: 
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  ,
);

6。运行

react-native run-ios

7.我的环境:

  React Native Environment Info:
    Binaries:
      Node: 11.2.0 - /usr/local/bin/node
      Yarn: 1.12.3 - /usr/local/bin/yarn
      npm: 6.4.1 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23, 24, 25, 26, 27, 28
        Build Tools: 23.0.1, 23.0.3, 24.0.1, 25.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.1, 27.0.3, 28.0.2, 28.0.3
        System Images: android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom
    IDEs:
      Android Studio: 3.2 AI-181.5540.7.32.5056338
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.6.1 => 16.6.1
      react-native: 0.57.7 => 0.57.7
    npmGlobalPackages:
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7
      react-native-rename: 2.3.2

【讨论】:

以上是关于react-native-fbsdk / Facebook iOS SDK - 未定义不是对象(评估'LoginManager.logInWithReadPermissions')的主要内容,如果未能解决你的问题,请参考以下文章

使用 react-native-fbsdk 注销的正确方法

让 react-native-fbsdk 与 CocoaPods 一起工作

react-native-fbsdk 不支持redirect_uri URL

react-native-fbsdk:“ld:找不到-l-lRNSDKCoreKit的库”

未找到 react-native-fbsdk FBSDKCoreKit 和 FBSDKShareKit

react-native-fbsdk 发布消息 facebook Graph API FBGraphRequest