I have checked the official links for doc support, and it's clear that docx is not supported in UIWebview.
But, I also found out that some guys were able to open docx files in UIWebview:
Cannot open docx file through webbrowser in app using OpenIn functionality
Why doc and docx losing style information in iOS?
How to open Microsoft Office documents in iPhone
Also, afaik, iOS safari browser is built upon UIWebview and I am able to open docx files from internet in the browser.
However, when I download a docx from my test server (I have cross checked the downloaded docx by importing it from simulator and it opens perfectly on my mac), I am unable to view it in UIWebview.
I am confused,
Is docx Supported or not?
Or it seems that docx has further variety of formats out which some are supported?
Here is my loading code:
NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];
Yes UIWebView supports the docx. Just tried loading a docx file from bundle and it seems working fine(in this example named "DOC1.docx")::
NSString *path = [[NSBundle mainBundle] pathForResource:#"DOC1" ofType:#"docx"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mywebView loadRequest:request];
and If you are trying to display a docx file residing on a server somewhere, you can simply load it to your web view directly:
NSURL *targetURL = [NSURL URLWithString:#"http://www.central.wa.edu.au/Current_Students/JobsCareersandLeavingCentral/Documents/Resume%20Template.docx"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mywebView loadRequest:request];
Don't know why but using the URL scheme to load docx didn't work, (below code didn't work)
NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];
Instead, loading the file in memory(using NSData) and loading the data with MIME type worked (the below code worked like charm!)
NSString *path = [urlFileInView path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
webViewForDocsView.delegate = self;
[webViewForDocsView loadData:data MIMEType:#"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:#"UTF-8" baseURL:nil];
Using UIWebView to display select document types
As per the Apple documentation, docx isn't supported by UIWebView and is deprecated.
To open docx in app use UIDocumentInteractionController.
DispatchQueue.main.async {
let docOpener = UIDocumentInteractionController.init(url: fileURL)
docOpener.delegate = self
docOpener.presentPreview(animated: true)
}
Related
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
I am able to successfully view a PDF from a website.
The code I am currently using:
PDFAddress = [NSURL URLWithString:#"http://www.msy.com.au/Parts/PARTS.pdf"];
request = [NSURLRequest requestWithURL:PDFAddress];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
However, I have added that pdf in my bundle resources and I want user to download it from there, by doing so, there is no network required. I wonder how it could be done?
Make a url that points to your resource bundle
NSURL *url = [[NSBundle mainBundle] URLForResource:#"myPDF" withExtension:#"pdf"];
I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com
Since OS 3.0 UIWebView supports RTF files.
If you have a .rtf file, it's easy enough to load it into a uiwebview, e.g.
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
When loading HTML stored in a string variable, you can use loadHTMLString.
How would one load RTF stored in a string directly into a UIWebView? For example, if you have a string containing {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}....., using loadHTMLString will render it literally.
I would like to be able to render RTF from a string variable without having first to save it to a file.
Try loadData:MIMEType:textEncodingName:baseURL:. Haven't tried it, but it's worth a shot.
Hi i want to make a few http request and generate html from within my ipod app. I hear i want a WebView to view the html as if it were a browser. So my question is how do i make the http request? How do i parse json (or xml) and how do i save/load data? I figure i can use webstorage to load/save the data however i am thinking since its an ipod app i have a better way of storing it?
Here's a snippet to load a local html web page contained inside your app bundle:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"mywebpage" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Another alternative is to embed a local web server running in a background thread, and use a URL to localhost.
A UIWebview supports HTML5 local storage.
Here is the working code snippet. for iPod/iPhone/iPad;
Works with iOS7 and iOS6.. I have not tested in others...
UIWebView *_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];