
这个工作正常,直到iOS 8.现在,文件已创建,我可以验证它包含正确的内容,出现打开菜单,我可以选择一个应用程序,委托方法运行并清理文件.但是,不是iOS切换到所选应用程序并将文件复制到之前,当我选择应用程序时,打开输入菜单就会关闭,并且不会复制文件.
如果我给UIdocumentInteractionController一个现有文件,这是有效的.如果我使用提供的fileData但将目标文件名更改为现有文件的文件名,它也可以工作.这表明存在权限问题 – 就像在iOS 8中使用UIdocumentInteractionController无法读取的默认权限创建新文件一样.
有谁知道发生了什么以及我如何解决它?
解决方法 看起来iOS 8中的 *** 作顺序略有变化.DIDdismissOpenInMenu曾经在文件发送完成后运行,但现在它在文件开始发送后运行.这意味着我的清理代码有时会在文件完成发送之前运行,不会发送任何文件.在注意到较小的文件被发送好后,我想出了这个;显然,在我的清理代码获得之前,对较小文件的处理已经完成,但是对较大文件的处理却没有.为了确保正确的计时,还要清理用户打开documentInteractionController时创建的文件,然后在不做任何事情的情况下解除控制器,我改变了我的方法:
@H_419_11@- (voID)openIn:(NSData *)fileData { // save the pdf data to a temporary file Nsstring *filename = [Nsstring stringWithFormat:@"%@.pdf",filename]; BOol result = [fileData writetofile:filePath atomically:TRUE]; if (result) { self.sendingfile = FALSE; NSURL *URL = [NSURL fileURLWithPath:filePath]; UIdocumentInteractionController *controller = [[UIdocumentInteractionController interactionControllerWithURL:URL] retain]; controller.delegate = self; [controller presentopenInMenuFrombarbuttonItem:self.openInbutton animated:TRUE]; }}- (voID)documentInteractionController:(UIdocumentInteractionController *)controller willBeginSendingToApplication:(Nsstring *)application { // the user chose to send the file,so we shouldn't clean it up until that's done self.sendingfile = TRUE;}- (voID)documentInteractionControllerDIDdismissOpenInMenu:(UIdocumentInteractionController *)controller { if (!self.sendingfile) { // the user dIDn't choose to send the file,so we can clean it up Now [self openInCleanup]; }}- (voID)documentInteractionController:(UIdocumentInteractionController *)controller dIDEndSendingToApplication:(Nsstring *)application { // the user chose to send the file,and the sending is finished,so we can clean it up Now [self openInCleanup]; self.sendingfile = FALSE;}- (voID)openInCleanup { // delete the temporary file Nsstring *filename = [Nsstring stringWithFormat:@"%@.pdf",filename]; [[NSfileManager defaultManager] removeItemAtPath:filePath error:nil];}iOS 11更新
在iOS 11之前, *** 作系统似乎保留了该文件的副本,直到接收应用程序完成读取,即使我的清理功能在文件从我的应用程序发出后立即运行.在iOS 11中,此更改并且接收应用程序无法读取文件,因为我的应用程序在完成之前将其删除.所以现在不是将临时文件保存到documents并使用openInCleanup方法立即删除它,而是将临时文件保存到tmp并在下次启动应用程序时清空tmp文件夹.此方法也适用于较旧的iOS版本.只需删除openInCleanup,将文档更改为路径中的tmp,然后将其添加到applicationDIDFinishLaunching:
@H_419_11@// clear the tmp directory,which will contain any files saved for Open InNsstring *tmpDirectory = [Nsstring stringWithFormat:@"%@/tmp",NSHomeDirectory()];NSArray *tmpfiles = [[NSfileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:NulL];for (Nsstring *tmpfile in tmpfiles) { [[NSfileManager defaultManager] removeItemAtPath:[Nsstring stringWithFormat:@"%@/%@",tmpDirectory,tmpfile] error:NulL];} 总结以上是内存溢出为你收集整理的iOS 8中动态生成的文件“Open In”失败全部内容,希望文章能够帮你解决iOS 8中动态生成的文件“Open In”失败所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)