1

我正在使用 UIActivityViewController 将图像和文本共享到 Facebook 和 Twitter。如果我在设置页面中使用 Facebook 登录,Facebook 图标将显示 UIActivityViewController。但是,如果我还没有登录,那么 facebook 图标不会显示在 UIActivityViewcontroller 中。如何在不登录的情况下启用 Facebook 图标

如何在用户尝试共享时提示用户登录,而不是共享失败?

在此处输入图像描述

这是我的代码:

NSArray *activityItems = @[viewImage];

    UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    avc.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, nil];
    [self presentViewController:avc animated:YES completion:nil];
4

1 回答 1

1

(iOS 6.0 及更高版本)

我称这个函数为:

- (void)askUserToLoginToFacebookInSettings
{
    if(![SLComposeViewController isAvailableForServiceType: SLServiceTypeFacebook])
    {
        // Facebook Service Type is Available

        SLComposeViewController *slVC   =   [SLComposeViewController composeViewControllerForServiceType: SLServiceTypeFacebook];
        SLComposeViewControllerCompletionHandler handler =   ^(SLComposeViewControllerResult result)
        {
            [slVC dismissViewControllerAnimated:YES completion:Nil];
        };

        slVC.completionHandler = handler;

        [slVC.view setHidden:YES];

        [(MainViewController *)[(SettingsLayoutView *)delegate delegate] presentModalViewController:slVC animated:NO];

        [slVC.view endEditing:YES];
    }
    else
    {
        NSLog(@"Service Unavailable!!!");
    }
}

然后向用户显示警报,这在您提供的屏幕截图中可见。不会显示任何帖子表单。

但在此之前 - 我请求允许这样做:

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
{
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];
    ACAccountType *FBaccountType= [accountStore accountTypeWithAccountTypeIdentifier:
         ACAccountTypeIdentifierFacebook];

    NSDictionary *options = @{
          ACFacebookAppIdKey: @"xxxxxxxxxxx",
          ACFacebookPermissionsKey: @[@"user_birthday"],
          ACFacebookAudienceKey: ACFacebookAudienceFriends
    };

    [accountStore requestAccessToAccountsWithType:FBaccountType options:options
         completion: ^(BOOL granted, NSError *e) 
    {
         if (granted) 
         {
             NSLog(@"access granted");
         } 
         else 
         {
             NSLog(@"error getting permission %@",e);
         }
      }];
}
于 2013-09-02T05:48:55.923 回答