Reputation: 687
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!
Upvotes: 7
Views: 8904
Reputation: 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
}];
Upvotes: -1
Reputation: 489
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];
Upvotes: 0
Reputation: 4799
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:^{ }];
}
Upvotes: 1
Reputation: 33650
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];
Upvotes: 8