0

I have an iOS app which uses on-device database. I want to access the database from my App Extension. The database access requires a path which I insert into the NSUserDefaults as shown below. I first run the App Target which triggers the following code.

 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
    [userDefaults setValue:databasePathFromApp forKey:@"DatabasePath"];
    [userDefaults synchronize];

Then I run the Todays Extension which accesses the NSUserDefaults set by the Main App.

 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];

    _databasePath = [userDefaults valueForKey:@"DatabasePath"];

The _databasePath is always nil. What am I doing wrong? Do I have to use App Groups to share database between the app and the todays extension.

john doe
  • 9,220
  • 23
  • 91
  • 167
  • possible duplicate of [Sharing data between an iOS 8 share extension and main app](http://stackoverflow.com/questions/24118918/sharing-data-between-an-ios-8-share-extension-and-main-app) – tbaranes Apr 24 '15 at 20:26

1 Answers1

0

You should use NSUserDefaults like this following and make sure you must have enabled app group in your provisional profile and app group must configure as a green symbol and it should add to your provisional profile & BundleID.

NSUserDefaults *sharedUserDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.yougroup"]; [sharedUserDefault setObject:object forKey:@"yourkey"]; [sharedUserDefault synchronize];

NSUserDefaults *sharedUserDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.yougroup"]; sharedUserDefault value = [sharedUserDefault valueForKey:@"yourkey"];

Hitesh Vaghela
  • 1,635
  • 1
  • 11
  • 11
  • Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, _tailor your answers to the question_. – kleopatra Oct 12 '15 at 12:46