为什么app无法访问其他应用的文档文件夹?

时间:2013-10-05 06:18:38

标签: iphone ios sandbox

我们正在尝试为两个应用程序使用应用程序间通信,并尝试将文件路径从一个应用程序发送到另一个应用程序,问题是如果我通过发送方应用程序将任何文本传递给接收方,那么它运行良好但是如果我尝试传递文件文档路径然后它不起作用  这是我的发件人代码

-(IBAction) openReceiverApp:(id)sender {
// Opens the Receiver app if installed, otherwise displays an error

UIApplication *ourApplication = [UIApplication sharedApplication];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"ads.rtf"];
NSLog(@"filePath %@", filePath);
//  NSString *URLEncodedText = [self.textBox.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 NSString *URLEncodedText = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *ourPath = [@"readtext://" stringByAppendingString:URLEncodedText];

NSLog(@"%@",ourPath);

NSURL *ourURL = [NSURL URLWithString:ourPath];
if ([ourApplication canOpenURL:ourURL]) {
    [ourApplication openURL:ourURL];
}
else {
    //Display error
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Receiver Not Found" message:@"The Receiver App is not installed. It must be installed to send text." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

在接收方,我写了这段代码

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// Display text
UIAlertView *alertView;
NSString *text = [[url host]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
alertView = [[UIAlertView alloc] initWithTitle:@"Text" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];

return YES;
我在哪里出错? 提前谢谢....

3 个答案:

答案 0 :(得分:3)

关键词是Sandboxing。在iOS上,每个应用程序都是沙盒,这意味着它在自己的容器中运行,应用程序存储的所有数据都应该受到其他应用程序的操作(读取和写入)的保护。

当设备没有越狱时,无法访问其他应用程序文档文件夹。

答案 1 :(得分:1)

我不确定你是否设法解决了这个问题。但我想分享这些可能有用的信息。 Apple已经在iOS8中发布了一个新的api来访问应用程序沙箱外的文档。

文件选择器 文档选取器视图控制器(UIDocumentPickerViewController)授予用户对应用程序沙箱外部文件的访问权限。这是一种在应用程序之间共享文档的简单机制。它还支持更复杂的工作流程,因为用户可以使用多个应用程序编辑单个文档。

文档选择器允许您从多个文档提供程序访问文件。例如,iCloud文档提供程序授予对存储在另一个应用程序的iCloud容器中的文档的访问权限。第三方开发人员可以使用存储提供程序扩展提供其他文档提供程序。

有关详细信息,请参阅“文档选择器编程指南”。 (https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentPickerProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014451

答案 2 :(得分:0)

我认为有一种称为钥匙串访问组的东西可以满足您的需求。我从未使用它,所以我不确定它。阅读并确定它是否适合您。我见过的一些评论表明,设置和测试很难确定。

如果在安装两个应用程序后需要在用户的设备上续订钥匙串访问权限,您需要担心会发生什么。如何续订是这样一种方式,他们继续共享对彼此的文件的访问权限?