How to send and receive custom data with AirDrop

主宰稳场 提交于 2019-12-04 16:46:43

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:

  1. 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> <...>

  1. 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.

  2. 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!