React-Native:Facebook 登录后返回应用程序?

Posted

技术标签:

【中文标题】React-Native:Facebook 登录后返回应用程序?【英文标题】:React-Native: Return to app after Facebook login? 【发布时间】:2016-05-28 14:36:40 【问题描述】:

我正在使用 react-native 构建应用程序。我已经使用本教程实现了 facebook 登录:https://developers.facebook.com/docs/react-native/login。

当在应用程序中按下“使用 Facebook 登录”按钮时,它会将我带到 facebook 网页并要求我授予应用程序使用我的一些个人信息的权限。我接受,但它永远不会让我回到应用程序。

有什么建议可以让我重定向回应用程序吗?

这是我正在使用的代码:

'use strict'

import React,  Component  from 'react'
import 
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  Navigator
 from 'react-native'

const FBSDK = require('react-native-fbsdk')
const 
  LoginButton
 = FBSDK

class Login extends Component
  render() 
    return (
      <View>
        <LoginButton
          publishPermissions=["publish_actions"]
          onLoginFinished=
            (error, result) => 
              if (error) 
                alert("Login failed with error: " + result.error)
               else if (result.isCancelled) 
                alert("Login was cancelled")
               else 
                alert("Login was successful with permissions: " + result.grantedPermissions)
              
            
          
          onLogoutFinished=() => alert("User logged out")/>
      </View>
    )
  


class App extends Component 
  render() 
    return (
      <View>
        <Text>Welcome to the Facebook SDK for React Native!</Text>
        <Login />
      </View>
    )
  


AppRegistry.registerComponent('App', () => App)

【问题讨论】:

【参考方案1】:

您是否已将 AppDelegate 类连接到 FBSDKApplicationDelegate 对象?如果不是,这可能就是问题所在。

查看Connect App Delegate部分here。

它基本上说要将此代码添加到 AppDelegate.m :

//  AppDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h>

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

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


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

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

我已经在另一个项目的 AppDelegate 类中实现了这个,here 是我添加新代码后的样子:

https://gist.github.com/dabit3/24ace6a1f6973ed8675fb37a211f3c4c

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import "RCTRootView.h"

#import <FBSDKCoreKit/FBSDKCoreKit.h>

@implementation AppDelegate

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

  NSURL *jsCodeLocation;

//  for (NSString* family in [UIFont familyNames])
//  
//    NSLog(@"%@", family);
//    for (NSString* name in [UIFont fontNamesForFamilyName: family])
//    
//      NSLog(@" %@", name);
//    
//  

  /**
   * 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:@"YOURAPPNAME"
                                               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 [[FBSDKApplicationDelegate sharedInstance] application:application
                                  didFinishLaunchingWithOptions:launchOptions];



- (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

【讨论】:

我也错过了教程中的 openURL 部分,将那部分添加到我的 AppDelegate 也对我有用。

以上是关于React-Native:Facebook 登录后返回应用程序?的主要内容,如果未能解决你的问题,请参考以下文章

Facebook在android的react-native应用程序中登录

FB SDK 错误:登录失败(react-native)

在 react-native 和 firebase 3.1 中登录 Facebook

facebook登录错误,react-native ios expo

React-Native Expo,Facebook 登录无法在 Android 设备上运行

React-native Log in with Facebook App 刷新应用