WKWebView loadData alternative for displaying NSData in iOS 8 (Except UIWebView) - ios

I would like to know if there are any alternatives to the loadData (iOS 9 only) function of WKWebView without using it's older brother UIWebView.
Maybe there's a way to display document files (xlsx,docx,pptx,pdf,images) using the loadHTMLString like how I display audio or video files in my code below.
It has to be able to accept NSData because the file is encrypted at all times in the disk, so loadRequest is not possible
Below is my code for viewing files:
if ( mimeType == kMimeTypeVideo || mimeType == kMimeTypeAudio ) {
NSString *contentType = [MIMETypeToFileExtViceVersaConverter convertFileExtensionToMimeType:shareWithObject.fileExtension];
NSString *base64String = [fileData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *str = [NSString stringWithFormat:#" \
<html><head></head><body style=\"margin:0;width:100%%;height:100%%;\">\
<video controls height=\"100%%\" width=\"100%%\"> \
<source type=\"%#\" src=\"data:%#;base64,%#\"> \
</video></body></html>", contentType, contentType, base64String];
[_webView loadHTMLString:str baseURL:[[NSBundle mainBundle] resourceURL]];
} else {
NSString *contentType = [MIMETypeToFileExtViceVersaConverter convertFileExtensionToMimeType:shareWithObject.fileExtension];
[_webView loadData:fileData MIMEType:contentType characterEncodingName:#"UTF-8" baseURL:[[NSBundle mainBundle] resourceURL]];
}

There are no other way as of now than to use UIWebView.
And to add to the troubles of using the loadData function of WKWebView, it has very limited support in the files that it can view, like how it can't load doc/docx and ppt/pptx files.
In conclusion, don't use WKWebView if you are thinking of making it the default viewer of the your NSData converted documents.

Related

Can't load javascript in UIWebView

I'm trying to load a javascript file but it doesn't work.
My code :
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><script type=\"text/javascript\" src=\"http://mywebsite.com/file.js\"></script></body></html>"];
[_webView loadHTMLString:html baseURL:nil];
This code is supposed to display a video, but nothing happens.
This code displays correctly the video in Desktop browser, my JS is correct.
use url in baseURL
NSURL *base=[NSURL URLWithString:#"website.com"];
Do not use string description. Instead use:
[_webView loadHTMLString:html baseURL:nil];
Write following code in - webViewDidFinishLoad:
NSString * strJavaScript = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://mywebsite.com/file.js"] encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:strJavaScript];

+URLWithString: returns nil

Im using QLPreviewController to view word, excel,pdf etc. The problem is the file is in the server so I need to download it from a certain URL.
I'm using this code:
NSURL *dLurl = [NSURL URLWithString:[NSString stringWithFormat:#"%#",DLPathStr]];
NSData *data = [NSData dataWithContentsOfURL:dLurl];
for downloading files, It works for the PDF file but in the word and excel files it doesn't work I'm not able to download anything.
I fixed it by using this code it doesn't download anything because the URL is wrong because it contains spaces.
NSString *str = [DLPathStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *dLurl = [NSURL URLWithString:[NSString stringWithFormat:#"%#",str]];

Parse RTF in Objective c

I want to parse RTF file in Objective c.Is its possible to parse it? if is possible then how can I use it into IOS.I want all the text and formatting.
I have tried in so many way to get text and formatting for this purpose I have used UIWEBVIEW but its not return any text string
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"rtf"];
NSData *data = [NSData dataWithContentsOfURL:imgPath];
[webView loadData:data MIMEType:#"text/rtf" textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#"/"]];
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
Here html return nil value.
Thanks
Ok, this question is very old but in case someone stumbles across this problem:
The html is nil as the webView is not finished loading. Fetch the html in the UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
NSLog(#"Html: %#",html);
}
You will get the desired html.

UIWebview not fetching the linked CSS resource

I am trying to load a local html content into uiwebview. I already have the html content ready with me, and i am adding a reference a online css file to the content by the mechanism as shown in the code below. However, the problem i am facing is when i load the webview, there is no styling. Also, I verified in the Charles Proxy, there is no outgoing call for fetching iphone.css file to my base server. Ideally, while loading a html page, uiwebview should get all the referred resources, but for some reason unknown to me, it is not fetching the css file.
Please review and let me know if there is some issue with my code that i am not able to identify here?
NSString* htmlString = #"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"he\" lang=\"he\"><head><link type=\"text/css\" href=\"http://<base_server>/resources/iphone.css\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>%#</body></html>";
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
NSLog(#"%#",[NSString stringWithFormat:htmlString,entry.body]);
[webview loadHTMLString:[NSString stringWithFormat:htmlString,entry.body] baseURL:[NSURL URLWithString:#"http://<base_server>/"]];
[self addSubview:webview];
It's hard to tell if there are any typos in the HTML with all of the double quote escaping going on in that string. Why don't you pull the HTML out of the string and into a file, read that file in and place your content in the body. There may be a mistake with the markup in that string. This would allow you to read it easier in a seperate file.
Something like:
NSError *error;
NSStringEncoding encoding;
NSString *htmlFilePath = [[NSBundle mainBundle] pathForResource: #"Sample"
ofType: #"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFilePath
usedEncoding:&encoding
error:&error];
NSString *html = [NSString stringWithFormat:htmlString, entry.body];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webview loadHTMLString:html baseURL:baseURL];

Run HTML5/CSS3 in UIWebView

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

Resources