6

任何人都有如何将 AirDrop 添加到应用程序以从文档文件夹发送和接收文件的任何示例?我正在尝试在 iPhone 和 iPad 应用程序之间共享 SQLite 数据库。我做了很多研究,看起来 AirDrop 是要走的路,但我在弄清楚如何做时遇到了问题。

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

谢谢!

4

4 回答 4

7

您无需在设备之间建立连接。您只需使用类似这样的代码呈现 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]; 
于 2013-10-05T00:12:01.923 回答
1

在 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:^{ }];
}
于 2015-12-19T06:52:45.640 回答
0

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

// 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
}];
于 2017-02-14T12:06:48.337 回答
0

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

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

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

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

NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                    UIActivityTypePostToWeibo,
                                    UIActivityTypeMessage, UIActivityTypeMail,
                                    UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                    UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                    UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                    UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
于 2016-05-18T14:04:25.100 回答