开发者

Check if it exists an app that can open a file before calling presentOpenInMenuFromRect

I have stored a file (pdf) in the "documents" directory and I would like to know how could be possible to detect if there are applications (app) installed in the mobile device that can open this file (for example, "pdf" files can be opened using "iBooks", "DropBox", ...). I would like to detect this before calling the method "presentOpenInMenuFromRect"; which shows a list of the possible applications that can handle a specific file. The desired behavior is:

1) Given a pdf stored in the "Document" directory, check if there are "app's" installed in the iPhone/iPad, which can open this file (iBooks, DropBox, ...). This is what I do not know how to do.

2) If no application in de iPhone/iPad can open the application, then do nothing, otherwise draw a "Save" button and then, if the user presses this "Save" button, then the "presentOpenInMenuFromRect" method will be called in order to show a list of possible app which can open that file. I know the way to present a list of of applications which can open a file; here are the source code:

The source code related to the "Save" button is:

- (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 = [NSDat开发者_开发技巧a 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
    }
}

Thanks in advance.

Note: The response time of calling the method "presentOpenInMenuFromRect" is about several seconds, so this is the reason why I would like to know if there is a another way to detect and get a list of possible app installed on the mobile device which can open a specific file (pdf, ...)


Check out ihasapp.

http://www.ihasapp.com/ it says that it is an iOS framework that lets developers detect apps that are currently installed on their users' devices.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜