Sharing Data with AppGroup
- Save data to
UserDefaults
in your main App:
UserDefaults(suiteName: <your_app_group>)!.set("test", forKey: "test")
- Read data from
UserDefaults
in your Widget:
let testStr = UserDefaults(suiteName: <your_app_group>)!.string(forKey: "test")
If you want to save other types see:
- How can I use UserDefaults in Swift?
App Groups Data Sharing Between Applications iOS
You can store shared data using App Groups, but I've only tried to do this with built in types.
If you're trying to store your own classes you may need to look at something like NSKeyedArchiver and NSCoding (maybe look here).
If you can find a way to use the built in types you can use App Groups as follows:
Sharing NSUserDefaults data between multiple apps
In order to have shared defaults between an app and an extension or between 2 apps you have to add an App Group in your settings using the following steps:
- In the Project Navigator click on the *.xcodeproj file (should be at the top).
- To the right of the Project Navigator look for Project and Targets. Under targets click on your primary target (should be the first thing under Targets).
- Towards the top, click on the Capabilities tab.
- In the App Groups section click the switch to the right to turn App Groups ON.
- Click on the + button and add an App Group named group.com.company.myApp.
- Go to the same place in your other apps and this group should now be available to select. Turn this group on for each app that will be using this shared data.
Note: If you go to the Apple Developer Portal (the Apple website that shows all of your Certificates, Identifiers, Devices and Provisioning Profiles) and go to Identifiers > App Groups you should see this new App Group.
To store data:
var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
userDefaults!.setObject("user12345", forKey: "userId")
userDefaults!.synchronize()
To retrieve data:
var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
if let testUserId = userDefaults?.objectForKey("userId") as? String {
print("User Id: \(testUserId)")
}
Communicating and persisting data between apps with App Groups
Another benefit to App Groups is the ability to share a NSUserDefaults
database. This also works for App Extensions (notification center widgets, custom keyboards, etc).
Initialize your NSUserDefaults
object like this in all applications in the app group and they will share the database:
Objective-C:
[[NSUserDefaults alloc] initWithSuiteName:@"<group identifier>"];
Swift:
NSUserDefaults(suiteName: "<group identifier>")
Keep in mind everything from the [NSUserDefaults standardUserDefaults]
database for each application will not carry over into this database.
The documentation gives a correct example as well (As of Beta 3).
And don't forget to synchronize the database:
[yourDefaults synchronize];
Can I share files/data through App Groups but in different developer accounts applications?
The answer is no.
If you create a provisioning profile with an app group my.app.group
and extract entitlements from it, you will see that the group is referenced as XXXXXXX.my.app.group
where XXXXXXX
is your developer ID, so on another account the group will be YYYYYYY.my.app.group
and it will be perceived as a different group by any device.
More info: App ID, Application Groups
Related Topics
How to Specify the Type Information of an Array That Would Hold Swiftui Custom Views
How to Set Scrollview Content Size in Swift 3.0
What Are the Fundamental Differences Between Set and Didset
Cannot Call Value of Non-Function Type 'Ciimage'
Searchbar Problem While Trying to Search Firestore and Reload the Tableview
Differences Generic Protocol Type Parameter VS Direct Protocol Type
Does Nsnumberformatter.Stringfromnumber Ever Return Nil
How Have Multiple Init() with Swift
Implementing Stringliteralconvertible on Nsurl
Displaying State of an Async API Call in Swiftui
Hovering a Modelentity in Front of Arcamera
How to Convert a Unsafemutablepointer<Void> to Uint8
Why Is Casting a Struct to Anyobject Not a Compile Error in Swift
How to Use Unsafemutablepointer<Opaquepointer> in Swift
Firestore Query Across Multiple Fields for Same Value