【问题标题】:App Group within Embedded Framework Does Not Share嵌入式框架内的应用程序组不共享
【发布时间】:2015-02-08 00:32:03
【问题描述】:

我正在尝试使用包含在嵌入式框架中的通用模块在我的 iOS 和 WatchKit 扩展之间共享数据。公共模块类与应用组共享 NSUserDefault 中保存的数据。

但是,从 WatchKit 扩展读取时,看不到我在 iOS 应用中写入 App Group 共享的内容。

我在两个目标中启用了具有相同组 ID 的应用组。 iOS 读取和写入没有错误,如果 Watch 写入数据,它也会正确读取数据 - 但两者都看不到对方的写入。

任何想法如何解决非常感谢。

public class ShareManager() {
    private let sharedKey = “kungFuShare"

    public var kungfuFighters:Array<KungFu> = [];

    public init() {
        readFromShare()
    }

    public func readFromShare() {
        let myData = NSUserDefaults(suiteName: "group.com.27feathers.kungfu"
        if let rawData: NSData = myData?.objectForKey(sharedKey) as? NSData
        {                     
            var myData: AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(rawData);
            self.kungfuFighters = myData as? [KungFu] ?? []
        }
    }

    public func writeToShare() {
        let myData = NSUserDefaults(suiteName: "group.com.27feathers.kungfu")
        let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.kungfuFighters)
        myData?.setObject(saveData, forKey: sharedKey)
        myData?.synchronize() // same behavior with or without this
    }
}

public class KungFu: NSObject, NSCoding {
    public var fighterName:String

    public init(fighterName:String) {
        self.fighterName = fighterName
    }

    required public init(coder: NSCoder) {
        self.fighterName = coder.decodeObjectForKey("fighterName")! as String
        super.init()
    }

    public func encodeWithCoder(coder: NSCoder) {
       coder.encodeObject(self.fighterName, forKey: "fighterName")
    }
}

【问题讨论】:

    标签: ios swift frameworks watchkit ios-app-group


    【解决方案1】:

    在简化到最基本的级别后,在 iOS 应用中将 Bool 保存到 NSUserDefaults,然后尝试在 Watch 中读取,我发现读取仍然失败。

    这促使我在开发者中心注册了一个新的应用组。

    然后,我将 iOS 和 Watch 目标更改为新的 App Group。使用新的 App Group 后,一切正常!我现在可以在 iPhone 应用程序中书写并在 Watch 中阅读。

    我不知道为什么第一个应用程序组不起作用,因为我在创建第二个应用程序组时遵循了完全相同的步骤。也许这个 beta 6.2 版本的 Xcode 有一些问题

    【讨论】:

    • 我发现第一次添加应用组非常麻烦且无证。你应该提交一个错误报告,我很乐意复制它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多