简体   繁体   English

在iOS8中的App和App Extension之间共享用户对象

[英]Share User Object between App and App Extension in iOS8

I am using Pocket API and authenticating the user on the app. 我正在使用Pocket API并在应用程序上对用户进行身份验证。

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. 正如我在各种线程上看到的那样,在应用程序及其扩展名之间共享数据的唯一选项是NSUserDefaults,但NSUserDefaults无法保存已登录用户的NSObjects。

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? 如何动态保存[PocketAPI sharedAPI]在任一端使用任何方法? 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. 您可以使用kSecAttrAccessGroup共享钥匙串, Apple文档将对此进行解释。
  2. Other option would be to share the core data or sqlite file in the shared container. 另一种选择是在共享容器中共享核心数据或sqlite文件。 I have done sample explanation in My Blog on Document Provider which will explain the sharing of data between app and the extension. 我已经在“文档提供者上的我的博客”中完成了示例解释,该示例解释了应用程序和扩展之间的数据共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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