简体   繁体   中英

iOS: Disabling WhatsApp in UIActivityViewController?

I'm using the following code to share a string only to Facebook using UIActivityViewController

UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
                                applicationActivities:nil];

activityViewController.excludedActivityTypes = @[UIActivityTypePostToWeibo,
                                   UIActivityTypeMessage,
                                   UIActivityTypeMail,
                                   UIActivityTypePrint,
                                   UIActivityTypeCopyToPasteboard,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo,
                                   UIActivityTypePostToTencentWeibo,
                                   UIActivityTypeAirDrop,
                                   UIActivityTypePostToTwitter];


[self presentViewController:activityViewController animated:YES completion:^{
...
}

This works except it also shows the WhatsApp icon and the more icon. Is there a way to remove these two? Thanks in advance!

No, you can only remove activity types that Apple has provided constants for. There is no way to remove 3rd party apps that appear or the "More..." activity.

If you only want to share with a single, specific app, there's no point in using a UIActivityController .

Use one of the various Facebook libraries/APIs to share your data with Facebook.

Whilst rmaddy's answer used to be true, for anyone coming across this since iOS 10 it's fairly simple to do:

activityController.excludedActivityTypes = [
    UIActivity.ActivityType(rawValue: "net.whatsapp.WhatsApp.ShareExtension")
]

If you want to hide other non-system activity types, you can share to them, and then use the identifier returned in the completionWithItemsHandler closure:

shareActivityController.completionWithItemsHandler = { [weak self] (activityType, completed, returnedItems, activityError) in
    print("Activity Type", activityType?.rawValue ?? "?") // <--- Use the identifier this logs!
}

Although admittedly in the case of the OP's question, this still isn't going to help you as to limit it to only Facebook, you'd have to install all the other thousands of apps with share activities in order to disable them all!

Here are all the identifiers for apps I happened to have installed on my iPhone:

UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
UIActivity.ActivityType(rawValue: "net.whatsapp.WhatsApp.ShareExtension"),
UIActivity.ActivityType(rawValue: "pinterest.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.facebook.Messenger.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.tinyspeck.chatlyio.share"), // Slack!
UIActivity.ActivityType(rawValue: "ph.telegra.Telegraph.Share"),
UIActivity.ActivityType(rawValue: "com.google.Drive.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.toyopagroup.picaboo.share"), // Snapchat!
UIActivity.ActivityType(rawValue: "wefwef.YammerShare"),
UIActivity.ActivityType(rawValue: "com.fogcreek.trello.trelloshare"),
UIActivity.ActivityType(rawValue: "com.linkedin.LinkedIn.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.hammerandchisel.discord.Share"),
UIActivity.ActivityType(rawValue: "com.google.Gmail.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.google.inbox.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.riffsy.RiffsyKeyboard.RiffsyShareExtension"), //GIF Keyboard by Tenor
UIActivity.ActivityType(rawValue: "com.google.hangouts.ShareExtension"),
UIActivity.ActivityType(rawValue: "com.ifttt.ifttt.share")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM