13

I wonder if we can share datas between apps with the new iOS 8 feature : App groups (using NSUserDefaults) - Or if App Groups only share datas between the main app and its extension?

I actually enabled the App Groups feature on both of the apps that should share datas between them (they belong the same company). They also have the same App Groups thing (like group.com.company.myApp).

Here's the code on the first one (in Swift)

    NSUserDefaults.standardUserDefaults().setBool(true, forKey:"Bool")
    NSUserDefaults.standardUserDefaults().synchronize()

And here's the code on the second one (in Objective-C)

    NSUserDefaults *datas = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.company.myApp"];
    NSLog(@"%@", [datas boolForKey:@"Bool"]);

Sadly, the Bool always returns nil.

If anyone has a solution :)

Thanks

Andrew
  • 15,357
  • 6
  • 66
  • 101
FabienP
  • 143
  • 1
  • 7
  • Hey folks - if you are going to downvote, please specify why. Thank you – netpoetica Jun 24 '14 at 15:18
  • iOS 8 is under NDA so we can't talk about it here. Use Apple dev forums instead. – Neal Ehardt Jun 24 '14 at 15:47
  • 9
    @NealEhardt: "You will not be bound by the foregoing confidentiality terms with regard to technical information about pre-release Apple Software and services disclosed by Apple at WWDC (Apple’s Worldwide Developers Conference), except that You may not post screen shots, write public reviews or redistribute any pre-release Apple Software or services" – bjtitus Jun 24 '14 at 15:48
  • Have you tried initializing your first User Defaults using initWithSuiteName? I don't believe they are the same set. Also, are you sure your group ID is right? I don't believe it should have the app's individual name on it. – bjtitus Jun 24 '14 at 15:52
  • 1
    @bjtitus Thanks for correcting me. I think this was not the case for iOS 7 or prior. – Neal Ehardt Jun 24 '14 at 15:57
  • @bjtitus I'll try to change the group ID name by the company name. Get back to you as soon as possible – FabienP Jun 24 '14 at 16:03
  • possible duplicate of [Communicating and persisting data between apps with App Groups](http://stackoverflow.com/questions/24015506/communicating-and-persisting-data-between-apps-with-app-groups) – Andrew Jun 25 '14 at 03:29
  • If this is asked one more time I am going to cry. – Andrew Jun 25 '14 at 03:29

2 Answers2

10

Check out this thread on the Apple Developer Forums: https://devforums.apple.com/message/977151#977151

I believe that both instances need to use the group ID (initializing with initWithSuiteName:) or else they are reading/writing to different user defaults sets.

So your Swift code would change to:

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
userDefaults.setBool(true, forKey: "Bool")
userDefaults.synchronize()
bjtitus
  • 4,231
  • 1
  • 27
  • 35
1

App Groups no longer work in WatchOS2. You must use the watch connectivity Framework.

in your iOS App:

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    if (WCSession.isSupported()) {
        let session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }

    do {
        let applicationDict = ["key" : "value"]
        try WCSession.defaultSession().updateApplicationContext(applicationDict)
    } catch {
        // Handle errors here
    }
}

}

In your Watch OS2 App:

import WatchKit
import WatchConnectivity
import Foundation

class InterfaceController: WKInterfaceController, WCSessionDelegate {

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
    print(applicationContext)
}
DavidNorman
  • 1,421
  • 1
  • 17
  • 22