Swift 3/iOS 10/TodayExtension-UserDefaults始终返回nil [英] Swift 3 / iOS 10 / TodayExtension - UserDefaults always returns nil

查看:170
本文介绍了Swift 3/iOS 10/TodayExtension-UserDefaults始终返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在此网站上的第一个问题. 我有一个无法解决的问题.

This is my first Question on this site. I have a problem that I cannot fix.

我正在开发一个具有今天扩展名的简单便笺应用程序.我在Swift 2.2和iOS 9中没有问题.问题仅出现在iOS 10的Swift 2.3和Swift 3中. 我遇到的问题如下:

I'm working on an easy note application with today extension. I have had no problem in Swift 2.2 and iOS 9. Problem just appears in Swift 2.3 and Swift 3, on iOS 10. The problem I have is the following :

用户可以编写便笺(保存在UserDefaults中),然后打开通知中心",并在TodayExtension中观看他的便笺.

User can write a note (saved in UserDefaults) and open Notification Center and watch his notes in a TodayExtension.

我有这些方法可以将注释保存在UserDefaults中并从UserDefaults中检索(我使用的是组,因此在我的应用程序和扩展程序的功能中,一切设置都很好-此外,我的对象当然需要NSCoding方法):

I have these methods to save notes in UserDefaults and retrieve it from UserDefaults (I'm using groups, so in Capabilities of my application and my extension, everything is set well - Furthermore, my objects have required NSCoding methods, of course) :

open class NoteManager: NSObject {

    private static let kKEY: String! = "Notes"
    private static let kSUITENAME: String! = "Team.group.bundleIdentifier"

    open static func saveNotes(_ notes: [Note]) {
        let notesData = NSKeyedArchiver.archivedData(withRootObject: notes)

        let defaults = UserDefaults(suiteName: kSUITENAME)!
        defaults.set(notesData, forKey: kKEY)
        defaults.synchronize()
    }

    open static func retrieveNotes() -> [Note] {
        let notesFromDefault = UserDefaults(suiteName: kSUITENAME)!.object(forKey: kKEY)
        var returnedNotes: [Note]! = [Note]()

        if notesFromDefault != nil {
            if let notesData: Data? = notesFromDefault as! Data! {
                NSKeyedUnarchiver.setClass(Note.self, forClassName: "Application_Name.Note")
                NSKeyedUnarchiver.setClass(Preference.self, forClassName: "Application_Name.Preferences")
                let unarchivedNotes = NSKeyedUnarchiver.unarchiveObject(with: notesData!) as? [Note]

                if let notes: [Note] = unarchivedNotes! as [Note]! {
                    returnedNotes = notes
                }
            }
        }

        return returnedNotes
    }
}

在iOS应用程序中,我没有任何问题. UserDefaults与此代码配合良好. 但是,当我打开TodayExtension时,以下行(在restoreNotes()中)始终返回nil:

In the iOS Application I have NO problems. UserDefaults works well with this code. But when I open the TodayExtension the following line (in retrieveNotes()) always returns nil :

let notesFromDefault = UserDefaults(suiteName: kSUITENAME)!.object(forKey: kKEY)

因此,我的TodayExtension总是说我没有笔记. 您是否知道为什么会发生此问题?

So my TodayExtension always says that I have no notes. Do you have any ideas why this problem happens ?

谢谢您的帮助! :)

推荐答案

我部分解决了我的问题.

I partially solved my problem.

我已经完全删除了Xcode和所有设置(缓存文件等), 然后我删除了套件名称中的团队"部分.

I have completely remove Xcode and all the settings (caches files etc...), then I have removed the "Team" part in the suite name.

现在,我在Xcode控制台中收到了警告,但可以在TodayExtension中检索我的笔记.皮奥夫!

Now I have like a warning in the Xcode console but I can retrieve my notes in my TodayExtension. Piouf !

我仍然对WatchKit Extension有问题.我不确定,但是我想我已经读过,即使与App Groups一起使用的UserDefaults也不会与Apple Watch Extensions共享.

I still have the problem for WatchKit Extension. I'm not sure but I think that I have read that UserDefaults even with App Groups are not shared with Apple Watch Extensions.

但是Apple在其文档中写道:此外,iOS会自动将iOS应用偏好的只读副本转发给Apple Watch.您的WatchKit扩展可以使用NSUserDefaults对象读取这些偏好,但无法直接更改为默认偏好数据库."

But Apple write in their documentation "Also, iOS automatically forwards a read-only copy of your iOS app’s preferences to Apple Watch. Your WatchKit extension can read those preferences using an NSUserDefaults object, but it cannot make changes directly to the defaults database."

所以我不知道为什么无法从Apple Watch检索笔记.

So I don't know why I cannot retrieve my notes from my Apple Watch.

至少我已经解决了我的第一个问题.如果有人有相同的话.

At least I have solved my first problem. If anybody has the same.

这篇关于Swift 3/iOS 10/TodayExtension-UserDefaults始终返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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