ios-sdk-examples icon indicating copy to clipboard operation
ios-sdk-examples copied to clipboard

iOS Example code: Download an offline map: swift deprecation warnings

Open jumbopilot opened this issue 6 years ago • 3 comments

Hi guys,

the iOS example https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/ shows warnings in Swift 4.2:

let context = NSKeyedArchiver.archivedData(withRootObject: userInfo) leads to warning: 'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String] leads to warning: 'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

Can someone help updating the example code to prevent such deprecation warnings?

jumbopilot avatar Dec 30 '18 10:12 jumbopilot

Thank you for flagging, @jumbopilot. We'll update this soon to remove the warnings, but it's worth noting here in the meantime that MGLOfflinePack's .context is meant to hold any arbitrary data you want associated with your offline payload (see the API documentation).

In other words, using NSKeyedArchiver is not a requirement for implementing an offline region in your app.

riastrad avatar Jan 07 '19 18:01 riastrad

Here are the replacements that work for me:

        let context = try NSKeyedArchiver.archivedData(withRootObject: userInfo, requiringSecureCoding: false)

        let userInfo = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSObject.self], from: pack.context) as? [String: String]

Zakalicious avatar May 07 '20 04:05 Zakalicious

If you run into the deprecation warning in the Offline swift example or in your own project...

'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead


  1. The Example Target, as of this writing, targets iOS 9.0. Changing the example code to fix the depcrecation may break that requirement.
  2. @Zakalicious's comments are helpful for replacements.
    • Side note: the Apple documentation states: To prevent the possibility of encoding an object that NSKeyedUnarchiver can’t decode, set requiresSecureCoding to true whenever possible.

roblabs avatar Mar 04 '21 18:03 roblabs