【问题标题】:How can I share the contents of my page in iPhone app on facebook?如何在 Facebook 上的 iPhone 应用程序中分享我的页面内容?
【发布时间】:2013-01-09 10:49:36
【问题描述】:

我在我的 iPhone 应用程序屏幕上提供了 facebook 分享选项,并且需要在用户的 facebook 个人资料上分享一些图片和 URL。我知道有几个第三方框架,如 ShareKit 和其他功能,如适用于 iOS 的 facebook sdk,但这是我能做到的最好方法,我需要获取共享按钮,直到现在,如图所示下面

点击后应该重定向到身份验证。 并且应该兼容iOS5及以上,希望我的问题很清楚。

提前致谢

【问题讨论】:

    标签: ios objective-c facebook facebook-graph-api facebook-ios-sdk


    【解决方案1】:

    通过研究自己解决了这个问题

    我使用 Facebook Graph API,因为我的应用程序需要来自 iOS4+ 的支持,如果它是 iOS6 的话会很简单,因为 iOS6 具有 Facebook 集成并且只需要几行代码来实现它。 导入Graph APi文件并添加如下代码:

    @synthesize facebook;
    
    //login with facebook
    - (IBAction) facebookButtonPressed {
        if (!facebook || ![facebook isSessionValid]) {
            self.facebook = [[[Facebook alloc] init] autorelease];
            NSArray *perms = [NSArray arrayWithObjects: @"read_stream", @"create_note", nil];
            [facebook authorize:FACEBOOK_API_KEY permissions:perms delegate:self];
        }
        else {
            [self fbDidLogin];
        }
    }
    
    //upload image once you're logged in
    -(void) fbDidLogin {
        [self fbUploadImage];
    }
    
    - (void) fbUploadImage
    {
        NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        resultImage, @"picture",
                                        nil];
        [facebook requestWithMethodName: @"photos.upload" 
                              andParams: params
                          andHttpMethod: @"POST" 
                            andDelegate: self]; 
    
        self.currentAlertView = [[[UIAlertView alloc]
                                 initWithTitle:NSLocalizedString(@"Facebook", @"")  
                                 message:NSLocalizedString(@"Uploading to Facebook.", @"")  
                                 delegate:self 
                                 cancelButtonTitle:nil
                                 otherButtonTitles: nil] autorelease];
    
        [self.currentAlertView show];
    }
    
    //facebook error
    - (void)request:(FBRequest*)request didFailWithError:(NSError*)error
    {
    
        [self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
        self.currentAlertView = nil;
    
        UIAlertView *myAlert = [[UIAlertView alloc]
                                initWithTitle:NSLocalizedString(@"Error", @"")  
                                message:NSLocalizedString(@"Facebook error message", @"")
                                delegate:self 
                                cancelButtonTitle:nil
                                otherButtonTitles:@"OK", nil];
        [myAlert show];
        [myAlert release];
    }
    
    //facebook success
    - (void)request:(FBRequest*)request didLoad:(id)result
    {
        [self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
        self.currentAlertView = nil;
    
        UIAlertView *myAlert = [[UIAlertView alloc]
                                initWithTitle:NSLocalizedString(@"Facebook", @"") 
                                message:NSLocalizedString(@"Uploaded to Facebook message", @"")
                                delegate:self 
                                cancelButtonTitle:nil
                                otherButtonTitles:@"OK", nil];
        [myAlert show];
        [myAlert release];
    }
    

    要获取特定 URL 在 facebook 中的共享计数, 使用

    http://graph.facebook.com/?id=url
    

    但这会返回所有喜欢、分享和帖子的总数,因此最好找到一些替代方法。

    希望这会有所帮助 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多