Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable "Save To Files" in iOS 11?

I have an iOS app which creates a PDF and is shared using the UIActivityViewController using the default share functionality. I had to block a few sharing facilities like Vimeo, Facebook, Twillio, etc. and also the file should not be stored on the device.

With the new iOS 11 version, Apple has provided SaveToFiles option with the default share functionality. I tried using excludedActivityTypes, and with blocking the "com.apple.CloudDocsUI.AddToiCloudDrive" option, but no success.

Can anyone help me to disable the SaveToFile option though Swift? I am using Xcode 9.3 and Swift 4.

like image 561
BlackPearl12 Avatar asked Nov 17 '22 13:11

BlackPearl12


1 Answers

Currently, we probably cannot disable Save to Files or Add to Shared Album by add excludedActivityTypes in activityViewController. But we can prevent when user did press two activity types will do not perform action instead we'll showing alert.

First we create custom UIActivityItemProvider

class ActivityItemProvider: UIActivityItemProvider {
  override func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
    // in here we'll check activityType = "com.apple.CloudDocsUI.AddToiCloudDrive" (Save to Files),
    // activityType = "com.apple.mobileslideshow.StreamShareService" (Shared Album)
    if(activityType.rawValue.contains("com.apple.CloudDocsUI.AddToiCloudDrive") || (activityType.rawValue.contains("com.apple.mobileslideshow.StreamShareService") {
      // dismiss activityViewController first
      activityViewController.dismiss(animated: true, completion: nil)
      // show alert controller, we can using UIApplication.shared.keyWindow?.rootViewController to present alert
      return nil
    }
    return self.placeholderItem
}

In where we call UIActivityController, let's using

let item = ActivityItemProvider.init(placeholderItem: {your item})
let activityViewController = UIActivityViewController.init(activityItems: item, applicationActivities: nil)

ObjectiveC version

#import "BRActivityItemProvider.h"
#import "UIViewController+Additions.h"
@implementation BRActivityItemProvider

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType {
  [super activityViewController:activityViewController itemForActivityType:activityType];
  NSLog(@"itemForActivityType %@", activityType);
  if([activityType containsString:@"com.apple.CloudDocsUI.AddToiCloudDrive"] || [activityType containsString:@"com.apple.mobileslideshow.StreamShareService"]) {
    [activityViewController dismissViewControllerAnimated:true completion:^{
      UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
      if(rootController != nil) {
        NSString *str = [activityType containsString:@"com.apple.CloudDocsUI.AddToiCloudDrive"]
        ? @"Cannot save file to iCloud Driver"
        : @"Cannot save file to Shared Album";
        dispatch_async(dispatch_get_main_queue(), ^{
// this's my custom show alert controller, you can change your own
          [rootController showAlertController:str action:^(UIAlertAction * _Nonnull action) {
          }];
        });
      }
    }];
    return nil;
  }
  return self.placeholderItem;
}

In where call UIActivityViewController

BRActivityItemProvider *itemProvider = [[BRActivityItemProvider alloc]initWithPlaceholderItem:item];
    UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:itemProvider applicationActivities:nil];
    activityController.popoverPresentationController.sourceView = controller.view;
// your logic code
// ....
like image 166
Giang Avatar answered Jan 01 '23 07:01

Giang