无法将 Facebook 和 Twitter 添加到 UIActivityViewController

Unable to added Facebook and Twitter to UIActivityViewController

尝试在社交媒体、电子邮件和消息上分享应用程序。 由于电子邮件和消息对我来说工作正常。 但是 Facebook 和 twitter 并没有全部显示给 UIActivityViewController

下面是我尝试的代码。

NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                               UIActivityTypePostToFacebook,
                               UIActivityTypePostToTwitter];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[string, URL]
                                  applicationActivities:nil];
activityViewController.excludedActivityTypes = excludeActivities;
[self presentViewController:activityViewController
                                   animated:YES
                                 completion:^{
                                 }];

- (id)activityViewController:(UIActivityViewController *)activityViewController
         itemForActivityType:(NSString *)activityType
{
    if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
        return NSLocalizedString(@"calling facebook", );
    } else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
        return NSLocalizedString(@"calling twitter", );
    } else {
        return nil;
    }
}

@全部 请让我知道我需要做什么。

这里的一个错误是 exclude activities 也有 facebooktwitter。因此它不会以任何方式显示。

注意 excludedActivityTypes 应该是控制器中不需要的。

例如应该是:

NSArray *excludeActivities = @[UIActivityTypePostToWeibo,
                               UIActivityTypeMessage,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo,
                               UIActivityTypePostToTencentWeibo];

为此,只需创建一个 class 即可。

SharingActivityProvider.h

#import <Foundation/Foundation.h>


@interface SharingActivityProvider : UIActivityItemProvider
@end


**SharingActivityProvider.m**

#import "SharingActivityProvider.h"


@implementation SharingActivityProvider {

}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {

    // Create the default sharing string
    NSString *shareString = @"";

    // customize the sharing string for facebook, twitter, weibo, and google+
    if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
        shareString = [NSString stringWithFormat:@"Attention Facebook:%@", shareString];
    } else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
        shareString = [NSString stringWithFormat:@"Attention Twitter:%@", shareString];
    } else if ([activityType isEqualToString:UIActivityTypePostToWeibo]) {
        shareString = [NSString stringWithFormat:@"Attention Weibo:%@", shareString];
    }

    return shareString;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return @"";
}

@end

然后在使用下面的代码之后,假设按钮动作。

-(IBAction)shareAppTapped:(id)sender{

    SharingActivityProvider *sharingActivityProvider = [[SharingActivityProvider alloc] init];

    NSArray *activityProviders = @[sharingActivityProvider,@"what ever text"];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:nil];

    // tell the activity view controller which activities should NOT appear
    activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];

    activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:activityViewController animated:YES completion:nil];
   // [self.btnShareApp setBackgroundColor:OnebackgroundColorTable];
}