我有一個奇怪的問題與fb手機api。我使用最新的Git版本。Facebook的iOS Api呼叫失敗重新登錄,但isSessionValid是YES
在我的應用程序中,用戶可以在應用程序啓動時登錄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
這實際上不是我所搜索的,但現在,這幫助我。謝謝! –