2012-04-16 208 views
1

我正在開發一款iphone遊戲,我想讓用戶在Facebook上發佈他們的分數。我設法實現它,但每次我想要發佈Feed時,都會有一個Facebook窗口告訴我我已經登錄到了我的Facebook應用程序,並且用戶需要點擊接受才能顯示將feed發佈到的nex窗口輪廓的牆。iPhone - Facebook連接

我需要跳過Facebook登錄嗎?我的意思是Facebook授權方法,並直接跳入Facebook的飼料方法?

這是我的,但會話總是無效的! fbDidLogin不會被調用,並在調試它給MES這樣那樣的錯誤:

void SendDelegateMessage(NSInvocation*): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode 

如果我註銷,然後嘗試喂,我必須先登錄2次,一個用於會話,一個用於飼料。

這是代碼:

#import "FacebookConnect.h" 

static NSString* kAppId = @"414427041919461"; // Your Facebook app ID here 

@implementation FacebookConnect 

@synthesize facebook = _facebook; 

#pragma mark - 
#pragma mark Singleton Variables 
static FacebookConnect *singletonDelegate = nil; 

#pragma mark - 
#pragma mark Singleton stuff 
- (id)init { 
    if (!kAppId) { 
     NSLog(@"MISSING APP ID!!!"); 
     exit(1); 
     return nil; 
    } 

    if ((self = [super init])) { 

     _facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self]; 
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     if ([defaults objectForKey:@"FBAccessTokenKey"] 
      && [defaults objectForKey:@"FBExpirationDateKey"]) { 
      _facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
      _facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
     } 

    } 

    return self; 
} 

+(FacebookConnect *) sharedFacebook 
{ 
    @synchronized([FacebookConnect class]) 
    { 
     if(!singletonDelegate) [[self alloc] init]; 
     return singletonDelegate; 
    } 
    return nil; 
} 
+(id)alloc 
{ 
    @synchronized ([FacebookConnect class]) 
    { 
     NSAssert(singletonDelegate == nil, @"Attempted to allocate a second instance of the Game Manager singleton"); 
     singletonDelegate = [super alloc]; 
     return singletonDelegate; 
    } 
    return nil; 
} 

#pragma mark - 
#pragma mark Singleton methods 
-(void) logout 
{ 
    [_facebook logout]; 
} 
-(void) feedWithScore:(int) _s andLevel:(int) _l 
{ 
    if (![_facebook isSessionValid]) { 
     NSLog(@"Session invalid - Authorize"); 
     /*NSArray *permissions = [[NSArray alloc] initWithObjects:@"user_likes", @"read_stream",nil]; 
     [_facebook authorize:permissions]; 
     [permissions release];*/ 
     [_facebook authorize:nil]; 
    } 

    [_facebook dialog:@"feed" andDelegate:self]; 

} 

#pragma mark - 
#pragma mark Facebook implementations 
// Pre iOS 4.2 support 
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    return [_facebook handleOpenURL:url]; 
} 

// For iOS 4.2+ support 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 
    return [_facebook handleOpenURL:url]; 
} 
- (void)fbDidLogin { 
    NSLog(@"Login OK"); 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[_facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[_facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 

} 
- (void) fbDidLogout { 
    NSLog(@"Log out"); 
    // Remove saved authorization information if it exists 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults objectForKey:@"FBAccessTokenKey"]) { 
     [defaults removeObjectForKey:@"FBAccessTokenKey"]; 
     [defaults removeObjectForKey:@"FBExpirationDateKey"]; 
     [defaults synchronize]; 
    } 
} 

- (void)dialogDidComplete:(FBDialog *)dialog{ NSLog(@"Login OK"); } 
- (void)dialogDidNotComplete:(FBDialog *)dialog{} 
- (void)dialogCompleteWithUrl:(NSURL *)url{} 
- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError *)error{} 
- (BOOL)dialog:(FBDialog*)dialog shouldOpenURLInExternalBrowser:(NSURL *)url{return NO;} 

- (void)fbDidNotLogin:(BOOL)cancelled{} 
- (void)fbDidExtendToken:(NSString*)accessToken 
       expiresAt:(NSDate*)expiresAt{} 
- (void)fbSessionInvalidated{} 

#pragma mark - 
#pragma mark Dealloc 
-(void) dealloc{ 
    [super dealloc]; 
    [_facebook release]; 
} 


@end 

回答

2

爲了張貼在Facebook的餵你必須有一個訪問令牌。所以你必須授權才能獲得該令牌。如果每次因爲訪問令牌已過期而獲得授權對話框。您可以在舊SDK中請求offline_access,並且新SDK會要求您在用戶每次使用應用程序時擴展access_token。儘管如此,你不應該將用戶發佈到Facebook feed。它應該問他們。但這只是我的一個良好的用戶體驗。

以下是關於如何擴展訪問令牌的文檔。

http://developers.facebook.com/docs/mobile/ios/build/#extend_token

你也應該在每次檢查您的應用程序加載:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
if ([defaults objectForKey:@"FBAccessTokenKey"] 
     && [defaults objectForKey:@"FBExpirationDateKey"]) { 
     facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
     facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 

,並檢查它已經授權:

f (![facebook isSessionValid]) { 
    [facebook authorize:nil]; 
} 

見這裏的文檔:https://developers.facebook.com/docs/mobile/ios/build/#implementsso

+0

好它並不是每次facebook都需要permis錫永。這是有一個窗口,它只是說用戶已登錄wth我的Facebook應用程序 – marcg11 2012-04-16 09:33:06

+0

我不確定你的意思。如果您要求提供所需的所有權限,則只需授權一次。每次您的應用加載時,您只需檢查一下該密鑰。查看我的編輯。 – 2012-06-19 16:02:04