Share data between two or more iPhone applications

2019-01-01 05:15发布

Is this possible to share data between two applications on the same device?

Or can I allow some other application to use my application's information / data or in any other way?

For example, the first application is for event management, and I use it to save some event. The second application is for reminders, which will get data from the other application in order to remind me about the event.

This is just a simple example, not a real scenario.

10条回答
怪性笑人.
2楼-- · 2019-01-01 05:33

No. You would have to use some cloud solution.

查看更多
谁念西风独自凉
3楼-- · 2019-01-01 05:37

You can use https://github.com/burczyk/Camouflage to read and write NSData to iOS Camera Roll as .bmp file and share it between apps :)

Brand new solution!

查看更多
路过你的时光
4楼-- · 2019-01-01 05:38

Since iOS 8 you can easily share data between apps as long as they are in the common App Group.

Apple documentation best explains it in the Extensions context: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

Basically, you need to:

  1. Define App Group ID (in Certificates, Identifiers & Profiles section of Member Center for your Apple Developer Program.
  2. Enable App Groups capability specifying the above App Group ID for each app that needs to communicate (cen be done either in Xcode: Target -> Capabilities or at Member Center).
  3. Use one of two APIs for shared container access.

First API is based on NSUserDefaults:

NSString *appGroupId = @"group.my.group.id";

NSUserDefaults *myDefaults = [[NSUserDefaults alloc] 
                    initWithSuiteName:appGroupId];

[myDefaults setObject:@"foo" forKey:@"bar"];

Second API is based on NSFileManager. It's simply a shared folder that you can access after obtaining it's url:

NSString *appGroupId = @"group.my.group.id";

NSURL *sharedFolderURL = [[NSFileManager defaultManager] 
                           containerURLForSecurityApplicationGroupIdentifier:appGroupId];

Anything you put inside myDefaults or the folder pointed by sharedFolderURL will be visible and accessible for all your apps.

In case of folder, please write/read atomically just to ensure no deadlocks are possible.

查看更多
若你有天会懂
5楼-- · 2019-01-01 05:42

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.

  1. first Enable "App Groups" from your developer account.
  2. Generate Provisioning profile. and use it.

Now You have to create Two Apps. Sample Name

  1. Demo_Share_One
  2. 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. :)

查看更多
泪湿衣
6楼-- · 2019-01-01 05:42

Share data between apps possible? Yes it is!

Use UIPasteBoard available from iOS 3.0, documentation is available here. Apple docs say:

The UIPasteboard class enables an application to share data within the application or with another application using system-wide or application-specific pasteboards.

It is also possible to share data between apps in the keychain, although the data is primarily meant to be passwords and such, anything serializable could be stored. Here is a Stack Overflow question about that.

查看更多
回忆,回不去的记忆
7楼-- · 2019-01-01 05:50

Historically, the iPhone has tried to prevent data sharing between apps. The idea was that if you couldn't get at another app's data, you couldn't do anything bad to that app.

In recent releases of IOS, they've loosened that up a bit. For example, the iOS programming guide now has a section on passing data between apps by having one app claim a certain URL prefix, and then having other apps reference that URL. So, perhaps you set your event app to answer "event://" URLs the same way that a webserver answers for "http://" URLs.

Apple's documentation of that approach is here.

Have a peek under "Implementing Custom URL Schemes".

查看更多
登录 后发表回答