Sharing UserDefaults between extensions

You cannot use UserDefaults.standard to share data between a host app and its app extension. You instead have to create a shared container with UserDefaults(suiteName:) to share data. Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s … Read more

SwiftUI: What is @AppStorage property wrapper

AppStorage @AppStorage is a convenient way to save and read variables from UserDefaults and use them in the same way as @State properties. It can be seen as a @State property which is automatically saved to (and read from) UserDefaults. You can think of the following: @AppStorage(“emailAddress”) var emailAddress: String = “[email protected]” as an equivalent … Read more

Sharing data between an iOS 8 share extension and main app

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”) … Read more

How can I use UserDefaults in Swift?

ref: NSUserdefault objectTypes Swift 3 and above Store UserDefaults.standard.set(true, forKey: “Key”) //Bool UserDefaults.standard.set(1, forKey: “Key”) //Integer UserDefaults.standard.set(“TEST”, forKey: “Key”) //setObject Retrieve UserDefaults.standard.bool(forKey: “Key”) UserDefaults.standard.integer(forKey: “Key”) UserDefaults.standard.string(forKey: “Key”) Remove UserDefaults.standard.removeObject(forKey: “Key”) Remove all Keys if let appDomain = Bundle.main.bundleIdentifier { UserDefaults.standard.removePersistentDomain(forName: appDomain) } Swift 2 and below Store NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: “yourkey”) NSUserDefaults.standardUserDefaults().synchronize() Retrieve var returnValue: [NSString]? … Read more