2013-04-28 111 views
0

我对通常的FB SDK和iOS开发颇为陌生。我试图将FBGraphUser对象传递给应用程序委托,然后在另一个视图中使用它。我的应用程序正确连接到FB,然后在logginview中将FBGraphUser对象分配给appDelegate。现在的问题是,当我去看另一个视图时,它再次检索它。如何将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); 
} 

SOLUTION:

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

我发现在其周围不再使用的应用程序委托retriving的fbgraph用户和使用我的所有其他视图这种方法的另一个工作。这将在彼此视图设置FB用户图表包括[自fbConnect]在viewDidLoad中

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

      } 
     }]; 
    } 
} 
+0

有更多的问题与你的方法(appDelegate持有数据...)但首先:是否有可能你的viewDidLoad在用户登录到FB之前被调用 - 因此你的NSLog写入null? – 2013-04-28 19:12:32

+0

只有在用户登录时才会调用createExercisePopupViewController.h中的viewDidLoad,并且在故事板的另一个视图中。我的问题有哪些问题? – 2013-04-28 19:17:50

回答

2

我与一个单独的类我写做这种....

// HEADER

// 
// 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 IN的应用程序委托.M ADD

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