Skip to content

Instantly share code, notes, and snippets.

@jazzedge
Last active December 3, 2017 08:38
Show Gist options
  • Select an option

  • Save jazzedge/ad7b76bc88d4d8d8706f504cc931aaa5 to your computer and use it in GitHub Desktop.

Select an option

Save jazzedge/ad7b76bc88d4d8d8706f504cc931aaa5 to your computer and use it in GitHub Desktop.
See:https://stackoverflow.com/questions/39309338/how-to-send-and-receive-custom-data-with-airdrop
See:https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html
See:https://stackoverflow.com/questions/45154373/creating-custom-uti-for-use-with-airdrop-ios
See:https://stackoverflow.com/questions/43195134/how-do-i-import-and-read-a-file-in-my-ios-app
01. How to send and receive custom data with AirDrop
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]
if let popoverPresentationController = activityViewController.popoverPresentationController {
popoverPresentationController.barButtonItem = (sender as! UIBarButtonItem)
}
activityViewController.popoverPresentationController?.barButtonItem = (sender as! UIBarButtonItem)
present(activityViewController, 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?
02. How to define your own file type/UTI and use it to send your custom data using AirDrop.
You need to register the document types that your application can open with iOS. To do this you need to add a document type to your app’s Info.plist for each document type that your app can open. Additionally if any of the document types are not known by iOS, you will need to provide an Uniform Type Identifier (UTI) for that document type.
Stated another way, if your app defines a new file type. then you need to define that custom UTI in the UTExportedTypeDeclarations section of Info.plist.
This can be setup in Xcode on the Info tab of your app target under the Exported UTIs section or you can manually update Info.plist as shown below.
The CFBundleDocumentTypes is to declare what file types your app can open.
Here's a made up file type that happens to be a binary file with an extension of .fun.
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>My Custom Binary File</string>
<key>UTTypeIdentifier</key>
<string>com.mycompany.myapp.myfiletype</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>fun</string>
</array>
</dict>
</dict>
</array>
With that in place, you can also setup your CFBundleDocumentTypes so your app is offered as a choice to open such files:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>My Custom Binary File</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.mycompany.myapp.myfiletype</string>
</array>
</dict>
</array>
Note how the LSItemContentTypes value of CFBundleDocumentTypes must match the UTI's UTTypeIdentifier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment