Sharing data between an iOS 8 share extension and main app

后端 未结 8 462
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 20:08

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:

<
相关标签:
8条回答
  • 2020-11-28 20:35

    In my scenario I'm sharing data between the parent iOS app and WatchKit. I'm using Xcode 6.3.1, iOS Deployment Target 8.3

    var defaults = NSUserDefaults(suiteName: "group.yourappgroup.example")
    

    In your viewDidLoad make sure you synchronize:

        override func viewDidLoad() {
        super.viewDidLoad()
    
        defaults?.synchronize()
    
    }
    

    Example of a button sending text but of course you can pass whatever:

     @IBAction func btnSend(sender: UIButton) {
        var aNumber : Int = 0;
        aNumber = aNumber + 1
    
        //Pass anything with this line
        defaults?.setObject("\(aNumber)", forKey: "userKey")
        defaults?.synchronize()
    
    }
    

    Then on the other side make sure app group matches:

     var defaults = NSUserDefaults(suiteName: "group.yourappgroup.example")
    

    Then synchronize and cal: (In this case "lblNumber" is an IBOutlet label)

    defaults?.synchronize()
    var tempVar = defaults!.stringForKey("userKey")!;
    lblNumber.setText(tempVar);
    

    Then if you wanted to set something on this side and sync it back then just do the same thing and synchronize and just make sure the stringForKey that you call on the other side is the same:

    defaults?.setObject("sending sample text", forKey: "sampleKey")
    defaults?.synchronize()
    

    Hope this makes sense

    0 讨论(0)
  • 2020-11-28 20:36

    If you have

    group.yourappgroup
    

    use

    var defaults = NSUserDefaults(suiteName: "yourappgroup")
    

    This works for me

    0 讨论(0)
  • 2020-11-28 20:48

    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")
      }
      
    0 讨论(0)
  • 2020-11-28 20:51

    I translated in swift foogry's answer and it works!!

    Save data:

    let shared = NSUserDefaults(suiteName: "nameOfCreatedGroup")
    shared("Saved String 1", forKey: "Key1")
    shared("Saved String 2", forKey: "Key2")
    

    Read data:

    let shared = NSUserDefaults(suiteName: "nameOfCreatedGroup")!
    valueToRead1 = shared("Key1") as? String
    valueToRead2 = shared("Key2") as? String
    println(valueToRead1)   // Saved String 1
    println(valueToRead2)   // Saved String 2
    
    0 讨论(0)
  • 2020-11-28 20:52

    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!

    0 讨论(0)
  • 2020-11-28 20:52

    So apparently it works, only when the group name is used as the suite name for NSUserDefaults.

    The documentation says NSUserDefaults.standartUserDefaults() should also work but it does not, and this is probably a bug.

    0 讨论(0)
提交回复
热议问题