问题
I've been struggling with this for ages now as I can't find any detailed examples.
In my app I have an array of custom data that I want to send to another user with the same app, via AirDrop.
The first step is sending the data:
@IBAction func share_Button_Click(sender: UIBarButtonItem)
{
let dataToShare: NSData = getMyCustomNSData()
let controller = UIActivityViewController(activityItems: [dataToShare], applicationActivities: nil)
controller.excludedActivityTypes = [UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToFlickr, UIActivityTypePostToTencentWeibo, UIActivityTypeMail, UIActivityTypeAddToReadingList, UIActivityTypeOpenInIBooks, UIActivityTypeMessage]
self.presentViewController(controller, animated: true, completion: nil)
}
This converts my data to an NSData object, the user gets the AirDrop share option, and of the data goes to another phone. So far so good...
But how does the other user's app know how to receive it?
I've read about custom UTI types and have declared one, but to be honest I don't know what to put in the declaration. And how do you indicate to iOS that the data you are sending conforms to this particular UTI?
There are AirDrop examples here and there online, but they focus on sharing common types like images, and no one I have found has worked through sharing a custom data type in detail.
Can anyone help?
回答1:
The AirDrop sample code covers how to define your own file type/UTI and use it to send your custom data using AirDrop.
The main required parts are:
- App's info.plist
<...>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>AirDrop Profile File Type</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.customProfileUTI.customprofile</string>
</array>
</dict>
</array>
<...>
To support receiving your custom type: in the app delegate, handle being launched with a fileURL by implementing
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
. Make sure to remove/move the file to clean up in the inbox.To support sending one the custom type: the item you pass to UIActivityViewController should either be a fileURL to a file with the extension that you registered as your custom file type, or if an object that conforms to the UIActivityItemSource protocol and returns something of NSData type in
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
, and your actual NSData blob in- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
. Then in- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType
you want to return the UTI of your custom type that you registered in the info.plist.
The sample code has a great example of how to do all of this, and also how to make the entire UX better by including a properly sized and cropped preview photo.
来源:https://stackoverflow.com/questions/39309338/how-to-send-and-receive-custom-data-with-airdrop