ios-sdk-examples
ios-sdk-examples copied to clipboard
iOS Example code: Download an offline map: swift deprecation warnings
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?
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.
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]
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
- The Example Target, as of this writing, targets iOS 9.0. Changing the example code to fix the depcrecation may break that requirement.
- @Zakalicious's comments are helpful for replacements.
- Side note: the Apple documentation states: To prevent the possibility of encoding an object that
NSKeyedUnarchivercan’t decode, setrequiresSecureCodingto true whenever possible.
- Side note: the Apple documentation states: To prevent the possibility of encoding an object that