我对fb mobile api有一个奇怪的问题。我正在使用最新的git版本。
在我的应用中,用户可以在应用启动时登录Facebook。 如果用户没有有效会话(例如,未登录,使用用户默认值中加载的FBCredentials进行检查),则会调用facebook授权代码。用户登录,然后返回我的应用程序(新令牌+用户默认保存的时间)。在那之后,我打电话给一个简单/我的请求。我从facebook上获取数据,一切都很好。
但是现在接下来是:用户关闭应用程序(仍然登录)。用户重新打开应用程序。我的应用程序检查用户是否具有有效会话(再次使用用户默认值中加载的FBCredentials进行检查),并返回YES(这就是我所期望的)。但是这一次,登录/启动应用程序后立即调用api会返回任何内容!所有进一步的api调用都没有返回。
如果用户在应用程序中注销([facebook logout:self];)然后再次登录,则api调用正在运行。因此,我假设我的代码无法在应用启动时使用加载凭据。
继承我的控制器,负责fb连接。
//
// FacebookController.h
//
#import <Foundation/Foundation.h>
#import "FBConnect.h"
@interface FacebookController : NSObject <FBRequestDelegate, FBSessionDelegate> {
Facebook *facebook;
NSArray *permissions;
}
@property(nonatomic, retain) Facebook *facebook;
+ (FacebookController *)instance;
- (void)loginToFacebook;
- (void)loggedIn;
- (void)logoutOfFacebook;
@end
实施:
//
// FacebookController.m
//
#import "FacebookController.h"
#import "MyAppAppDelegate.h"
static NSString* FBAppId = @"...";
@interface FacebookController()
- (void)loadFBCredentials;
- (void)saveFBCredentials;
@end
@implementation FacebookController
@synthesize facebook;
+ (FacebookController *)instance {
static FacebookController *instance = nil;
if (!instance) {
instance = [[FacebookController alloc] init];
}
return instance;
}
- (id)init {
self = [super init];
if (!facebook) {
facebook = [[Facebook alloc] initWithAppId:FBAppId];
[self loadFBCredentials];
}
return self;
}
// Show the authorization dialog.
- (void)loginToFacebook {
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
} else {
[self loggedIn];
}
}
// Show the authorization dialog.
- (void)logoutOfFacebook {
[facebook logout:self];
}
- (void)loggedIn {
// Api call.
[facebook requestWithGraphPath:@"/me" andDelegate:self];
}
// Called when the user has logged in successfully.
- (void)fbDidLogin {
// Save Credentials.
[self saveFBCredentials];
[self loggedIn];
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
}
- (void)request:(FBRequest *)request didLoad:(id)result {
NSLog(@"Got request");
if ([result isKindOfClass:[NSDictionary class]]) {
NSLog(@"count : %d ", [result count]);
NSArray* resultArray = [result allObjects];
NSLog(@"count : %d ", [result count]);
result = [resultArray objectAtIndex:0];
}
}
// Called when the request logout has succeeded.
- (void)fbDidLogout {
[self saveFBCredentials];
[(MyAppAppDelegate *)[UIApplication sharedApplication].delegate showModalLoginView:YES];
}
- (void)loadFBCredentials {
NSLog(@"Loading.");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = (NSDate *) [defaults objectForKey:@"FBExpirationDateKey"];
}
}
- (void)saveFBCredentials {
NSLog(@"Saving.");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)dealloc {
[facebook release];
[super dealloc];
}
@end
答案 0 :(得分:0)
请求offline_access权限并使用生成的访问令牌密钥以避免会话无效的情况。