Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Code Works on iOS 9 but not on iOS 8?

Tags:

One of my tabs (in my Tab-Based application) works on iOS 9, but does not work on iOS 8. Specifically, when trying to load data from a plist, I get the error shown below.

I have a "Planner" tab which saves entries into a plist. iOS 8 Error - reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'

Saving Code:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    plistPath = appDelegate.plistPathInDocument
    plistPath2 = appDelegate.plist2PathInDocument
    // Extract the content of the file as NSData
    let data:NSData =  NSFileManager.defaultManager().contentsAtPath(plistPath)!
    let data2:NSData = NSFileManager.defaultManager().contentsAtPath(plistPath2)!
    do{
        if(numOfViewWillAppear == 0)
        {
            if let x = NSKeyedUnarchiver.unarchiveObjectWithData(data2)
            {
                self.sortedSections = NSKeyedUnarchiver.unarchiveObjectWithData(data2) as! [String]
                self.sections = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Dictionary
            }
            numOfViewWillAppear++
        }
    }

And AppDelegate preparation code:

    func preparePlist()
{
    let rootPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]
    let url = NSURL(string: rootPath)
    plistPathInDocument = (url?.URLByAppendingPathComponent("planner.plist").path)!

    if !NSFileManager.defaultManager().fileExistsAtPath(plistPathInDocument){
        let plistPathInBundle = NSBundle.mainBundle().pathForResource("planner", ofType: "plist")!
        do {
            try NSFileManager.defaultManager().copyItemAtPath(plistPathInBundle, toPath: plistPathInDocument)
            print("plist copied")
        }
        catch{
            print("error copying plist!")
        }
    }
    else{
        print("plst exists \(plistPathInDocument)")
    }

}

Code in which I save items to the plist:

self.sections[todoItem.dueDate] = [Assignment(name: todoItem.name, dueDate: todoItem.dueDate)]
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
plistPath = appDelegate.plistPathInDocument
do{
  let sectionsData = NSKeyedArchiver.archivedDataWithRootObject(sections)
  sectionsData.writeToFile(plistPath, atomically: true)
}
like image 799
dannybess Avatar asked Jan 21 '16 03:01

dannybess


People also ask

How do I make an app compatible with an older version of iOS?

Download an older app version:Go to the Purchased screen. For iPhone the Purchase screen is in the Updates tab. Select the app you want to download. If a compatible version of the app is available for your version of iOS simply confirm that you want to download it.

Is iOS 9 still supported by Apple?

Apple ended support for all 9-11-year old iOS devices nearly 5 years, ago, in 2016.

What coding is used for iOS?

Swift is a robust and intuitive programming language created by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. It's designed to give developers more freedom than ever. Swift is easy to use and open source, so anyone with an idea can create something incredible.

Can iOS 9.3 be updated?

You can now download iOS 9.3 on your iPhone, iPad or iPod Touch. The latest version of Apple's mobile operating system will run on any iDevice from 2011 or later.


1 Answers

I'm not sure it will help but it's worth a try:

self.sections = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Dictionary

In the "saving code" in the above mentioned line, try using NSDictionary instead of Dictionary when running on iOS8. I think I read somewhere, they only introduced Dictionary class quite recently. I'm not certain of this and I'm kind of newbie when it comes to iOS but it's worth a try...it might work. If it works, just put the Dictionary line in a if #available(ios 9.0 *) condition.

like image 71
Ilan Kutsman Avatar answered Sep 23 '22 05:09

Ilan Kutsman