通过 AirDrop 发送自定义文件类型
Sending custom file types via AirDrop
我正在构建一个 Objective-C 应用程序,它使用 iOS 中包含的 AirDrop 功能。我尝试发送的文件是一个 .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];
提前致谢
您需要一个文件 URL。尝试改变这个
NSURL *url = [NSURL URLWithString:fileName];
至
NSURL *url = [NSURL fileURLWithPath:fileName];
我正在构建一个 Objective-C 应用程序,它使用 iOS 中包含的 AirDrop 功能。我尝试发送的文件是一个 .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];
提前致谢
您需要一个文件 URL。尝试改变这个
NSURL *url = [NSURL URLWithString:fileName];
至
NSURL *url = [NSURL fileURLWithPath:fileName];