将 Parse 与 FB 集成连接起来

Posted

技术标签:

【中文标题】将 Parse 与 FB 集成连接起来【英文标题】:Connecting Parse with FB integration 【发布时间】:2014-07-02 18:37:20 【问题描述】:

我正在创建一个聊天应用程序,用户首先被带到主菜单,在那里他们可以选择“聊天”,然后被定向到 FB 登录功能,在用户使用 FB 登录后,他们应该被重定向到标签栏控制器。但是我无法弄清楚如何设置程序化segue,以便在成功登录后将用户直接带到该屏幕。

我不知道这是否是 Parse 不收集用户数据的原因,但我在运行应用程序时收到此警告:[10799:60b] 警告:正在执行长时间运行的 Parse 操作主线程。中断 warnParseOperationOnMainThread() 进行调试。我的问题是如何为我的选项卡栏控制器创建该 segue,我是否缺少 Parse 的某些内容?

这是我的 AppDelegate.m 文件:

#import "CCAppDelegate.h"
#import <Parse/Parse.h>

@implementation CCAppDelegate

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

    // Override point for customization after application launch.

    [Parse setApplicationId:@"XXXX"
                  clientKey:@"XXXX"];
    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    [PFFacebookUtils initializeFacebook];

    [FBLoginView class];
    [FBProfilePictureView class];

    return YES;


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

    BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
    return wasHandled;



- (void)applicationWillResignActive:(UIApplication *)application

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.


- (void)applicationDidEnterBackground:(UIApplication *)application

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


- (void)applicationWillEnterForeground:(UIApplication *)application

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.


- (void)applicationDidBecomeActive:(UIApplication *)application

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.


- (void)applicationWillTerminate:(UIApplication *)application

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.


@end

这是我的 CCLoginViewController.m 文件:

#import "CCLoginViewController.h"

@interface CCLoginViewController ()

@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@end

@implementation CCLoginViewController

-(void)toggleHiddenState:(BOOL)shouldHide

    self.lblUsername.hidden = shouldHide;
    self.lblEmail.hidden = shouldHide;
    self.profilePictureView.hidden = shouldHide;


-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
    self.lblLoginStatus.text = @"You are logged in.";

    [self toggleHiddenState:NO];

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
    NSLog(@"%@", user);
    self.profilePictureView.profileID = user.objectID;
    self.lblUsername.text = user.name;
    self.lblEmail.text = [user objectForKey:@"email"];

-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
    self.lblLoginStatus.text = @"You are logged out";

    [self toggleHiddenState:YES];

-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
    NSLog(@"%@", [error localizedDescription]);


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    self.loginView.delegate = self;

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self toggleHiddenState:YES];
    self.lblLoginStatus.text = @"";
    self.lblLoginStatus.text = @"";
    self.loginView.readPermissions = @[@"public_profile", @"email"];


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

*/

@end

【问题讨论】:

【参考方案1】:

根据ios, Facebook Users 上的文档,您应该调用FBAppCall 略有不同:

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation 
    return [FBAppCall handleOpenURL:url
                  sourceApplication:sourceApplication
                        withSession:[PFFacebookUtils session]];


- (void)applicationDidBecomeActive:(UIApplication *)application 
    [FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];

您缺少的关键部分是withSession:[PFFacebookUtils session],我猜它可以让 Parse 知道 Facebook 正在做什么。

我也没有在您的代码中看到任何 Parse 查询,因此您的代码的某些其他部分必须触发该警告,在某处您正在执行查询而不使用 withBlock:,它可以处理结果没有锁定 UI 的后台线程。

【讨论】:

以上是关于将 Parse 与 FB 集成连接起来的主要内容,如果未能解决你的问题,请参考以下文章

phonegap facebook连接集成问题

FB 与 Three20 连接 - 登录 facebook 时出现问题

Facebook 登录问题 - iOS 应用程序的新用户无法通过 FB 连接登录

在 Swift 中使用 Parse 使用 FB 登录的用户持久性

将 Chargify 与 Firebase 连接

将简单的 Node.js 和 Socket.io 聊天应用程序与 Rails 应用程序连接起来(在 Heroku 上)