让ReactNative FacebookSDK正常工作

时间:2016-04-28 02:33:04

标签: react-native fbsdk react-native-fbsdk

我试图在iOS ReactNative项目中运行FBSDK。

react-native init AwesomeProject要获得一个全新的项目,请关注github上的React Native FBSDK instructions,我在main.m中收到错误:

thread 1:signal SIGABRT

一个小小的谷歌搜索引导我here然后here,我向LSApplicationQueriesSchemes添加info.plist密钥。解决这个问题。

然后我按照Facebook app setup guide跟我添加了NSAppTransportSecurity我的info.plist`的密钥。但是,应用程序无法连接到开发服务器。

更多谷歌搜索,我发现this page表示我不需要NSAppTransportSecurity密钥,因此我将其取出并运行应用程序。哇,问题解决了。

回到React Native FBSDK github page,我抓住了他们使用部分的第一个例子; LoginButton。将其逐字复制到我的应用程序中。它运行。我点击它。和...

thread 1:signal SIGABRT

AAAAH!

有没有人让这个工作?

1 个答案:

答案 0 :(得分:2)

我有!安装SDK后,您需要确保所有配置都是属性设置。您还需要在AppDelegate中导入sdk。

以下是我的info.plist中的相关配置。

<key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>fb12345678910</string>
      </array>
    </dict>
  </array>
  <key>FacebookAppID</key>
  <string>12345678910</string>
  <key>FacebookDisplayName</key>
  <string>My Awesome App</string>


  <key>NSAppTransportSecurity</key>
  <dict>
    <key>NSExceptionDomains</key>
    <dict>
      <key>facebook.com</key>
      <dict>
        <key>NSIncludesSubdomains</key> <true/>
        <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
      </dict>
      <key>fbcdn.net</key>
      <dict>
        <key>NSIncludesSubdomains</key> <true/>
        <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>  <false/>
      </dict>
      <key>akamaihd.net</key>
      <dict>
        <key>NSIncludesSubdomains</key> <true/>
        <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
      </dict>
      <key>localhost</key>
      <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/>
      </dict>
      <key>api.mydomain.com</key>
      <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/>
      </dict>
    </dict>
  </dict>


  <key>LSApplicationQueriesSchemes</key>
  <array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
  </array>

有三个部分:

  1. 您需要定义您的应用ID和显示名称。
  2. 您需要定义您的应用可以访问的域名,显然是facebook的域名,akamai和您自己的域名,我已将localhost添加到列表中。
  3. 最后,您需要包含查询方案
  4. 这是我的AppDelegate.m文件。

    ```     #import“AppDelegate.h”

    #import "RCTRootView.h"
    
    #import <FBSDKCoreKit/FBSDKCoreKit.h>
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      NSURL *jsCodeLocation;
    
      /**
       * Facebook SDK
       *
       **/
      [[FBSDKApplicationDelegate sharedInstance] application:application
                               didFinishLaunchingWithOptions:launchOptions];
    
      /**
       * Loading JavaScript code - uncomment the one you want.
       *
       * OPTION 1
       * Load from development server. Start the server from the repository root:
       *
       * $ npm start
       *
       * To run on device, change `localhost` to the IP address of your computer
       * (you can get this by typing `ifconfig` into the terminal and selecting the
       * `inet` value under `en0:`) and make sure your computer and iOS device are
       * on the same Wi-Fi network.
       */
    
      jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
    
      /**
       * OPTION 2
       * Load from pre-bundled file on disk. The static bundle is automatically
       * generated by the "Bundle React Native code and images" build step when
       * running the project on an actual device or running the project on the
       * simulator in the "Release" build configuration.
       */
    
    //   jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
    
      RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                          moduleName:@"MyAwesomeApp"
                                                   initialProperties:nil
                                                       launchOptions:launchOptions];
    
      self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
      UIViewController *rootViewController = [UIViewController new];
      rootViewController.view = rootView;
      self.window.rootViewController = rootViewController;
      [self.window makeKeyAndVisible];
      return YES;
    }
    
    /**
     * Facebook SDK
     *
     **/
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
      [FBSDKAppEvents activateApp];
    }
    
    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
      return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                             openURL:url
                                                   sourceApplication:sourceApplication
                                                          annotation:annotation];
    }
    
    @end
    

    通过这些配置,我可以连接我的服务器,登录用户等

    祝你好运!