Opening Word,Excel, and PDF Files Without Using Uiwebview on iOS

Opening word,excel, and PDF files without using UIWebview on iOS

You have two options. For iOS 4.0 or later, you can use the QLPreviewController. You will need to implement the following two methods-

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller 
{
return 1; //assuming your code displays a single file
}

- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL fileURLWithPath:path]; //path of the file to be displayed
}

And initialize the QLPreviewController as follows-

QLPreviewController *ql = [[QLPreviewController alloc] init];
ql.dataSource = self;
ql.delegate = self;
ql.currentPreviewItemIndex = 0; //0 because of the assumption that there is only 1 file
[self presentModalViewController:ql animated:YES];
[ql release];

For older iOS versions, you can use the UIDocumentInteractionController in the following way. Implement the delegate method-

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

Init & display-

    UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;
[docController presentPreviewAnimated:YES];

Thanks,

Akshay

Opening files after download other than using webview

Have a look at UIDocumentInteractionController and the Quick Look Framework to see if it can be used for your purposes.

How to open a PDF file in my iPad/iPhone using my iOS application?

You can use UIwebview to load it. It is very simple. If you want more flexibility you should use Quartz framework classes.

EDIT:

To view downloaded PDF, you can provide open-in functionality in your app. This is how you add "open-in" to your app.

Look here for complete tutorial.

How to open Password Protected PDF/DOC in UIWebview without using a temp file?

Write file in local and read the data and show it in web view

//convert file data(ie : NSData) as string
NSData *fileData;//This is the data from your request
NSString *fileString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];

//get the file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"sample.pdf"];

//Save as file
NSError *error;
BOOL hasFileWritten = [fileString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

if(!hasFileWritten)
{
NSLog(@"Write file error: %@", error);
}

//open pdf in webview
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

Update
without writing the file data in local, Process the data in memory and show the pdf file in webview

@interface ViewController ()
{
IBOutlet UIWebView *webView;
CATiledLayer *tiledLayer;
CGPDFPageRef pageRef;
}
@end

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url);

BOOL success = CGPDFDocumentUnlockWithPassword(pdf, "test");

if(success)
{
pageRef = CGPDFDocumentGetPage(pdf, 1);

CGRect pageRect = self.view.frame;

tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
tiledLayer.levelsOfDetail = 1000;
tiledLayer.levelsOfDetailBias = 1000;
tiledLayer.frame = pageRect;

UIView *contentView = [[UIView alloc] initWithFrame:pageRect];
[contentView.layer addSublayer:tiledLayer];

[webView addSubview:contentView];
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, pageRef);
}

Is there any sdk or kit to handle microsoft office formats in iOS?

Actually there is an Library to open Excel files known as LibXLS

to display Word documents i would recommend you to user UIWebView as it can handle word files.

If you dont want to use UIWebView then you can use QLPreviewController for Word, PDF files and ppt files.

Tutorial can be found here

UIWebView Unable to read document error

The reason i found int these case was due to password protected or encrypted file,or any file format which webview can load.

UIWebView has these EXCEPTION SFUZipEndOfCentralDirectoryError: Could not find the end of central directory record when it encounter with above reason.

You can load document like this



Related Topics



Leave a reply



Submit