Add to iCloud Drive with UIActivityViewController

1.4k Views Asked by At

I would like to use UIActivityViewController's "Add to iCloud Drive" action pictured here to upload a file that I store on the iOS device:

enter image description here

Currently in my custom UIActivityItemProvider class I have my - (id)item method returning this: [NSURL fileURLWithPath:fullPath];

FullPath is the path to the file that I am creating and want to upload to iCloud drive. It works when I click "Add to Notes", the mail activity, or my custom activity but it does not for iCloud drive. Instead of using this file, it creates a default file with the subject I am using for the email activity which is "Attached is a file." as the file contents. Here is the code I am using to present the UIActivityViewController:

CustomActivity * customActivity = [[CustomActivity alloc]init];
CustomItemProvider *itemProvider = [[CustomItemProvider alloc] initWithPlaceholderItem:_itemsArray];

NSArray *activityItems = [NSArray arrayWithObjects:itemProvider, NSLocalizedString(@"Attached is a file.", nil), nil];

UIActivityViewController *avc = [[UIActivityViewController alloc]
                                 initWithActivityItems:activityItems
                                 applicationActivities:@[customActivity]];

float iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

if (iOSVersion < 9.0)
{
    avc.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypeCopyToPasteboard, UIActivityTypeMessage, UIActivityTypePostToFlickr, UIActivityTypePostToTwitter, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToTencentWeibo,UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
}
else
{
    avc.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypeCopyToPasteboard, UIActivityTypeMessage, UIActivityTypePostToFlickr, UIActivityTypePostToTwitter, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToTencentWeibo,UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList, UIActivityTypeOpenInIBooks];
}

[self presentViewController:avc animated:YES completion:nil];
1

There are 1 best solutions below

2
On

Code seems to correct. But iCloud option will be added only when :

  1. User must login in iCloud in Settings.

  2. You have something valid info like you can upload on iCloud. (If you are using any url in sharing then iCloud option is not shown in ActivityController)

It support those type of data it can allow you to upload, like notes, photos, contact, calendar event, etc.

I have tried this :

NSString *strTextToShare = @"Hi... Friends";
UIImage *imgToShare = [UIImage imageNamed:@"wall1.png"];
NSURL *urlToShare = [NSURL URLWithString:@"https://www.apple.com"];

UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:@[strTextToShare, /*imgToShare, urlToShare*/] applicationActivities:nil];
[self presentViewController:shareController animated: YES completion: nil];

In above, if I tried to add URL then iCloud not shown. And also

Note : At a time if only one item you share, then only iCloud will shown.