Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send and Receive a File Using AirDrop

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!

like image 385
Jon Avatar asked Oct 04 '13 16:10

Jon


People also ask

How do I receive files through AirDrop?

How to accept Airdrop. When someone shares something with you using AirDrop, you see an alert with a preview. You can tap Accept or Decline. If you tap Accept, the AirDrop will come through within the same app it was sent from.

Can AirDrop send files?

If anything, transferring files between two smartphones should be quick and simple. One such service is AirDrop which allows users to move data of any sort from one iOS device to another.

Where do files sent via AirDrop go on iPhone?

AirDrop is often used as a quick and easy way to share files between Apple devices. On an iPhone, the files you receive through AirDrop will go to the app that is associated with their file type, and on a Mac, they will go to your Downloads folder.


2 Answers

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]; 
like image 67
Greg Avatar answered Sep 18 '22 10:09

Greg


In iOS 7, Apple has introduce the new technology called AirDrop to share the data with nearby other iOS devices. AirDrop uses Bluetooth to scan for nearby devices. When a connection is established via Bluetooth, it’ll create an ad-hoc Wi-Fi network to link the two devices together, allowing for faster data transmission. It doesn’t mean you need to connect the devices to a Wi-Fi network in order to use AirDrop. Your WiFi simply needs to be on for the data transfer.

The UIActivityViewController class available in iOS 7 SDK makes it easy to integrate this feature. Use below code to integrate AirDrop sharing feature in your iOS app.

- (NSURL *)generateFileURL:(NSString*)filename
{
      NSArray *fileComponents = [filename componentsSeparatedByString:@"."];
      NSString *filePath = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];

      return [NSURL fileURLWithPath:filePath];
}

- (IBAction) shareButtonClicked:(UIButton *)button
{
      NSString * fileName = @"testImage.png";     // @"myFile.pdf"
      NSURL *url = [self generateFileURL:fileName];

      UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];

      // you can exclude certain types of activities. You can just display the AirDrop activity by excluding all other activities. 
      NSArray *excludedActivities = @[UIActivityTypePostToWeibo, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo];
      activityViewController.excludedActivityTypes = excludedActivities;

      [self presentViewController:activityViewController animated:YES completion:^{ }];
}
like image 30
Himanshu Mahajan Avatar answered Sep 18 '22 10:09

Himanshu Mahajan