Send and Receive a File Using AirDrop

Jon picture Jon · Oct 4, 2013 · Viewed 7.6k times · Source

Anyone have any examples of how to add AirDrop to an app for sending and receiving a file from the documents folder? I am trying to share a SQLite database between to an iPhone and iPad app. I have done a lot of research and it looks like AirDrop is the way to go, but I am having problems figuring out how.

I know I need to open AirDrop using UIActivityViewController and that is not a problem but how do I establish the connection between the two devices? Any have a simple example that would help me get on the right track?

Thank you!

Answer

Greg picture Greg · Oct 5, 2013

You don't need to establish a connection between the devices. You just present the UIActivityViewController using code something like this, and when the user chooses the AirDrop option, it's taken care of for you.

NSString* text = @"Some text I want to share";
UIImage* image = [UIImage imageNamed:@"image.png"];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[text, image] applicationActivities:nil];
activityViewController.completionHandler = ^(NSString* activityType, BOOL completed) {
    // do whatever you want to do after the activity view controller is finished
};
[self presentViewController:activityViewController animated:YES completion:nil];