当在iPhone4和iPad2中执行"presentOpenInMenuFromRect"时(我在iOS 4.3.5中运行),我观察到不同的行为.在iPad2中,它会显示一个下拉列表,其中包含可以打开特定文件的所有应用程序,并且工作正常; 但是,在iPhone4中它也会显示一个下拉列表(也可以正常工作),但是在下拉列表的末尾有一个"取消"按钮,它似乎处于非活动状态.
在iPad2中,这个问题不会发生,因为没有出现"取消"按钮,只出现下拉列表,当我点击与下拉列表不同的另一个区域时,此列表关闭; 这是理想的行为.
我的意思是说iPhone/iPhone4中的"取消"按钮似乎处于非活动状态:当我触摸它时,没有任何反应!好吧,更具体地说,如果我在短时间内或时间内多次触摸它,那么,比较早,按钮"取消"似乎响应(它将颜色从灰色变为蓝色)然后下拉列表真的很封闭; 这是理想的行为.
我通过以下方式使用"presentOpenInMenuFromRect":我已经实现了一个"保存"按钮,默认情况下是隐藏的; 但是,在方法" - (void)webViewDidFinishLoad:(UIWebView*)webView"我检测到URL的"路径扩展",如果它有".pdf"扩展名,那么我显示"保存"按钮; 那是:
- (void)webViewDidFinishLoad:(UIWebView *)webView { if ([[[myWebViewOL.request.URL.absoluteString pathExtension] lowercaseString] isEqualToString:@"pdf"]) { saveFile0L.hidden = NO; } }
与"保存"按钮(saveFile0L)相关的"动作"是以下方法"saveFile":
[saveFile0L addTarget:self action:@selector(saveFile) forControlEvents:UIControlEventTouchUpInside];
其中"saveFile"具有以下代码:
- (void) saveFile { //Do not worry about "FileManager" is only a singleton class //which I have defined in order to implement some common methods //and one of them is "saveFile:(UIWebView*) withUIView:(UIView*) [[FileManager sharedInstance] saveFile:myWebView withUIView:self.view]; }
并且"saveFile:(UIWebView*)myWebView withUIView:(UIView*)self.view"具有以下代码:
- (void) saveFile:(UIWebView*)webView withUIView:(UIView*)view { NSString* fileName = [[NSFileManager defaultManager] displayNameAtPath:webView.request.URL.absoluteString]; NSURL* fileurl = [NSURL URLWithString:webView.request.URL.absoluteString]; NSData* data = [NSData dataWithContentsOfURL:fileurl]; NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* docsDirectory = [paths objectAtIndex:0]; NSString* filePath = [docsDirectory stringByAppendingPathComponent:fileName]; [data writeToFile:filePath atomically:YES]; NSURL* url = [NSURL fileURLWithPath:filePath]; //UIDocInteractionController API gets the list of devices that support the file type docController = [UIDocumentInteractionController interactionControllerWithURL:url]; [docController retain]; //Note:[docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES] //presents a drop down list of the apps that support the file type, //clicking on an item in the list will open that app while passing in the file. //Note: [[UIApplication sharedApplication] canOpenURL:fileurl] //will return YES if there is an app that can handle the specific file BOOL isValid = ([[UIApplication sharedApplication] canOpenURL:fileurl] && [docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]); if (!isValid) { [self showAlertSaveFileError:fileName]; } }
目前,我不知道如何使"取消"按钮正常工作,该按钮仅出现在下拉列表末尾的iPhone(不在iPad中)中.
提前致谢,
BOOL isValid = ([[UIApplication sharedApplication] canOpenURL:fileurl] && [docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]);
尝试view
inView:view
改为inView:self.view.window
.