我有一些代码显示了一个UIAcvityViewController,用于从我的应用程序导出自定义对象,格式为JSON。此代码在以前版本的iOS中运行良好,但现在无法通过iOS 11运行。问题是,当活动视图控制器显示时,它根本不会显示Airdrop面板或可用的收件人设备。该文档是一个NSDictionary,它被编码并写入NSData对象,然后写入磁盘并由NSURL引用。正如我所说,这段代码运行良好,在以前版本的iOS中仍然可以正常工作。我还有另一个地方,我使用UIActivityViewController导出图像文件,Airdrop继续在iOS 11中正常工作。我假设问题与导出文件的格式有关,并由URL引用我在ActivityProvider的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;
}
}
答案 0 :(得分:1)
问题是你的activityViewControllerPlaceholderItem
会返回一个词典。您在此处返回的类型用于指示您要销售的对象类型。 Airdrop并不想收到神秘的词典,所以它没有回应。如果什么是自动售货是文件网址,您应该返回一个网址。