2012-04-16 86 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