UIActivityViewController - 保留从URL读取的附加文件的文件名

时间:2013-07-06 09:42:24

标签: ios xcode email pdf uiactivityviewcontroller

iOS6 UIActivityViewController是否有相同的代码(下面的代码)?问题是,我可以通过URL共享数据并保留文件名吗?截至目前,我可以弄清楚如何做到这一点的唯一方法是将PDF转换为NSData并将数据添加为activityItem。工作正常,但我丢失了附加的pdf的名称。

[composer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"myPDF.pdf"];

2 个答案:

答案 0 :(得分:6)

是的,我认为这是可能的,试试这个

NSString *str = [[NSBundle mainBundle] pathForResource:@"AppDistributionGuide" ofType:@"pdf"]; 
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", [NSURL fileURLWithPath:str]] applicationActivities:nil];

答案 1 :(得分:0)

请参考以下方法来实现此目的。保留从URL读取的附加文件的文件名:

dispatch_async(dispatch_get_main_queue(), ^ {
    NSData *fileData;
    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [documentDir stringByAppendingPathComponent:@"filename.pdf"];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.com/filename.pdf"]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error) {
            NSLog(@"Download Error:%@",error.description);
        }
        if (data) {
            fileData = data;
            [data writeToFile:filePath atomically:YES];
            NSLog(@"File is saved to %@",filePath);
        }
    }];
    if(fileData && filePath) {
            UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[NSURL fileURLWithPath:filePath]] applicationActivities:nil];
            [self presentViewController:activityViewController animated:YES completion:nil];
    }
});