Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between an iOS 8 share extension and main app

Recently, I've been making a simple iOS 8 share extension to understand how the system works. As Apple states in its App Extension Programming Guide:

By default, your containing app and its extensions have no direct access to each other’s containers.

Which means the extension and the containing app do not share data. But in the same page Apple brings a solution:

If you want your containing app and its extensions to be able to share data, use Xcode or the Developer portal to enable app groups for the app and its extensions. Next, register the app group in the portal and specify the app group to use in the containing app.

Then it becomes possible to use NSUserDefaults to share data between the containing app and the extension. This is exactly what I would like to do. But for some reason, it does not work.

In the same page, Apple suggests the standard defaults:

var defaults = NSUserDefaults.standardUserDefaults() 

In a WWDC presentation (217), they suggest a common package:

var defaults = NSUserDefaults(suiteName: kDefaultsPackage) 

Also, I enabled App Groups for both the containing app target and the extension target, with the same App Group name: XCode Target capabilities, App Groups

But all this setup is for nothing. I cannot retrieve the data I stored in the containing app, from the extension. It is like two targets are using completely different NSUserDefaults storages.

So,

  1. Is there a solution for this method?
  2. How can I share simple data between the containing app and the share extension? The data is just user credentials for an API.
like image 283
Oguz Bilgener Avatar asked Jun 09 '14 11:06

Oguz Bilgener


People also ask

How do I share data between iOS apps?

Use a shared app group to share data/files between two/more apps or containing apps. An app group creates a secure container that multiple processes can access. Normally, each process runs in its own sandbox environment, but an app group lets both processes share a common directory.

What is iOS share extension?

Share Extension is an easy way that Apple provides to share contents (images, audio, files, etc.) from one app to another, even made by different developers.

Can iPhone apps access other apps data?

But in general, sandboxing means that apps can't access other apps, their data, or their files. An app has only access to its own folder (sandbox). Therefore the focus is on apps, and, while apps do work with files under the hood, you don't usually see them.


2 Answers

Here is how I did it:

  1. Open your main app target > Capabilities > App Groups set to on
  2. Add a new app group and make sure it is ticked (e.g. group.com.seligmanventures.LightAlarmFree)
  3. Open your watch target (the one with Capabilities tab) > App Groups set to on
  4. Add a new app group and make sure it is ticked (e.g. group.com.seligmanventures.LightAlarmFree - but must be the same name as group above)
  5. Save data to the group as follows:

    var defaults = NSUserDefaults(suiteName: "group.com.seligmanventures.LightAlarmFree") defaults?.setObject("It worked!", forKey: "alarmTime") defaults?.synchronize() 
  6. Retrieve data from the group as follows:

    var defaults = NSUserDefaults(suiteName: "group.com.seligmanventures.LightAlarmFree")  defaults?.synchronize()  // Check for null value before setting if let restoredValue = defaults!.stringForKey("alarmTime") {     myLabel.setText(restoredValue) } else {     myLabel.setText("Cannot find value") } 
like image 41
Charlie Seligman Avatar answered Sep 28 '22 02:09

Charlie Seligman


You should use NSUserDefaults like this:

Save data:

objc

NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.yougroup"]; [shared setObject:object forKey:@"yourkey"]; [shared synchronize]; 

swift

let defaults = UserDefaults(suiteName: "group.yourgroup") defaults?.set(5.9, forKey: "yourKey") 

Read data:

objc

NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.yougroup"]; id value = [shared valueForKey:@"yourkey"]; NSLog(@"%@",value); 

swift

let defaults = UserDefaults(suiteName: "group.yourgroup") let x = defaults?.double(forKey: "yourKey") print(x) 

This will work fine!

like image 142
foogry Avatar answered Sep 28 '22 01:09

foogry