Reputation: 71852
I am trying to change my custom keyboard background from host app but some reason it does not work.
I tried NSUserDefaults
but it doesn't work, here is my code when button is pressed from host app:
func buttonTapAction() {
NSUserDefaults.standardUserDefaults().setInteger(1, forKey: "abc")
}
And this way I am reading values from disk but it always execute else condition:
override func viewDidLoad() {
super.viewDidLoad()
let abc = NSUserDefaults.standardUserDefaults().integerForKey("abc")
if abc == 1 {
self.view.backgroundColor = UIColor.redColor()
}else{
self.view.backgroundColor = UIColor(red: 241.0/255, green: 235.0/255, blue: 221.0/255, alpha: 1)
}
//Other code
}
I don't know what I am doing wrong.
HERE is my full project.
Upvotes: 2
Views: 1018
Reputation: 16864
Enable App Groups for both targets
Step 1.
Same steps and group name should be same for NuberWidget Target Keep in mind.
Step 2.
Step 3.
Step 4.Code for store value
Objective C
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.yourcompany.TodayExtensionSharingDefaults"];
[sharedDefaults setInteger:[self.textField.text integerValue] forKey:@"MyNumberKey"];
[sharedDefaults synchronize]; // (!!) This is crucial.
Swift
let appGroupID = "group.yourcompany.TodayExtensionSharingDefaults"
let defaults = NSUserDefaults(suiteName: appGroupID)
defaults.setInteger(1, forKey: "MyNumberKey")
Step 5.
Objective C
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.TodayExtensionSharingDefaults"];
NSString* colornumber= [defaults objectForKey:@"MyNumberKey"];
Swift
let appGroupID = "group.yourcompany.TodayExtensionSharingDefaults"
let defaults = NSUserDefaults(suiteName: appGroupID)
let number = defaults.integerForKey("MyNumberKey")
Step 6. Last but not list is don't forgot to enable "Allow Full Access" Enable. Goto Keyboard -> info.plist -> NSExtention -> NSExtensionAttributes
With this you can solve your problem.
Upvotes: 2