在viewsController之间传递facebook信息

Posted

技术标签:

【中文标题】在viewsController之间传递facebook信息【英文标题】:passing facebook info between viewsController 【发布时间】:2014-03-11 11:31:46 【问题描述】:

我一直在网上查看我是否需要将代表放在 profileViewController 中(这是信息将要显示的视图)或者我需要在登录/注册视图控制器中的代表我不知道如何通过信息简介图片、姓名、登录状态请帮忙,

编辑::

我能够通过我的协议传递 user.name,但我无法传递 FBProfilePictureView

ViewController.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>


@protocol passNames <NSObject>

- (void)setFBName: (NSString *)FBName;
- (void)setFBProfilePicture: (id<FBGraphUser>)FBPicture;

@end

@interface ViewController : UIViewController <FBLoginViewDelegate>


@property (weak) id <passNames> delegate;
@property (strong, nonatomic) NSString *FBNamePass;
@property (strong, nonatomic) FBProfilePictureView *FBProfilePicture;

@end

ViewController.m

#import "ViewController.h"
#import "NeXtViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePictureView;


@end

@implementation ViewController

@synthesize delegate, FBNamePass, FBProfilePicture;

- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.



// Create a FBLoginView to log the user in with basic, email and likes permissions
// you should always ask for basic permissions when loggin the user in
FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions:@[@"basic_info",@"email",@"user_likes"]];

// set this loginUIViewCOntroller to be the loginView button's delegate
loginView.delegate = self;


// align the button in the center horizontally
loginView.frame = CGRectMake(25, 299, 271, 50);

// add the button to the view
[self.view addSubview:loginView];

//[self pushViewController];




-(void)viewDidAppear:(BOOL)animated




- (void)didReceiveMemoryWarning

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


// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user

self.nameLabel.text = user.name;
//self.profilePictureView.profileID = user.id;


//    FBProfilePicture.profileID = user.id;
//    NSLog(@"%@ FB", FBProfilePicture.class);
//    FBNamePass = user.name;
//    NSLog(@"%@ user", FBNamePass);
[self pushViewController:user.name andProfilePicture:user];



- (BOOL)isUser:(id<FBGraphUser>)firstUser equalToUser:(id<FBGraphUser>)secondUser 
return
[firstUser.id isEqual:secondUser.id] &&
[firstUser.name isEqual:secondUser.name] &&
[firstUser.first_name isEqual:secondUser.first_name] &&
[firstUser.middle_name isEqual:secondUser.middle_name] &&
[firstUser.last_name isEqual:secondUser.last_name];




// implement the loginViewShowingLoggedInUser: delegate method to modify your app's UI for a logged-in user experoience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView

self.statusLabel.text = @"Logged in";

if ([self.statusLabel.text isEqualToString:@"Logged in"]) 
    //NeXtViewController *n = [self.storyboard instantiateViewControllerWithIdentifier:@"NeXt"];
    //[self.navigationController pushViewController:n animated:NO];




// implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experoience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView

self.profilePictureView.profileID = nil;
self.nameLabel.text = @"";
self.statusLabel.text = @"You're not logged in";


// You need to override loginView:handleError in order to handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error

NSString *alertMessage, *alertTitle;

// If the user should perform an action outside of you app to recover,
// the SDK will provide a message for the user, you just need to surface it.
// This conveniently handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) 
    alertMessage = [FBErrorUtility userMessageForError:error];
    alertTitle = @"Facebook Error";

    // This code will handle session closures since that happen outside of the app.
    // You can take a look at our error handling guide to know more about it
    // https://developers.facebook.com/docs/ios/errors
 else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) 
    alertTitle = @"Session Error";
    alertMessage = @"Your current session is no longer valid. Please log in again.";

    // If the user has cancelled a login, we will do nothing.
    // You can also choose to show the user a message if cancelling login will result in
    // the user not being able to complete a task they had initiated in your app
    // (like accessing FB-stored information or posting to Facebook)
 else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) 
    NSLog(@"user cancelled login");

    // For simplicity, this sample handles other errors with a generic message
    // You can checkout our error handling guide for more detailed information
    // https://developers.facebook.com/docs/ios/errors
 else 
    alertTitle = @"Something went wrong";
    alertMessage = @"Please try again later";
    NSLog(@"Unexpected error:%@",error);


if (alertMessage) 
    [[[UIAlertView alloc] initWithTitle:alertTitle
                                message:alertMessage
                               delegate:nil
                      cancelButtonTitle:@"ok"
                      otherButtonTitles:nil] show];







- (void)pushViewController:(NSString *)user andProfilePicture:(id<FBGraphUser>)profilePicture

NeXtViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"NeXt"];
[controller setFBNameString:user];
//controller.profilePicture = profilePicture;
[controller setFBProfilePicture:profilePicture];
NSLog(@"%@",profilePicture);
[self.navigationController pushViewController:controller animated:NO];


@end

NeXtViewController.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "ViewController.h"

@interface NeXtViewController : UIViewController <passNames>

ViewController *view;



@property (strong,nonatomic) NSString *FBNameString;
@property (strong, nonatomic) FBProfilePictureView *profilePicture;

@property (weak, nonatomic) IBOutlet FBProfilePictureView *FBProfilePictureView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;


- (IBAction)FBLogOut:(id)sender;

@end

NeXtViewController.m

#import "NeXtViewController.h"

@interface NeXtViewController ()

@end

@implementation NeXtViewController

@synthesize FBNameString = _FBNameString, profilePicture = _profilePicture;

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

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

return self;


- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view.

view = [[ViewController alloc] init];
[view setDelegate:self];
self.nameLabel.text = _FBNameString;
//self.FBProfilePictureView.profileID = _profilePicture.profileID;
NSLog(@"%@ %@", self.FBProfilePictureView.class, self.FBNameString.class);

self.navigationItem.hidesBackButton = YES;


-(void)viewDidAppear:(BOOL)animated




- (void)didReceiveMemoryWarning

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



- (void)setFBName:(NSString *)FBName

_FBNameString = FBName;


- (void)setFBProfilePicture:(id<FBGraphUser>)FBPicture

self.FBProfilePictureView.profileID = FBPicture.id;


- (IBAction)FBLogOut:(id)sender

[FBSession.activeSession closeAndClearTokenInformation];
if (FBSessionDidBecomeClosedActiveSessionNotification) 
    ViewController *views = [self.storyboard instantiateViewControllerWithIdentifier:@"View"];
    [self.navigationController pushViewController:views animated:NO];
    views.navigationItem.hidesBackButton = YES;




@end

【问题讨论】:

贴一些代码,它会帮助我们给出确切的想法。 您成功获取信息了吗?并放一些代码人。 我打印了项目中的所有内容 【参考方案1】:

在我看来,您不需要代表(根据我从您的问题中收集到的信息)。您可以做的是从您的“登录/注册视图控制器”中获取对“profileViewController”的引用,然后将一些信息传递给他们。让我们假设您正在使用情节提要和转场。在您的 profileViewController.h 中,您将创建一些像这样的属性:

@interface profileViewController

@property(nonatomic) UIImage* profilePicture;
@property(nonatomic) NSString* name;
@property(nonatomic) NSString* loginStatus;

//all your methods and properties...

@end

然后在您的登录/注册视图控制器中,找到一种方法来找到 profileViewController:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    // Get the profile view controller from segue and cast it
    profileViewController* profileVC = (profileViewController*)[segue destinationViewController];

    // Set the properties in the profile VC Class
    [profileVC setProfilePicture:myImage];
    [profileVC setName:myName];
    [profileVC setLoginStatus:myLoginStatus];

然后每当在 profileViewController 中调用 'viewDidLoad' 或 'init' 时,都应该设置属性并准备好使用!它不必是您正在使用的 segue,只要在使用之前访问 profileViewController 的任何方式即可。

【讨论】:

我没有使用 segues 虽然是的我使用 UINavigationController 但是两个 viewControllers 没有通过 segue 连接并且 UIImage 是 FBProfilePictureView 而用于为 FBProfilePictureView 赋予价值的 user.id 是一个字符串【参考方案2】:

尝试将您的数据设置为 NSUserDefault 它易于使用,并且使用 NSUserDefault 在多个类之间共享数据很简单。

AppDelegate.m

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

保存

 AppDelegate * degate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    delegate.prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[delegate.prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[delegate.prefs setInteger:42 forKey:@"integerKey"];

[delegate.prefs synchronize];

检索

delegate.prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [delegate.prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [delegate.prefs integerForKey:@"integerKey"];

并在您的 AppDelegate 中定义您的 NSUSserDefault 并使用 AppDelageate 对象以及您在多个类之间的任何数据进行共享。

试过这个

【讨论】:

以上是关于在viewsController之间传递facebook信息的主要内容,如果未能解决你的问题,请参考以下文章

Android如何在Activity和Service之间传递数据

如何在两页面之间传递JSON数据

在DELPHI中窗体之间 如何传递数据?

快速展开函数 3

在Jsp中页面与页面之间传递参数有几种方法?

按引用传递或按值传递指针容器之间的区别