Facebook Connect iPhone问题

时间:2010-01-31 22:31:12

标签: iphone sdk fbconnect

我正在使用Facebook Connect分享我的申请。所以我想通过UIACtionSheet实现带有2个按钮的Facebook Connect API,“登录/在Facebook上分享”。

现在,我有一些问题:

假设UIActionSheet上有两个按钮,标题为“在Facebook上分享”“登录”。

我想,当用户登录Facebook时,我的登录按钮标题将更改为“注销”。 我知道我应该使用这个功能:

- (void)session:(FBSession*)session didLogin:(FBUID)uid {
    //a code that change my login button title  to LogOut
}

2-我已登录Facebook。当我要从我的应用程序中出去并再次打开应用程序时,我应该再次登录!我怎么能阻止这个?

3-最后,我想将一些文本从UIWebView分享到Facebook。我的webview插座名为“myWeb”。我如何将Facebook Connect与UIWebView连接以共享它?

我应该使用

-(void)publishFeed:(id)target 

2 个答案:

答案 0 :(得分:10)

#pragma mark FBDialog delegate methods
- (void)dialogDidSucceed:(FBDialog *)dialog {
    if ([dialog isMemberOfClass:[FBLoginDialog class]]) {
        NSLog(@"[FBLoginDialog::dialogDidSucceed] just did succeed");
    } else if ([dialog isMemberOfClass:[FBPermissionDialog class]]) {
        NSLog(@"[FBPermissionDialog::dialogDidSucceed] update user status");
        [self facebookUpdateUserStatus];
    }
}

- (void)dialogDidCancel:(FBDialog *)dialog {    
}

- (void)dialog:(FBDialog *)dialog didFailWithError:(NSError *)error {
    NSLog(@"dialog:%@ didFailWithError:%@", dialog, error); 
}

#pragma mark FBSession delegate methods
- (void)session:(FBSession *)session didLogin:(FBUID)uid {
    NSLog(@"User with id %lld logged in.", uid);
    [self facebookCheckForPermission];
}

- (void)request:(FBRequest*)request didReceiveResponse:(NSURLResponse*)response {
    NSLog(@"did r response");
}

- (void)request:(FBRequest*)request didLoad:(id)result {
    if ([@"facebook.Users.hasAppPermission" isEqualToString: request.method]) {
        if ([@"1" isEqualToString: result]) {
            // post comment
            NSLog(@"[Users.hasAppPermission::dialogDidSucceed] succeed, update status");
            [self facebookUpdateUserStatus];
        } else {
            // show dialog
            NSLog(@"[Users.hasAppPermission::dialogDidSucceed] fail, show dialog");         
            FBPermissionDialog* dialog = [[[FBPermissionDialog alloc] init] autorelease]; 
            dialog.delegate = self; 
            dialog.permission = @"status_update"; 
            [dialog show];      
        }
    } else if ([@"facebook.Users.setStatus" isEqualToString: request.method]) {
        if ([@"1" isEqualToString: result]) {
            NSLog(@"facebook update did succeed");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Facebook"
                                                            message: @"L'article a bien été publié sur votre profil"
                                                           delegate: nil
                                                  cancelButtonTitle: @"OK"
                                                  otherButtonTitles: nil]; 
            [alert show];
            [alert release];            
        } else {
            NSLog(@"facebook update did fail");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Facebook"
                                                        // Slava, change text here
                                                            message: @"Update did fail"
                                                           delegate: nil
                                                  cancelButtonTitle: @"OK"
                                                  otherButtonTitles: nil]; 
            [alert show];
            [alert release];            
        }
    }
}

- (void)request:(FBRequest*)request didFailWithError:(NSError*)error {
    NSLog(@"did fail: %@", [error localizedDescription]);
}

#pragma mark FBSession helper functions
- (FBSession *)fbSessionWithDelegate:(id)theDelegate {
    if (nil != [FBSession session]) {
        return [[FBSession session] retain]; // fuckup this leak =)
    }

    FBSession *session = [FBSession sessionForApplication: kFBAPIKeyEncoded 
                                                   secret: kFBAPISecretEncoded 
                                                 delegate: theDelegate];
    return session;
}

- (void) facebookCheckForPermission {
    NSLog(@"[facebookCheckForPermission] make a call");
    NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys: @"status_update", @"ext_perm", nil];
    //  [[FBRequest requestWithDelegate: self] call: @"facebook.Users.hasAppPermission" params: d];     
    FBSession *fbSession = [self fbSessionWithDelegate: self];
    [[FBRequest requestWithSession: fbSession delegate: self] call: @"facebook.Users.hasAppPermission" params: d];  
}

- (void) facebookUpdateUserStatus {
    NSLog(@"[facebookUpdateUserStatus] updating status");
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat: @"Je te recommande cet article: %@", postURL],
                            @"status", @"true", @"status_includes_verb", nil]; 
    FBSession *fbSession = [self fbSessionWithDelegate: self];
    updateRequest = [FBRequest requestWithSession: fbSession delegate: self];
    [updateRequest call: @"facebook.Users.setStatus" params: params];
}

答案 1 :(得分:0)

使用[session resume],如果用户已使用您的应用程序,则返回YES,否则返回NO。 通过使用这个mehhod,无需再次登录。