Search code examples
iosobjective-cuiwebview

how to open file.xlsx in UIWebView IOS using objective-c


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];

Solution

  • 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];
    

    Side-by-side comparison of origional file and webview rendered file