简体   繁体   中英

Share User Object between App and App Extension in iOS8

I am using Pocket API and authenticating the user on the app.

While in the extension i want to use the logged in user to run methods to call various endpoints. The only option to share data between the app and its extension is NSUserDefaults as I see on various threads but NSUserDefaults cannot save NSObjects of logged in user.

How can I use the logged in user in the Extention ?

I am logging the user like this in the app..

[[PocketAPI sharedAPI] loginWithHandler: ^(PocketAPI *API, NSError     *error){
    if (error != nil)
    {   
            NSlog (@"User Logged In"); 
    }
}];
}

and in the extension i want to use :

[[PocketAPI sharedAPI] saveURL:url handler: ^(PocketAPI *API, NSURL *URL,
                                              NSError *error){
    if(error){
        NSLog(@"There was an error in saving the URL : %@ ", [error description]);


    }else{
        NSLog(@"URL was saved successfully ");

    }
}];

how do i dynamically save the [PocketAPI sharedAPI] to use any method on either side? How can i share the same instance of the object

For saving user credentials better store in Keychain, it is more secure and you can access in extension also.

  1. You can share keychain using kSecAttrAccessGroup Apple doc will explain it.
  2. Other option would be to share the core data or sqlite file in the shared container. I have done sample explanation in My Blog on Document Provider which will explain the sharing of data between app and the extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM