This is my first Question on this site. I have a problem that I cannot fix.
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 :
User can write a note (saved in UserDefaults) and open Notification Center and watch his notes in a TodayExtension.
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
}
}
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)
So my TodayExtension always says that I have no notes. Do you have any ideas why this problem happens ?
Thank you for your help ! :)