iOS add share extension on maps

2019-03-16 21:40发布

问题:

I want to add share extension on Apple's Maps, does anyone know how to do. I try to set NSExtensionAttributes as below but it don't work, my APP doesn't show in the Maps's share sheet.

NSExtensionAttributes

NSExtensionActivationRule
  NSExetnsionActivationSupportsWebURLWithMaxCount
  NSExetnsionActivationSupportsWebPageWithMaxCount

回答1:

I'm not sure why NSExtensionActivationSupportsText doesn't work with Maps, but I get the same result when I try.

What you need is a more complex activation rule. Set the type of the activation rule to "string", and set up the value using the SUBQUERY format described in the App Extension Programming Guide. When you do that you can request one or more specific UTIs. Maps will provide plain text (public.plain-text), which should be equivalent to NSExtensionActivationSupportsText but apparently is not. It also provides a location card (public.card) and a URL (public.url).

An activation rule that checks for any of these by the UTIs would look like

SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text").@count >= 1).@count >= 1 OR SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, SUBQUERY($attachment.registeredTypeIdentifiers, $uti, $uti UTI-CONFORMS-TO "public.url" AND NOT $uti UTI-CONFORMS-TO "public.file-url").@count >= 1).@count >= 1).@count >= 1 OR SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.vcard").@count >= 1).@count >= 1

That's just three SUBQUERY clauses that check for each of those UTIs, OR-ed together.

Depending on what data you can handle, you might want to reduce that to cover only UTIs that your extension knows how to deal with. For example if all you want is the URL, only use that part:

SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, SUBQUERY($attachment.registeredTypeIdentifiers, $uti, $uti UTI-CONFORMS-TO "public.url" AND NOT $uti UTI-CONFORMS-TO "public.file-url").@count >= 1).@count >= 1).@count >= 1

This version just checks that you're getting a URL which is not a file URL.

Maps provides an Apple Maps URL which will be something like http://maps.apple.com/?q=37.332331,-122.031219&sll=37.332331,-122.031219

If you use the vcard UTI, you'll get an NSString encoded into an NSData. If you decode it, it looks something like this:

BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//iOS 8.2//EN
N:;Shared Location;;;
FN:Shared Location
item1.ADR;type=HOME;type=pref:;;;;;;
item2.URL;type=pref:http://maps.apple.com/?q=37.332331\,-122.031219&sll=37.332331\,-122.031219
item2.X-ABLabel:map url
END:VCARD