2

我正在构建一个使用 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];

提前致谢

4

1 回答 1

8

你需要一个文件URL。尝试改变这个

NSURL *url = [NSURL URLWithString:fileName];

NSURL *url = [NSURL fileURLWithPath:fileName];
于 2016-01-20T06:19:24.520 回答