iPhone UIDocumentInteractionController PresentOpenInMenuFromRect 取消按钮没有响应

标签 iphone objective-c ipad sdk

在 iPhone4 和 iPad2(均在 iOS 4.3.5 中运行)中执行“presentOpenInMenuFromRect”时,我观察到不同的行为。在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

关于iPhone UIDocumentInteractionController PresentOpenInMenuFromRect 取消按钮没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7015153/

相关文章:

ios - 如何暂停自己应用程序中来自另一个应用程序的音乐?

ios - 如何使用 WKWebView 正确实现身份验证质询?

ios - UIActivityViewController 打印对话框 UI 故障(错误?)

iphone - 了解崩溃日志 - KERN_PROTECTION_FAILURE

ios - swift 错误 : value of optional type 'Double?' not unwrapped

iphone - 在 iphone 中预览圆形图像

iphone - 在标签栏 View 之前查看

objective-c - NSDocument 与 sqlite 记录

html - 如何更改 loadedHtml 文本中的字体颜色

ipad - 如何使用 UIImage(contentsOfFile :String) with image data downloaded from the internet?