I have file.xlsx in my IOS app documents folder. I want to show open this excel file in UIWebview. but i am getting below error,
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted"
but pdf and CSV files are opening, I am new to IOS and tried all possible things for it to work i guess from last 2 days. nothing worked out.. please help me
Update: Even if i rename it as file.xls its not opening below is my code,
NSURL* nsUrl = [NSURL URLWithString:url];
_urlReq = [NSURL URLWithString:url];
[self performSelector:@selector(urlRequestForFile) withObject:nil afterDelay:0];
_webView.delegate = self;
NSURLRequest* request = [NSURLRequest requestWithURL: nsUrl];
[_webView loadRequest: request];
-(void)urlRequestForFile{
self.connection = nil;
NSURLRequest *requestForFile = [NSURLRequest requestWithURL:_urlReq cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:300];
_connection = [[NSURLConnection alloc]initWithRequest:requestForFile delegate:self startImmediately:YES];
_ongingServiceFlag = YES;
}
need help in showing xlsx file inside my IOS app either using UIWebView or is there any other way to show xlsx file inside the app without using third party apps?
Update(Solution):
I am very surprised to see that there is no support for XLSX mentioned even in apple site for UIWebView but actually UIWebView completely supports XLSX format. one thing you need to make sure is to specify the correct 'textEncodingName' value. if your file is stored with base64 binary encoding u have to mention it as textEncodingName:@"base64" otherwise u have to mention as "utf-8"
Below line worked for me:
[webView loadData:urlData MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"base64" baseURL:nil];
Use QLPreviewController for showing xlsx document in your app. Find the links for the tutorial below.
https://developer.apple.com/documentation/quicklook/qlpreviewcontroller http://iosdevelopertips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html https://www.appcoda.com/quick-look-framework/
The .XLSX filetype is based on openXML which is good news! This means it's easily readable, we just need to let the webview know the type, or rather mimeTYPE, of the file we are loading/displaying. According to microsoft the mimetype to use for XLSX (OpenXML) files is:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
source: https://blogs.msdn.microsoft.com/dmahugh/2006/08/08/content-types-for-open-xml-documents/
To do this we load the data (dataWithContentsOfFile:
or dataWithContentsOfURL:
or your prefered method) by calling the webView method:
[_webView loadData:<#(nonnull NSData *)#> MIMEType:<#(nonnull NSString *)#> textEncodingName:<#(nonnull NSString *)#> baseURL:<#(nonnull NSURL *)#>]
Example of my working code:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://mycoolserver.com/file.xlsx"]];
[_webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"utf-8" baseURL:nil];
.xlsx
File cannot be opened using UIWebView
. Though you can load .xls
file using UIWebView
.
Here is a list of files which you can load using UIWebView
https://developer.apple.com/library/content/qa/qa1630/_index.html
If you want to use .xlsx file
, you have to use QuickLook FrameWork
which contains QLPreviewController
. Your code should be like this -
- (void) initQlController{
QLPreviewController *prev = [[QLPreviewController alloc]init];
prev.delegate = self;
prev.dataSource = self;
[self presentModalViewController:prev animated:YES];
[prev.navigationItem setRightBarButtonItem:nil]; }
Then you have to use the dataSource methods for the same : -
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With