开发者

Open file viewer (PDF) from iOS application

We have a code to download a PDF from the server using a SOAP protocol.

We store the file on the Documents folder of the iOs device, and the we want to allow the user to open the file (usually PDF's files)

But, at least on the simulator, the application didn't open the file successfully.

Of course, I can open manually the file (in order to check that the file has been downloaded perfectly)

// Get the documents folder where write the content
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [do开发者_JS百科cumentsDirectory stringByAppendingPathComponent:documentDownloaded.fileName];

if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFileCompletePath]]){
    NSString *filePath = [[NSString alloc] initWithFormat:@"%@", [self getFileCompletePath]];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];

    if ([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL: url];
    } else {
        NSLog(@"Not supported application to open the file %@", filePath);
    }

    [url release];
    [filePath release];
}

The file full path in the simulator is:

/Users/User/Library/Application Support/iPhone Simulator/4.3.2/Applications/87800296-2917-4F26-B864-B20069336D1D/Documents/0000907.pdf

Anybody knows if is something wrong?

The file could be and spreadsheet, pdf, image, document ... but for the first work around will be great just for pdf's.

Thank you,


omz you was right. The final solution was the next.

My controller implemented the UIDocumentInteractionControllerDelegate

When the user wants to open the document the code was the next:

    NSString *filePath = [[NSString alloc] initWithFormat:@"%@", [self getFileCompletePath]];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
    UIDocumentInteractionController *documentController = [self setupControllerWithURL:url 
                                                                         usingDelegate:self];

    [documentController retain];
    [documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
    [url release];
    [filePath release];

Using the next method, you could found it on the apple developer library

- (UIDocumentInteractionController *) 
    setupControllerWithURL: (NSURL *) fileURL
    usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

    UIDocumentInteractionController *interactionController =
      [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}

Finally we include the delegate methods:

#pragma mark methods for the UIDocumentInteractionControllerDelegate

- (void)previewDocumentWithURL:(NSURL*)url
{
    UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
    preview.delegate = self;
    [preview presentPreviewAnimated:YES];
    [preview retain];
}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [controller autorelease];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.view.frame;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
}

Thank you,


You cannot open file URLs with UIApplication's openURL: method. Because of sandboxing, other apps could not access any files within your application's Documents folder.

You need to use UIDocumentInteractionController to open documents that you download or create in other applications.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜