Serialize and deserialize world map objects for file storage or network transmission.
Framework
- ARKit
Overview
ARWorld
conforms to the NSSecure
protocol, so you can convert a world map to or from a binary data representation using the NSKeyed
and NSKeyed
classes.
Saving an ARWorldMap to a file URL
func writeWorldMap(_ worldMap: ARWorldMap, to url: URL) throws {
let data = try NSKeyedArchiver.archivedData(withRootObject: worldMap, requiringSecureCoding: true)
try data.write(to: url)
}
Loading an ARWorldMap from a file URL
func loadWorldMap(from url: URL) throws -> ARWorldMap {
let mapData = try Data(contentsOf: url)
guard let worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: mapData)
else { throw ARError(.invalidWorldMap) }
return worldMap
}
To send an ARWorld
to another device, creating a shared frame of reference for multiuser AR experiences:
On one device, use
NSKeyed
as in Listing 1 to convert the world map to a data object. (You don't need to write the data to a file to send it over the network.)Archiver Use the networking technology of your choice to send the resulting data to another device. (For example, in a MultipeerConnectivity session, call
send(_:
to send data, and implementto Peers: with:) MCSession
methods on the other device to receive data.)Delegate On the receiving device, use
NSKeyed
as in Listing 2 to instantiate anUnarchiver ARWorld
from the data.Map