当用户在设备上没有电子邮件帐户时,在 IOS 上共享文档时出现问题
Posted
技术标签:
【中文标题】当用户在设备上没有电子邮件帐户时,在 IOS 上共享文档时出现问题【英文标题】:Problems sharing document on IOS when the user doesn't have an email account on the device 【发布时间】:2017-06-16 10:50:07 【问题描述】:当我尝试使用 UIActivityViewController 通过电子邮件共享 pdf 文档时遇到问题。
当用户配置他的电子邮件共享工作正常时,它显示为附件的文档,但是当用户之前没有配置电子邮件帐户时,它会提示用户选择电子邮件提供商,如果我配置电子邮件帐户新电子邮件出现,但没有附加文件。如果我用配置的电子邮件重试,效果很好。
我在 ios 9 和 10 上对其进行了测试,它给了我同样的问题。
这是我在 Objective C 上的代码:
NSString *file = [NSString stringWithFormat:@"file://%@", items[0][@"source"]];
NSURLRequest *urlRequestToLoad = [NSURLRequest requestWithURL:[NSURL URLWithString:file] cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSArray *sharingItems = @[items[0][@"filename"], [urlRequestToLoad URL]];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[activityController setValue:@"Your email Subject" forKey:@"subject"];
[self.viewController presentViewController:activityController animated:YES completion:nil];
知道我做错了什么吗?
【问题讨论】:
我认为你所做的一切都很好,它只是这样工作。 您要共享本地文档吗? 本地文档。 【参考方案1】:是!简单的解决方案:) 下载并附加文件。如果您之前在 webview 中加载 PDF,请在下面找到评论。
-(void)print:(NSString*)subject withFileName:(NSString*)fileName webFileURL:(NSURL*)webURL
if ([UIActivityViewController class])
if (subject)
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *title = [fileName stringByReplacingOccurrencesOfString:@" " withString:@"_"];;
title = [NSString stringWithFormat:@"%@.pdf",title];
NSString * filePath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",title];
// Showing spinner
NSURLRequest *request = [NSURLRequest requestWithURL:webURL];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
// Close the spinner
if (error)
NSLog(@"Download Error:%@",error.description);
if (data)
[data writeToFile:filePath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^
/* Set this available field on the activity controller */
NSMutableArray *items = [NSMutableArray array];
if (data)
[items addObject:[NSURL fileURLWithPath:filePath]];
// Print webView
//
// [items addObject:[self.webViewInstance viewPrintFormatter]];
//
if (webURL)
[items addObject:[NSString stringWithFormat:@"%@ - %@",subject, [webURL absoluteString]];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[activityViewController setValue:subject forKey:@"subject"]; // Set the mail subject.
// If you are not handle it. It will crash in iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
activityViewController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popPC = activityViewController.popoverPresentationController;
popPC.sourceView = self.view;
popPC.sourceRect = kSharePopArea;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:activityViewController animated:YES completion:nil];
);
];
NSString *content = @"write text here";
NSError* lError;
NSMutableArray *items = [NSMutableArray array];
if ([NSData dataWithContentsOfURL:fileUrl options:NSDataReadingMappedAlways error:&lError]) // Local file
[items addObject:fileUrl];
if(content)
[items addObject:content];
if(imagename && [UIImage imageNamed:@"imagename"])
[items addObject:[UIImage imageNamed:@"imagename"]];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
// Add email subject.
[activityViewController setValue:@"Your email Subject" forKey:@"subject"];
// If you are not handle it. It will crash in iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
activityViewController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popPC = activityViewController.popoverPresentationController;
popPC.sourceView = self.view;
popPC.sourceRect = kSharePopArea;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:activityViewController animated:YES completion:nil];
【讨论】:
是本地文档,不需要下载。以上是关于当用户在设备上没有电子邮件帐户时,在 IOS 上共享文档时出现问题的主要内容,如果未能解决你的问题,请参考以下文章