Unable to get HTML string from UIWebView - ios

I loaded a file on uiWebView. Its extension type is ".pages". Now I trying to get HTML string from webview, but it returns to empty string.
For ".doc or .docx" files getting the String. It happens to ".pages" extension only.
Thanks in Advance,

NSString *html = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.outerHTML"];
Refer this post - Reading HTML content from a UIWebView

Related

iOS UIWebView failed to find PDF header after converting PDF as String to NSData

I'm trying to download a PDF and display it in a UIWebView. The API is giving me the PDF as a String, looking like this:
I convert the string to NSData:
self.fileData = responseObject!.dataUsingEncoding(NSUTF8StringEncoding)!
Then i load it into my WebView, however i am getting:
failed to find PDF header: `%PDF' not found.
What am i doing wrong?
You could save the string as a file, with String's writetoFile method for example, then grab this file directly from the webview with UIWebView's loadRequest method.
Note that usually a PDF encoded as a String should be encoded as Base64 String. If this is the case for your String, then you could use NSData's base64EncodedString initializer to read the string content as data.

Get html code and edit then load on to web view

I want to load a web site on a UIWebView which is not under my control and edit/add certain UI changes (Some texts, images, etc) to it. Can I do this within my iOS source code? I can't change the hosted html contents since them not under my control.
If this cannot doable within iOS source code, please advice me the correct way to achieve this.
Load the webpage into an NSString, make any modifications and then put the html into the UIWebView.
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *page = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
/* Make changes to page here */
[self.webView loadHTMLString:page baseURL:nil];
I'd get the dom with JavaScript, manipulate, then inject back with JavaScript.
See stringByEvaluatingJavaScriptFromString:.
You can write your own full featured, minified JavaScript, then pass into using this method.
// Change body color of any HTML content inside a UIWebView.
NSString *javaScript = #"document.getElementByTagName('body').backgroundColor = '#888';";
[webView stringByEvaluatingJavaScriptFromString:javaScript];

After attempting to create a UIWebView with a GIF in it, when webViewDidFinishLoad is called, the URL is always just about:blank. Why?

I'm trying to show a UIWebView with a GIF in it, but only once the GIF has loaded.
I load the GIF as follows:
self.GIFWebView = [[UIWebView alloc] init];
self.GIFWebView.delegate = self;
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
[self.GIFWebView loadHTMLString:html baseURL:nil];
Where post is just an object with some properties such as the URL for the GIF.
Then in webViewDidFinishLoad: I show the web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%f", webView.scrollView.frame.size.width);
NSLog(#"%#", [webView.request.URL absoluteString]);
}
I get "0" and "about:blank" for the NSLogs each time, however.
Why does it not load the GIF properly?
I get "0" and "about:blank" for the NSLogs each time, however.
Not surprising. You're telling the web view to load HTML that you're providing in a string rather than giving it a request. The URL that you're logging is the request URL, and since there's no request, there's no request URL.
Why does it not load the GIF properly?
Possibly because you're misusing the URL object. Look at the code:
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
We can't tell what type post.url is, but it's probably NSURL*. You're probably passing a NSURL into the format string, and that may not produce the result you're looking for. Try passing in a string like [post.url absoluteString] instead of the actual NSURL object.
Also, you might want to log the value of html right after you create it so that you can check the full HTML that you're sending to the web view.
Update: Some additional things to check:
Are you running the code in question on the main thread?
Is the thread's run loop getting time?
Have you tried setting a non-nil base URL?
If the web view's delegate has a -webView:shouldStartLoadWithRequest: method, does it return YES?
What happens if you use a constant string that includes the HTML you want and the hard-coded URL instead of constructing the string with +stringWithFormat:?
Is the test device connected to the network? (Sometimes it's the simplest thing that gets you.)
Does it work correctly if you use a different image? I notice that the URL you're using is for an animated .gif file, try a non-animated .gif or a .jpg image instead.
Update 2: The problem lies in your creation of the web view. Look at the very first line in the code that you showed:
self.GIFWebView = [[UIWebView alloc] init];
That looks okay for a typical object, but -init is not the designated initializer for a view. You should use -initWithFrame: instead. The image loads fine in your sample project when I change the code in your project to use the right initializer:
UIWebView *GIFWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];

iOS RSS feed data fetching : How to get Description / Content of feed

I have followed this tutorial http://www.raywenderlich.com/2636/how-to-make-a-simple-rss-reader-iphone-app-tutorial#comments
But I also want to get description/content instead of showing its URL in web view.
I tried everything but nothing worked.
Any help?
As shown in ray wenderlich website, you can get the feed details from GDataXML's GDataXMLElement. Check the key you are getting for description/content GDataXMLElement like:
[item valueForChild:#"description"]
and save that string in the nsobject class you created to save the rss feed required data. After that you can load html string in the webview and add your custom css styling for the html string if needed. You can load html string in webview using webview's
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
method. Hope my answer helps.

iOS UIWebView - refresh only part of html file

Is there any way to refresh only part of an HTML file displayed in a UIWebView? For example, if I make changes to a div in the body, is there any way to refresh only the contents of that div tag? Or do I have to reload the entire file into my UIWebView?
You have to reload entire HTML file. It is not possible to reload only part of pure HTML file.
Using a little bit of javascript?
use javascript like this
NSString *js = [NSString stringWithFormat:#"document.getElementById('foo').innerHTML ='update';"];
NSString *res = [self stringByEvaluatingJavaScriptFromString:js];

Resources