3

UserDefaults are not working in my App. Please find the below code under AppDelegate file.

let sharingData = UserDefaults.init(suiteName: "group.macuser79.xxx");
sharingData?.set("vincenzo", forKey:"username");
sharingData?.synchronize();

In the InterfaceController of the app to Watch, to be able to retrieve the value so I did this:

override func awake(withContext context: Any?) {
let sharingData = UserDefaults.init(suiteName: "group.macuser79.xxx");

let username = sharingData?.object(forKey: "username");
    print("Value username \(username)");
}

Please let me know, what I'm doing wrong!

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
macuser
  • 567
  • 2
  • 6
  • 16
  • Alternate solution : Use GenericKeychain. https://developer.apple.com/library/content/samplecode/GenericKeychain/Introduction/Intro.html – Poles Nov 14 '16 at 13:09
  • 3
    First thing to look at is make that `group.macuser79.xxx` group is defined in project's capabilities tab in both targets. – Otávio Nov 14 '16 at 13:11
  • @Otávio. Hello, I checked if the group is defined in both the target and have been defined. Have been defined both within the target of the iPhone, both within the target Watch, is defined inside the Watch Extensions and it all seems ok. I've also updated the provisioning profile but unfortunately nothing. – macuser Nov 14 '16 at 13:34
  • Hmm, this code is a direct port from my code written in Objective-C so I can't spot any mistake. Is `sharingData` object not nil in both cases? – Otávio Nov 14 '16 at 13:51
  • @Otávio I'm sorry, but I didn't think it was your code, in fact I have found many examples where it is shown how to write the code. However, the object is not null in both cases. – macuser Nov 14 '16 at 14:06
  • I did not mean that you ported it from my code. Just wanted to point out that my code and your code are pretty much the same so I can't clearly see what you are doing wrong. – Otávio Nov 14 '16 at 14:16
  • @Otávio If you want I can put all of your code, both the AppDelegate both as a way to recover the information inside of the app's WatchKit, but I believe that it is so trivial the thing, that I can not understand what mistake. I think that is a problem of the simulator, in fact I noticed that the requests to a remote URL are null, or are a long time, about 10 minutes, while the same request is made with the simulator iPhone is very fast – macuser Nov 14 '16 at 14:49
  • Sorry for the confusion but I did not mean I have ownership of the code, not at all. I just wanted to **compare** your implementation to mine, and they both **happen** (Totally by chance and due being a trivial code) to be the same. No need to remove any code. As you said, this code is even inside Apple's sample. – Otávio Nov 14 '16 at 14:52
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128072/discussion-between-macuser-and-otavio). – macuser Nov 14 '16 at 15:07

3 Answers3

0

In Swift3 UserDefaults made much smarter to obtained stored value. In below line, you are storing String value without specifying it!:

sharingData?.set("vincenzo", forKey:"username");

So, in order to get that, you need to write like below:

let username = sharingData?.string(forKey: "username");
    print("Value username \(username)");
}

This is much more better context in getting values based on you Store.

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • Hello, thanks for your answer but unfortunately it does not work. I've corrected my code as I have indicated, but unfortunately the value is always nil. I have also started the app on the simulator of iPhone but unfortunately no, it does not work. – macuser Nov 14 '16 at 13:22
  • Hello, I did some research on the internet and in this link the user is having the same problem that I am having I also. http://stackoverflow.com/questions/39808319/swift-3-ios-10-todayextension-userdefaults-always-returns-nil It should be a problem of the simulator of the apple Watch. Now how do I resolve so that I can try to test my app ? I was left with only one out and then I can publish. Thanks, Vincenzo – macuser Nov 15 '16 at 10:19
0

Try this instead:

let defaults = UserDefaults.standard 
defaults.set("group.macuser79.xxx", forKey:"username")
defaults.synchronize()

To access the store:

let defaults = UserDefaults.standard
let username = defaults.string(forKey: "username")

I admit that I haven't tried to init() my own UserDefaults instance before, but the standard instance of it, which is made into a singleton by iOS, is good practice to use.

Also, don't forget to unwrap the optional properly.

dokun1
  • 2,120
  • 19
  • 37
  • Hi, I tried with your instructions but unfortunately it is always nil – macuser Nov 14 '16 at 13:30
  • Apologies! Read this for some more information: http://stackoverflow.com/questions/27025526/passing-data-to-apple-watch-app/32002403#32002403 – dokun1 Nov 14 '16 at 13:50
  • Hello, I did some research on the internet and in this link the user is having the same problem that I am having I also. http://stackoverflow.com/questions/39808319/swift-3-ios-10-todayextension-userdefaults-always-returns-nil It should be a problem of the simulator of the apple Watch. Now how do I resolve so that I can try to test my app ? I was left with only one out and then I can publish. Thanks, Vincenzo – macuser Nov 15 '16 at 10:18
0

To set:

UserDefaults(suiteName: "group.macuser79.xxx")?.set("vincenzo", forKey: "username")

To access:

UserDefaults(suiteName: "group.macuser79.xxx")!.string(forKey: "username")
Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
  • Hello, I tried to do what I have indicated but unfortunately it does not work. At this point I think there is a problem with the simulator because I don't know what else to think. I'll try a Apple Watch to a friend to figure out if this is the problem or not. Thanks for your availability. Vincenzo – macuser Nov 15 '16 at 10:09
  • Hello, I did some research on the internet and in this link the user is having the same problem that I am having I also. http://stackoverflow.com/questions/39808319/swift-3-ios-10-todayextension-userdefaults-always-returns-nil It should be a problem of the simulator of the apple Watch. Now how do I resolve so that I can try to test my app ? I was left with only one out and then I can publish. Thanks, Vincenzo – macuser Nov 15 '16 at 10:19
  • Hello, I did some research on the internet and in this link the user is having the same problem that I am having I also. http://stackoverflow.com/questions/39808319/swift-3-ios-10-todayextension-userdefaults-always-returns-nil It should be a problem of the simulator of the apple Watch. Now how do I resolve so that I can try to test my app ? I was left with only one out and then I can publish. Thanks, Vincenzo – macuser Nov 15 '16 at 10:19