作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
总的来说,我对 FB SDK 和 iOS 开发还很陌生。我正在尝试将 FBGraphUser 对象传递给应用程序委托(delegate),然后稍后在另一个 View 中使用它。我的应用程序正确连接到 FB,然后在 logginview 中将 FBGraphUser 对象分配给 appDelegate。现在的问题是,当我转到另一个 View 时,它会再检索它。
有谁知道为什么不工作?
我在 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 时从应用程序委托(delegate)中检索对象....
现在...当我进入另一个 View 时,用户不再设置
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 给出的答案是有效的
我发现了另一个解决方法,它不再使用应用程序委托(delegate)来检索 fbgraph 用户并在我的所有其他 View 中使用此方法。这将在彼此 View 中设置 fb 用户图包括 [self fbConnect] in viewdidload
- (void) fbConnect {
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
_loggedInUser = user;
}
}];
}
}
最佳答案
我已经用我写的单例类完成了这个......
//标题
//
// 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
//示例使用
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
#import <UIKit/UIKit.h>
#import "imoFacebookSingletone.h"
@interface ViewController : UIViewController<FBLoginViewDelegate>
{
}
@property (strong, nonatomic) FBRequest *pendingRequest;
@property (strong, nonatomic) imoFacebookSingletone *facebook;
@end
#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;
}
关于iphone - 如何将 FBGraphUser 对象传递给 appDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266405/
我是一名优秀的程序员,十分优秀!