使用AirDrop发送和接收文件

时间:2013-10-04 16:47:10

标签: iphone objective-c file sharing airdrop

任何人都有任何关于如何将AirDrop添加到应用程序以从文档文件夹发送和接收文件的示例?我想在iPhone和iPad应用程序之间共享一个SQLite数据库。我已经做了很多研究,看起来AirDrop是可行的方法,但我在弄清楚如何解决问题。

我知道我需要使用UIActivityViewController打开AirDrop,这不是问题,但如何在两个设备之间建立连接?有没有一个简单的例子可以帮助我走上正确的轨道?

谢谢!

4 个答案:

答案 0 :(得分:6)

您无需在设备之间建立连接。您只需使用类似的代码呈现UIActivityViewController,当用户选择AirDrop选项时,它就会为您完成。

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]; 

答案 1 :(得分:0)

在iOS 7中,Apple推出了名为AirDrop的新技术,以便与附近的其他iOS设备共享数据。 AirDrop使用蓝牙扫描附近的设备。当通过蓝牙建立连接时,它将创建一个ad-hoc Wi-Fi网络,将两个设备连接在一起,从而实现更快的数据传输。这并不意味着您需要将设备连接到Wi-Fi网络才能使用AirDrop。您只需启用WiFi即可进行数据传输。

iOS 7 SDK中提供的UIActivityViewController类可以轻松集成此功能。使用以下代码在您的iOS应用中集成AirDrop共享功能。

- (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:^{ }];
}

答案 2 :(得分:0)

如果将文件保存到文档目录,则需要更改上面的URL。

$collection = Model::get();

foreach($collection as $c) {

$c->delete();

}

另外,如果我没有排除更多活动,我发现有很长的延迟(1-2分钟):

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

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

答案 3 :(得分:0)

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
}];