UIActivityViewController not showing AirDrop option in iOS11

1.1k Views Asked by At

I have some code that shows an UIAcvityViewController for exporting a custom object from my app, formatted as JSON. This code works fine in previous versions of iOS but now fails it iOS 11. The problem is that when the Activity View Controller displays, it does not display the Airdrop panel or available recipient devices at all. The document is an NSDictionary that is encoded and written to a NSData object and then written to disk and referenced by an NSURL. As I stated, this code worked fine and still works fine in previous versions of iOS. I also have another place where I am using the UIActivityViewController to export an image file, and Airdrop continues to work fine in iOS 11. I am assuming that the issue has to do with the format of the file being exported and referenced by the URL I am referencing in the url key of the ActivityProvider, but I have tried every way of outputting and encoding this object that makes sense. Here is the code I am using:

NSString *textToShare = @"I am sharing this record with you!";
NSURL* url = [self.record exportNoteToURL];

NSMutableArray* activityProviders = [[NSMutableArray alloc]initWithCapacity:0];
NoteRecordActivityProvider *provider = [[NoteRecordActivityProvider alloc] initWithPlaceholderItem:@{@"body":textToShare, @"url":url}];
[activityProviders addObject:provider];

//Initialize the ActivityViewController
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:applicationActivities];

NSArray *excludeActivities = @[UIActivityTypePostToFacebook,
                               UIActivityTypePostToTwitter,
                               UIActivityTypePostToWeibo,
                               //UIActivityTypeMessage,
                               //UIActivityTypeMail,
                              //UIActivityTypePrint,
                               UIActivityTypeCopyToPasteboard,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo,
                               UIActivityTypePostToTencentWeibo,
                               //UIActivityTypeAirDrop,
                               UIActivityTypeOpenInIBooks];

activityController.excludedActivityTypes = excludeActivities;
[activityController setValue:[NSString stringWithFormat:@"Record:  %@", self.record.title] forKey:@"subject"];
activityController.popoverPresentationController.barButtonItem = (UIBarButtonItem*)sender;
activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
    if (completed)
    {
        NSLog(@"The Activity: %@ was completed", activityType);
    }
    else
    {
        NSLog(@"The Activity: %@ was NOT completed", activityType);
    }
    [FileSystemProvider clearExportsDirectory];
};
[self presentViewController:activityController animated:YES completion:nil];

Following is the code that exports the dictionary to the URL, which seems to be working properly.

#pragma mark Document Export
-(NSURL*) exportNoteToURL
{
    NSMutableDictionary* dict =[[NSMutableDictionary alloc]initWithCapacity:0];

    // 1 Create Dictionary
    NSDictionary* noteDict = [self getSettingsDictionary];
    [dict setValue:noteDict forKey:@"Note"];

    // 2 Get File Name and create file path
    NSString* fullFilePath = [[FileSystemProvider documentPath]stringByAppendingPathComponent:@"Exports"];
    NSLog(@"Export Path:  %@", fullFilePath);
    [MiscUtilities createDirectory:fullFilePath];

    NSString* fileName = @"ExportedNoteRecord.lgz";
    fileName = self.title;
    fileName = [fileName stringByReplacingOccurrencesOfString:@" " withString:@""];
    fileName = [MiscUtilities SanitizeFileNameString:fileName];
    fileName = [NSString stringWithFormat:@"%@%@", fileName, @".lgz"];

    fullFilePath = [fullFilePath stringByAppendingPathComponent:fileName];

    // 3 Write dictionary to FileSystem
    NSURL* url = [[NSURL alloc]initFileURLWithPath:fullFilePath];
    BOOL res = [dict writeToURL:url error:&error];

    if (res)
    {
        return url;
    }
    else
    {
        return nil;
    }
}
1

There are 1 best solutions below

3
On

The problem is that your activityViewControllerPlaceholderItem returns a Dictionary. The type you return here is used as an indication of what type of object you are vending. Airdrop doesn't want to receive a mysterious Dictionary so it doesn't respond. If what are vending is a file URL, you should have been returning a URL.