Sharing Data in Between Apps in Ios

Sharing data between apps on iOS

You can have a common file space between apps by using app groups. An example of how to use them can be found here: Sharing data in between apps in IOS

You can use this as part of a solve for not duplicating the data in every bundle. One way might be to have the data hosted on a server somewhere and when the app is installed you can check the App Group for the common data, if it is not there, you can download it and store it there. Then the next app that is installed will have the data already available. This should help avoid having to include it in every small app.

You can set up the code to check the shared location and download the data in a framework and share it between all your apps making it a bit easier to maintain. If you do not already have a content management system then you could google for a few that have iOS support. There are many out there. You would then host the shared data there. This would give you the ability to update the data for each app while they are in the field which could be a time saver. If these apps are very small though, this may be overkill.

Share data between two or more iPhone applications

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".

Passing data between an iOS app on different devices

Based on what you have said,

You could just use the UIActivityViewController to export your data to AirDrop (or etc)
and using the exported UTI, your app could open it once it detected.

for Sharing, you will have to use a custom data format (or file) and you can refer to this post, Sending custom data via UIActivityViewController

and for reading your custom file, here is a good tutorial of how to implement the Exported UTI https://www.raywenderlich.com/8413525-universal-type-identifiers-tutorial-for-ios-importing-and-exporting-app-data

Cheers,
Kel

Sharing Data Between Applications with different App ID Prefix in iOS

No, it's not possible. Since the two apps are not part of the same app group they can not share any resources. The use of a common framework is irrelevant. The two apps will each have their own sandbox with no common area for the framework code to share.

The end result is each app using the framework will end up with its own copy of the cache.

Sharing file/data between two iOS apps that I own

You actually don't need extensions to share data between your own apps. You can use app groups for this.

In both MyApp1 and MyApp2 goto the target, then capabilities, then click on the app groups capability. Let Xcode help you get app group entitlement setup in your apple developer account.

Now you can use that app group ID to share data between you apps. For instance:

In MyApp1 put this in your appDelegate:

NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myappgroup"];
[myDefaults setObject:@"foo" forKey:@"bar"];

And in MyApp2 appDelegate put this:

NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myappgroup"];
NSLog(@"Show me something: %@",[myDefaults objectForKey:@"bar"]);

Make sure that the string you used for the suite name is the exact same as what is under the app group capabilities section in Xcode and also the string in your entitlements plist that was automatically added to your project.

You can also share files using the same idea:

NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:
@"group.mycompany.myappgroup"];

And for those of you who want to see it in Swift:

var userDefaults = NSUserDefaults(suiteName: "group.mycompany.myappgroup")

var fileManager = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.mycompany.myappgroup")

Sharing data in between apps in IOS

You can turn on App group on your App Project capabilities tab on both of your apps with the same group container ID. "group.com.yourCompanyID.sharedDefaults"

Sample Image

Then you can access the same folder from your apps using the following url:

let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourCompanyID.sharedDefaults")!

So if you would like to share a switch state from two different apps you should do it as follow:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var sharedSwitch: UISwitch!
let switchURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourCompanyID.sharedDefaults")!
.appendingPathComponent("switchState.plist")
override func viewDidLoad() {
super.viewDidLoad()
print(switchURL.path)
NotificationCenter.default.addObserver(self, selector: #selector(updateSwitch), name: .UIApplicationDidBecomeActive, object: nil)
}
func updateSwitch(_ notofication: Notification) {
sharedSwitch.isOn = NSKeyedUnarchiver.unarchiveObject(withFile: switchURL.path) as? Bool == true
}
@IBAction func switched(_ sender: UISwitch) {
let success = NSKeyedArchiver.archiveRootObject(sender.isOn, toFile: switchURL.path)
print(success)
}
}


Related Topics



Leave a reply



Submit