0

一般来说,我对 FB SDK 和 iOS 开发很陌生。我正在尝试将 FBGraphUser 对象传递给应用程序委托,然后稍后在另一个视图中使用它。我的应用程序正确连接到 FB,然后在 loginview 中将 FBGraphUser 对象分配给 appDelegate。现在的问题是,当我转到另一个视图时,它不再检索它。

有谁知道为什么不工作?

我在 loginViewController.h 中的代码是

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    // here we use helper properties of FBGraphUser to dot-through to first_name and
    // id properties of the json response from the server; alternatively we could use
    // NSDictionary methods such as objectForKey to get values from the my json object
;
    // setting the profileID property of the FBProfilePictureView instance
    // causes the control to fetch and display the profile picture for the user
    _loggedInUser = user;

        [self saveFBuserToDB];
    _appDelegate.loggedInUser = (id<FBGraphUser>) user;

   // NSLog(@"%@",_appDelegate.loggedInUser);
}

直到这里它就像一个魅力。它连接到 FB 并在我执行 NSLOG 时从应用程序委托中检索对象......

现在......当我进入另一个视图时,用户不再设置

createExercisePopupViewController.h

#import <UIKit/UIKit.h>
#import "myGymAppDelegate.h"
@interface createExercisePopupViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSArray* routineName;
}
@property (retain,nonatomic) NSArray* routineName;
@property (strong,nonatomic) myGymAppDelegate *appDelegate;

和 createExercisePopupViewController.h

#import "createExercisePopupViewController.h"

@interface createExercisePopupViewController ()
{
    NSManagedObjectContext *context;
}
@end
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _appDelegate=(myGymAppDelegate*)[[UIApplication sharedApplication] delegate];

    NSLog(@"%@",_appDelegate.loggedInUser);
}

解决方案:

虽然@Borinschi Ivan 给出的答案是有效的

我发现了另一个解决方法,它不再使用应用程序委托来检索 fbgraph 用户并在我的所有其他视图中使用此方法。这将在彼此的视图中设置 fb 用户图在 viewdidload 中包含 [self fbConnect]

- (void) fbConnect {
    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error) {
             if (!error) {
                 _loggedInUser = user;

             }
         }];
    }
}
4

1 回答 1

2

我已经用我写的单例类做到了这一点......

//标题

//
//  imoFacebookSingletone.h
//  PeopleInc
//
//  Created by Borinschi Ivan on 2/17/13.
//  Copyright (c) 2013 Borinschi Ivan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <FacebookSDK/FacebookSDK.h>
#import "AppDelegate.h"

@interface imoFacebookSingletone : NSObject
+ (imoFacebookSingletone*)facebookUser;
@property (nonatomic,retain) NSString *userFullName;
@property (nonatomic,retain) NSString *userFirstName;
@property (nonatomic,retain) NSString *userLastName;
@property (nonatomic,retain) NSString *userId;
@property (nonatomic,assign) BOOL userLoged;
- (void)logIn;
- (void)logOut;
@end

//执行

//
//  imoFacebookSingletone.m
//  PeopleInc
//
//  Created by Borinschi Ivan on 2/17/13.
//  Copyright (c) 2013 Borinschi Ivan. All rights reserved.
//

#import "imoFacebookSingletone.h"
#import <FacebookSDK/FacebookSDK.h>

@implementation imoFacebookSingletone

+ (imoFacebookSingletone*)facebookUser
{
    static imoFacebookSingletone *facebookUser = nil;
    if (!facebookUser) {  facebookUser = [[super allocWithZone:nil] init]; }
    return facebookUser;
}

+(id)allocWithZone:(NSZone *)zone { return [self facebookUser]; }

- (id)init {
    self = [super init];
    if (self) {  self.userLoged = NO; }
    return self;
}

- (void)logOut {
    [FBSession.activeSession closeAndClearTokenInformation];
}

-(void)logIn
{
    if (!self.userLoged) {
        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler: ^(FBSession *session,  FBSessionState state, NSError *error) {
            [self sessionStateChanged:session state:state error:error];
        }];
    }
    else { [[NSNotificationCenter defaultCenter] postNotificationName:@"populateUserDetails" object:self]; }
}


- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error {
    switch (state) {

        case FBSessionStateOpen: { [self populateUserDetails]; }
            break;
        case FBSessionStateClosed: { [self cleanUsser]; }
             break;
        case FBSessionStateClosedLoginFailed:
        {
            [FBSession.activeSession closeAndClearTokenInformation];
            [self cleanUsser];
        }

            break;
        default: { }
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}


- (void)populateUserDetails {
    if (FBSession.activeSession.isOpen) { [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
        if (!error)
        {
                 self.userFirstName = user.first_name;
                 self.userLastName = user.last_name;
                 self.userFullName = user.name;
                 self.userId = user.id;
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"populateUserDetails" object:self];
        }
         }];
    }
}


- (void)cleanUsser
{
    self.userFirstName = @"";
    self.userLastName = @"";
    self.userFullName = @"";
    self.userId = @"";
    self.userLoged = NO;
}

@end

//示例使用

1 在应用程序代表 .M 添加

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [FBSession.activeSession handleOpenURL:url];
}

2 然后在您的视图控制器.h

#import <UIKit/UIKit.h>
#import "imoFacebookSingletone.h"

@interface ViewController : UIViewController<FBLoginViewDelegate>
{

}

@property (strong, nonatomic) FBRequest *pendingRequest;
@property (strong, nonatomic) imoFacebookSingletone *facebook;

@end

3 然后是您的视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{


    self.facebook = [[imoFacebookSingletone alloc] init];

    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
    {
        [self.facebook logIn];
    }
    else
    {

    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateUserDetails:) name:@"populateUserDetails" object:nil];
}

- (void)populateUserDetails:(NSNotification *)notification
{
    userName.text = self.facebook.userFullName;
    userPicture.profileID = self.facebook.userId;
}
于 2013-04-28T19:21:22.747 回答