Send and Receive a File Using AirDrop
Asked Answered
A

4

7

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!

Anosmia answered 4/10, 2013 at 16:47 Comment(0)
T
8

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]; 
Thurlow answered 5/10, 2013 at 0:12 Comment(1)
UIActivityViewController is a great convenience, but I would like to send the files programmatically. I want myDeviceApp to scan for all devices that have AirDrop enabled, get UUID of each device and send concurrently or synchronously a picture or video to all devices. 1. How can I get UUID of each device that is in range of myDeviceApp? 2. How can I send data programmatically to all devices?Monkhood
L
1

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:^{ }];
}
Lorenzen answered 19/12, 2015 at 6:52 Comment(1)
+1 because sharing a file's URL is significantly faster and displays the appropriate metadata as well as sharing options (e.g. "Add Tags")Septuplicate
P
0

If you save the file to the documents directory, you will need to change the URL from above.

 NSURL *url = [NSURL fileURLWithPath:[self dataFilePath]];

-(NSString *)dataFilePath {
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"myFile.txt"];
}

Also, I found there was a very long delay (1-2 minutes) if I didn't exclude more activities:

NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                    UIActivityTypePostToWeibo,
                                    UIActivityTypeMessage, UIActivityTypeMail,
                                    UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                    UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                    UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                    UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
Pucka answered 18/5, 2016 at 14:4 Comment(0)
F
-1

UIImage *image = imageView.image; NSArray *items = @[image];

// build an activity view controller
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];

// and present it
[self presentViewController:controller animated:YES completion:^{
    // executes after the user selects something
}];
Fabria answered 14/2, 2017 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.