From iOS 8 I've successfully Access Same folder in using "App Group Functionality." I'm extending answer of @siejkowski.
Note: It will work from same developer account only.
For that you have to follow below steps.
- first Enable "App Groups" from your developer account.
- Generate Provisioning profile. and use it.
Now You have to create Two Apps. Sample Name
- Demo_Share_One
- Demo_Share_Two
Now We are copying images from Demo_Share_One to Sharing folder which is created by default when you enable App Groups and run app. and will access all those images from Demo_Share_Two.
You have to Take Group Name which was set to your developer account.lets say group.filesharingdemo
.
Add Below method in Both apps to get Relative path of sharing folder url.
- (NSString *) getSharedLocationPath:(NSString *)appGroupName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *groupContainerURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:appGroupName];
return [groupContainerURL relativePath];
}
Now we are Copying Images from Bundle from Demo_Share_One
-(IBAction)writeImage:(id)sender
{
for (int i = 0; i<15; i++)
{
NSString *strSourcePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"hd%d",i+1] ofType:@"jpg"];
NSString *strDestinationPath = [[self getSharedLocationPath:@"group.filesharingdemo"] stringByAppendingPathComponent:[NSString stringWithFormat:@"hd%d",i+1]] ;
BOOL filewrite = [[NSFileManager defaultManager]copyItemAtPath:strSourcePath toPath:strDestinationPath error:nil];
if (filewrite)
NSLog(@"File write");
else
NSLog(@"can not write file");
}
}
Now in Demo_Share_Two to access those images
NSString *pathShared = [[self getSharedLocationPath:@"group.filesharingdemo"] stringByAppendingPathComponent:[NSString stringWithFormat:@"hd%d.jpg",number]];
NSLog(@"%@",pathShared);
//BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:pathShared];
imgView.image = [UIImage imageWithContentsOfFile:pathShared];
And Now You will get all images which your write from Demo_Share_One.
So From now onwards if you want to share this folder two your third app. just add that app in your group. So it is too easy to access same elements in Your Multiple apps.
if you will not enable App Groups in your AppID then you will get [self getSharedLocationPath:@"group.filesharingdemo"] is null.
Thanks to Apple for Share Elements from your own apps functionality. Happy Coding. :)