Home:ALL Converter>How to send and receive custom data with AirDrop

How to send and receive custom data with AirDrop

Ask Time:2016-09-04T01:02:53         Author:Greg

Json Formatter

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?

Author:Greg,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39309338/how-to-send-and-receive-custom-data-with-airdrop
ccjensen :

The AirDrop sample code covers how to define your own file type/UTI and use it to send your custom data using AirDrop. \n\nThe main required parts are as follows.\n\n\nThe following keys and values in your app's Info.plist\n\n<...>\n<key>CFBundleDocumentTypes</key>\n<array>\n<dict>\n <key>CFBundleTypeName</key>\n <string>AirDrop Profile File Type</string>\n <key>LSHandlerRank</key>\n <string>Default</string>\n <key>LSItemContentTypes</key>\n <array>\n <string>com.apple.customProfileUTI.customprofile</string>\n </array>\n</dict>\n</array>\n<...>\n\nTo support receiving your custom type\nIn the app delegate, handle being launched with a fileURL by implementing \n\nObjective-C\n\n- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation\n\n\nSwift\n\nfunc application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {\n // Check if your app can open the URL\n // If it can, do something with the url and options, then return true\n // otherwise return false\n}\n\n\nMake sure to remove/move the file to clean up in the inbox folder, where recieved documents go (inside your app's sandbox).\nTo support sending the custom type\nThe 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 an object that conforms to the UIActivityItemSource protocol and returns something of NSData type in\n\n- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController\n\n\nand your actual NSData blob in \n\n- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType\n\n\nThen in the following method you'll want to return the UTI of your custom type that you registered in your app's Info.plist.\n\n- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType\n\n\n\nThe 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.",
2016-09-04T06:16:54
yy