3

I'm trying to make an Action Extension for my app where the user can add his current location along with some other data. I debugged the extension after sharing a location with Apple Maps App and found out that Maps sends four providers with following content:

  • a vCard of selected location
  • an Apple Maps URL for selected location
  • a plain text which is the name of selected location
  • a MKMapItem

All of the content above is in type of NSSecureCoding. Force cast to Data and initialising String with that data for vCard, to String for plain text and url succeed from NSSecureCoding, but I haven't found a way to create a MKMapItem object from the Data I receive.

Here's what I tried:

provider.loadItem(forTypeIdentifier: "com.apple.mapkit.map-item", options: nil) { (content, _) in
    let item = content as! MKMapItem

}

but it fails. I probably have to cast it to Data first but I couldn't find any initialiser for MKMapItem with Data

cameloper
  • 297
  • 1
  • 3
  • 16

1 Answers1

2

Use NSKeyedUnarchiver

itemProvider.loadItem(forTypeIdentifier: "com.apple.mapkit.map-item", options: nil) { (item, error) in

    guard let data = item as? Data else { return }

    do {
         guard let mapItem = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? MKMapItem else { return }
         print(mapItem)
    } catch {
         print("Error unarchiving mapItems, \(error)")
    }
Sarang Borude
  • 329
  • 1
  • 12