我有一些代码显示 UIAcvityViewController 用于从我的应用程序导出自定义对象,格式为 JSON。此代码在以前的 iOS 版本中运行良好,但现在在 iOS 11 中失败。问题是当 Activity View Controller 显示时,它根本不显示 Airdrop 面板或可用的接收设备。该文档是一个 NSDictionary,它被编码并写入 NSData 对象,然后写入磁盘并由 NSURL 引用。正如我所说,这段代码运行良好,并且在以前的 iOS 版本中仍然运行良好。我还有另一个地方使用 UIActivityViewController 导出图像文件,Airdrop 在 iOS 11 中继续正常工作。我假设这个问题与我在 ActivityProvider 的 url 键中引用的 URL 导出和引用的文件的格式有关,但我已经尝试了所有有意义的输出和编码这个对象的方法。这是我正在使用的代码:
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];
以下是将字典导出到 URL 的代码,它似乎工作正常。
#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;
}
}