使用iOS 8应用程序组在两个应用程序之间共享数据(使用NSUserDefaults) [英] Share datas between two apps with iOS 8 App Groups (using NSUserDefaults)

查看:534
本文介绍了使用iOS 8应用程序组在两个应用程序之间共享数据(使用NSUserDefaults)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我们是否可以在具有新iOS 8功能的应用程序之间共享数据:应用程序组(使用NSUserDefaults) - 或者如果应用程序组仅在主应用程序及其扩展程序之间共享数据?

I wonder if we can share datas between apps with the new iOS 8 feature : App groups (using NSUserDefaults) - Or if App Groups only share datas between the main app and its extension?

我实际上在两个应该共享数据的应用程序上启用了应用程序组功能(它们属于同一家公司)。它们也有相同的App Groups(比如group.com.company.myApp)。

I actually enabled the App Groups feature on both of the apps that should share datas between them (they belong the same company). They also have the same App Groups thing (like group.com.company.myApp).

这是第一个代码(在Swift中)

Here's the code on the first one (in Swift)

    NSUserDefaults.standardUserDefaults().setBool(true, forKey:"Bool")
    NSUserDefaults.standardUserDefaults().synchronize()

这是第二个代码(在Objective-C中)

And here's the code on the second one (in Objective-C)

    NSUserDefaults *datas = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.company.myApp"];
    NSLog(@"%@", [datas boolForKey:@"Bool"]);

可悲的是,Bool总是返回nil。

Sadly, the Bool always returns nil.

如果有人有解决方案:)

If anyone has a solution :)

谢谢

推荐答案

在Apple开发者论坛上查看此主题:
https://devforums.apple.com/message/977151# 977151

Check out this thread on the Apple Developer Forums: https://devforums.apple.com/message/977151#977151

我相信两个实例都需要使用组ID(使用initWithSuiteName :)初始化,否则它们正在读/写不同的用户默认值集。

I believe that both instances need to use the group ID (initializing with initWithSuiteName:) or else they are reading/writing to different user defaults sets.

所以你的Swift代码会改为:

So your Swift code would change to:

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
userDefaults.setBool(true, forKey: "Bool")
userDefaults.synchronize()

这篇关于使用iOS 8应用程序组在两个应用程序之间共享数据(使用NSUserDefaults)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆