Часы iPhone и Apple не могут разархивировать файлы других - PullRequest
0 голосов
/ 18 февраля 2019

И iPhone, и Apple Watch, файл архивируется следующим образом в одном и том же объекте NSObject:

do {
            let data = try NSKeyedArchiver.archivedData(withRootObject: currentRoute!, requiringSecureCoding: false)
            try data.write(to: URL(fileURLWithPath: path!))
            print("save success")
        } catch {
            print("Couldn't write file")
        }

Часы Apple используют self.session?.transferFile(URL(fileURLWithPath: filePath!), metadata: ["name": fileName]) для отправки файла на iPhone, и файл получен, но не может быть разархивирован, поскольку он находится вApple watch.

    func session(_ session: WCSession, didReceive file: WCSessionFile) {


        let name:String = file.metadata!["name"] as! String
        // document directory
        let path = FileHelper.filePathWithName(name: name, isCache: false)

        if FileManager.default.fileExists(atPath: path!){
            do {
                try FileManager.default.removeItem(at: URL(fileURLWithPath: path!))
            }catch{
                print("cannot remove file")
            }
        }
        do{
                try FileManager.default.copyItem(at: file.fileURL, to: URL(fileURLWithPath: path!))
        }catch{
            print("cannot copy file")
        }


        let route = NSKeyedUnarchiver.unarchiveObject(withFile: path!) as? Route
        print(route) // it's nil

    }

Я также пытался вручную скопировать файл того же формата из каталога документов iPhone в Apple Watch, Apple Watch также не может разархивировать его.Но они могут открывать файлы, которые они создали.

Имеет ли NSKeyedArchiver определенный символ, который запрещает открывать другое приложение?


Я думаю, что проблема может быть в NSCoding в классе Route, которыйв архиве, он используется Apple Watch и iPhone:

class Route: NSObject, NSCoding, CLLocationManagerDelegate {

    public func encode(with aCoder: NSCoder) {
        aCoder.encode(startTime, forKey: "startTime")
        aCoder.encode(endTime, forKey: "endTime")
        aCoder.encode(locations, forKey: "locations")
    }

    let distanceFilter: CLLocationDistance = 10

    var startTime: NSDate
    var endTime: NSDate
    var locations: Array<CLLocation>

    override init() {

        startTime = NSDate()
        endTime = startTime
        locations = Array()
    }

    deinit {
//        println("deinit")
    }

    /// NSCoding

    required init?(coder aDecoder: NSCoder) {
        startTime = aDecoder.decodeObject(forKey: "startTime") as! NSDate
        endTime = aDecoder.decodeObject(forKey: "endTime") as! NSDate
        locations = aDecoder.decodeObject(forKey: "locations") as! Array
    }

//.........

}
...