Search code examples
iphoneiossandbox

Why can't app access another app's document folder?


We are trying to use inter-app communication for two application and trying to send the file path from one application to another application and the problem is that if i pass any text via sender application to receiver then it works well but if i try to pass the file document path then it doesn't works here is my code for sender

-(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];
}

In receiver side i have written this code

- (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;

am i going wrong anywhere?? Thanks in advance....


Solution

  • The key word is Sandboxing. On iOS every app is sandboxed, which means it runs in it's own container and all data stored by the app should be secured against manipulation (read and write) of other apps.

    It is impossible to access other apps Documents folder when the device is not jailbroken.