我正在构建一个使用iOS中包含的AirDrop功能的Objective-C应用程序。我试图发送的文件是.plist文件,由于某种原因在技术上被认为是自定义文件类型。从我的Mac接收.plist文件时,一切都很顺利,但是当我尝试发送时,AirDrop立即失败,我能够在调试器中显示错误。
它说:
AirDrop: Invalid URL (no scheme) /var/mobile/Containers/Data/Application/AB771F7E-AFDB-4CB4-8D60-E1B62BAB8A62/Documents/Chicken.plist
后来:
Sender kSFOperationEventErrorOccured { Error = "Error Domain=SFOperation Code=-4 \"The transfer failed because there were no valid files to send.\"
最后,错误的NSLocalizedDescription说:
UserInfo={NSLocalizedDescription=The transfer failed because there were no valid files to send.}
我不确定为什么它找不到该文件,因为我在整个应用程序中使用类似的代码从该目录中检索文件,而且一切正常。以下是用户按下共享按钮时调用的IBAction方法中的代码:
recipeName = [[NSUserDefaults standardUserDefaults]
stringForKey:@"recipeName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appFolderPath = [paths objectAtIndex:0];
NSString *plistName = [recipeName stringByAppendingString:@".plist"]; //add ".plist" to the end of the recipe name
NSString *fileName = [appFolderPath stringByAppendingPathComponent:plistName]; //find plist
NSURL *url = [NSURL URLWithString:fileName];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
// Exclude all activities except AirDrop.
NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
UIActivityTypePostToWeibo,
UIActivityTypeMessage, UIActivityTypeMail,
UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
controller.excludedActivityTypes = excludedActivities;
// Present the controller
[self presentViewController:controller animated:YES completion:nil];
提前致谢
答案 0 :(得分:7)
您需要一个fileURL。尝试更改此
NSURL *url = [NSURL URLWithString:fileName];
到
NSURL *url = [NSURL fileURLWithPath:fileName];