在调用presentOpenInMenuFromRect之前,检查它是否存在可以打开文件的应用程序

时间:2011-08-23 10:43:49

标签: iphone objective-c sdk

我已将文件(pdf)存储在“documents”目录中,我想知道如何检测移动设备中是否安装了可以打开此文件的应用程序(app)(例如,可以使用“iBooks”,“DropBox”,...)打开“pdf”文件。我想在调用方法“presentOpenInMenuFromRect”之前检测到这个;它显示了可以处理特定文件的可能应用程序的列表。期望的行为是:

1)如果存储在“Document”目录中的pdf,请检查iPhone / iPad中是否安装了“app”,可以打开此文件(iBooks,DropBox,...)。这是我不知道该怎么做。

2)如果de iPhone / iPad中没有应用程序可以打开应用程序,则不执行任何操作,否则绘制“保存”按钮然后,如果用户按下此“保存”按钮,则“presentOpenInMenuFromRect”方法将为调用以显示可以打开该文件的可能应用程序列表。我知道提供可以打开文件的应用程序列表的方法;这是源代码:

与“保存”按钮相关的源代码是:

- (void) saveFile:(UIWebView*)webView 
{
    NSString* fileName  = [[NSFileManager defaultManager]  displayNameAtPath:webView.request.URL.absoluteString];
    #if DEBUG
    NSLog(@"<%p %@: %s line:%d> File name:%@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __PRETTY_FUNCTION__, __LINE__, fileName);
    #endif
    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]; //Very important, if "retain" is not called, the application crashes
    //Present 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.
    BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:webView animated:YES]; //Using "webView" instead of "self.view"
    if (!isValid)
    { 
        [self showAlertSaveFileError:fileName]; //Shows an alert message
    }
}

提前致谢。

注意:调用方法“presentOpenInMenuFromRect”的响应时间大约是几秒钟,所以这就是为什么我想知道是否有另一种方法来检测并获取移动设备上安装的可能应用程序列表的原因可以打开特定文件的设备(pdf,...)

1 个答案:

答案 0 :(得分:1)

查看ihasapp。

http://www.ihasapp.com/ 它说它是一个iOS框架,可以让开发人员检测当前安装在用户设备上的应用程序。