Search code examples
objective-csaveios8ios-app-extensioncustom-keyboard

Storing data in custom keyboard


I am building a custom keyboard that learns the way you type for smarter auto-correct. In order to learn...

I need to be able to STORE data to the users device. I've tried using NSFileManager with NSDocumentsDirectory but nothing is getting saved int he AppExtension. (I tested the code (copy paste) in a regular app (non-app-extension) and it worked). I even enabled "Requests Open Access" in the .plist and re-installed the keyboard... still wouldn't save data.

Is there a way to store data in an app-extension?


Possible solutions I've pondered:

•Maybe creating a contact in the users address book that has my info in it (if app-extensions are allowed to do that), but a user might be suspicious as to why my app is requesting permission to modify their contact's address book).

•Displaying a hidden UIWebView that uses javascript injection to store and read data Safari Javascript Database, but I'm afraid this data might be erased if cache is ever cleared.


Solution

  • edit: now this is no longer working for me on iDevice beta 5 (with requestOpenAccess enabled OR disabled)? But it still works on SIMULATOR beta 5? Hmmm

    __ original post below: _

    It turns out NSUserDefaults DOES save data, it just does not work on BETA 3 simulator, but did work on BETA 5 iDevice. RequestsOpenAccess does NOT need to be enabled!

    NSUserDefaults *defaults = [[NSUserDefaults alloc]initWithSuiteName:@"com.company.keyboard.uniqueCodeHere"];//uniqueCodeHere can be anything... we just have to make sure the SuiteName isn't equal to your info.plist bundle ID so we add a random uniqueCode to the end of this ID.
    [defaults setObject:@"myStringData" forKey:@"savedStrings"];
    [defaults synchronize];
    

    *Note, SuiteName Can NOT equal bundle ID in info.plist or it doesn't work for some reason...