One of the most interesting new features of iOS 7 is the AirDrop functionality. AirDrop allows users to send files between devices without the need for an Internet connection by establishing a direct connection between two devices using the built-in Bluetooth and WiFi protocols. This makes it very easy and very fast to exchange data between devices and can be a valuable competitive advantage for the apps that need to transfer files between iOS devices.
While sending files over AirDrop is quite easy, receiving them is a bit more involved but nothing to be afraid of. Let’s look at some example code to see how AirDrop support can be added to your app:
To enable your own app to have the option to share files over AirDrop, you need use the system provided class
UIActivityViewController * activities = [[UIActivityViewController alloc] initWithActivityItems:@[_url]
_url is the file URL of the file that you would like to send. After initizalization, the activities controller is presented modally. If there are any other devices with AirDrop turned on nearby, the activity controller will automatically show them and handle the file transfer for you.
That’s it! No, really, you don’t need to do more to send files over AirDrop from your app!
In order to be able to receive files over AirDrop there are two steps that you need to implement:
- Declare your app as an handler for the desired files types.
- Handle the received AirDrop file in your app delegate
If we want our app to be recognized as a handler for a certain file type we have to declare it as such by adding the corresponding UTI in our Info.plist. In Xcode 5 this can be achieved by going to the Info tab under our build target and adding our desired UTI under “Document Types”. One important caveat here is that some UTIs are reserved for the system. For instance if you declare your app as a handler for
public.png this will not be honoured by the system at run time because those UTIs seem to be reserved for Apple’s own Camera and Photo apps.
After you have declared your desired UTIs, you need to implement the method
application:openURL:sourceApplication:annotation: in your application delegate. When the system routes an AirDrop file to your app, this method will get called and the url will point to a file in your app’s
/Documents/Inbox directory. This directory is a special directory where your app has only read permissions. If you need to modify the file you must move it to another directory first. Another point to pay attention to is, that the files in this directory can be encrypted using data protection. In the normal case the files inside your apps documents directory are freely available but in some cases, by the time this method gets called the user could have already locked the device. Therefore it is a good idea to check if the file is readable first by using the
protectedDataAvailable property of the application object which gets passed to this delegate method as the first parameter. Apple documentation recommends to save the AirDrop URL for later and to return
YES from this method even if the file could not be accessed due to data protection.
If you would like to get up to speed on further new technologies introduced in iOS 7 such as UIKit Dynamics, Multipeer Connectivity, Custom View Controller Transitions and much more, check out our book Developing an iOS 7 Edge, which I wrote with an amazing group of top-notch iOS developers.