iPhone - Facebook 连接

标签 iphone objective-c ios facebook

我正在开发一款 iphone 游戏,我想让用户在 facebook 上发布他们的分数。我已经设法实现了它,但每次我想发布提要时,都会有一个 facebook 窗口告诉我我已经登录到我的 Facebook 应用程序,用户需要点击接受才能显示将提要发布到的下一个窗口配置文件的墙。

我需要跳过 Facebook 登录吗?我的意思是 facebook 授权方法并直接跳转到 facebook feed 方法?

这就是我所拥有的,但是 session 总是无效的! fbDidLogin 永远不会被调用,在调试中它会给我这种错误:

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

如果我注销然后尝试提要,我必须登录 2 次,一次用于 session ,一次用于提要。

这是代码:

#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

最佳答案

要在 Facebook 订阅源上发帖,您必须拥有访问 token 。因此,您必须授权才能获取该 token 。如果您每次都收到授权对话框,那是因为访问 token 已过期。您可以在旧SDK中请求offline_access,新SDK会在用户每次使用您的应用时要求您扩展access_token。不过,作为一种观点,您不应该“制作”一个用户帖子到 Facebook 提要。它应该问他们。但这只是我对良好用户体验的看法。

这里是关于如何扩展访问 token 的文档。

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

关于iPhone - Facebook 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10167427/

相关文章:

iphone - 在我的步行/运行应用程序中发现错误的距离和路线

ios - iPad:iPad 中的 Linkedin API 未调整大小以适应更大的登录 View

iphone - iPhone 上的快速(反)序列化

隐藏在 UIActionSheet 后面的 iPhone 弹出对话框

objective-c - AVFoundation 文档代码

ios - 选择图像 ios 的不规则部分

ios - 如何上传用户个人资料图片以及如何从其他用户设备获取该个人资料图片?

iphone - IOS新Facebook iPhone App UISplitViewController Layout

iphone - 如何制作更圆润的 UITextField?就像 iPad 上 Safari 中的搜索字段一样

iphone - 在 objective-c 中枚举具有固定长度的 nsstring 的所有子字符串