ios - 我们可以从 App2 访问 App1 的 NSUserDefaults 吗?

标签 ios objective-c iphone nsuserdefaults

我有两个应用:App1 和 App2。

在 App1 中,我将一个键 tokenvalue 保存到 NSUserDefaults

我需要在 App2 中获得与在 App1 中相同的 tokenvalue。除了使用钥匙串(keychain)之外,是否有任何可能获得该值(value)?

最佳答案

您必须使用App Groups。这将使两个应用程序都保存到共享的 NSUserDefaults 和文件夹中。

在 Xcode 中,单击您的项目文件夹项目名称 -> DesiredTarget -> Capabilities -> App Group,打开它并创建关联的应用程序组。

对 App1 和 App2 执行此过程。

来自文档:https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

// Create and share access to an NSUserDefaults object
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.example.domain.MyShareExtension"];

// Use the shared user defaults object to update the user's account
[mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];

关于ios - 我们可以从 App2 访问 App1 的 NSUserDefaults 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40107106/

相关文章:

ios - Cocos2d - 平铺 map 错误

ios - 将字符串日期长格式转换为 12 小时日期格式

ios - 使用WKWebView获取嵌入网页的视频url

iphone - 应用程式可在iOS 6上运作,但无法在iOS 5上运作

ios - Facebook Paper - 应用程序的哪些部分使用了哪些第 3 方库?

iphone - 在后台线程上接收 CLLocation 更新

iphone - 如何修复 "unrecognized selector sent to instance"

ios - 用于 ios 应用程序的 Objective-c SDK 分析加载应用程序的活跃用户数

ios - 创建 ViewController 时,无法删除 "also create a xib"

iphone - 在 Objective-C 中分配/初始化实例变量的正确方法?