未配置应用时在Facebook上分享

时间:2016-05-06 08:50:44

标签: ios swift slcomposeviewcontroller sfsafariviewcontroller

我想在Facebook上发布一些内容。我已经使用了SLComposeViewController。我只是想问如果用户没有在手机中配置其应用程序我该如何分享。 有什么办法可以在浏览器中打开然后发布任何内容。 考虑我想发帖说“你好那里”。所以我保留这个字符串,打开safari并登录。登录后,字符串会自动发布

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
            let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            fbShare.completionHandler = {
                result in
                switch result {
                case SLComposeViewControllerResult.Cancelled:
                    //Code to deal with it being cancelled
                    break

                case SLComposeViewControllerResult.Done:
                    //Code here to deal with it being completed
                    break
                }
            }
        refrenceViewController.presentViewController(fbShare, animated: true, completion: nil)

    } else {
        //open safari and post it there
    }

1 个答案:

答案 0 :(得分:0)

我使用以下代码将视频发布到Facebook。您可以使用类似的方法发布文字。

NSData *data = [NSData dataWithContentsOfURL:outputFileURL];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                               data, @"video.mp4",
                                               @"video/mp4", @"contentType",
                                               caption, @"description",
                                               nil];
/* make the API call */
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                              initWithGraphPath:@"/me/videos"
                                              parameters:params
                                              HTTPMethod:@"POST"];

[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                      id result,
                                                      NSError *error) {
       if(!error) {
              DDLogDebug(@"result %@",result);
       } else {
              DDLogError(@"error description : %@",error.description);
              [Helper showToast:[NSString stringWithFormat:@"Unable to share to Facebook : Error: %@",[error localizedDescription]] withDuration:1];
       }
 }];

当然,在此之前,您需要确保已经授予了FBSDKAccess令牌。您可以查看facebook sdk的完整文档 https://developers.facebook.com/docs/ios/graph