Reading HTML content from a UIWebView - html

Is it possible to read the raw HTML content of a web page that has been loaded into a UIWebView?
If not, is there another way to pull raw HTML content from a web page in the iPhone SDK (such as an equivalent of the .NET WebClient::openRead)?

The second question is actually easier to answer. Look at the stringWithContentsOfURL:encoding:error: method of NSString - it lets you pass in a URL as an instance of NSURL (which can easily be instantiated from NSString) and returns a string with the complete contents of the page at that URL. For example:
NSString *googleString = #"http://www.google.com";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL
encoding:NSASCIIStringEncoding
error:&error];
After running this code, googlePage will contain the HTML for www.google.com, and error will contain any errors encountered in the fetch. (You should check the contents of error after the fetch.)
Going the other way (from a UIWebView) is a bit trickier, but is basically the same concept. You'll have to pull the request from the view, then do the fetch as before:
NSURL *requestURL = [[yourWebView request] URL];
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestURL
encoding:NSASCIIStringEncoding
error:&error];
EDIT: Both these methods take a performance hit, however, since they do the request twice. You can get around this by grabbing the content from a currently-loaded UIWebView using its stringByEvaluatingJavascriptFromString: method, as such:
NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString:
#"document.body.innerHTML"];
This will grab the current HTML contents of the view using the Document Object Model, parse the JavaScript, then give it to you as an NSString* of HTML.
Another way is to do your request programmatically first, then load the UIWebView from what you requested. Let's say you take the second example above, where you have NSString *page as the result of a call to stringWithContentsOfURL:encoding:error:. You can then push that string into the web view using loadHTMLString:baseURL:, assuming you also held on to the NSURL you requested:
[yourWebView loadHTMLString:page baseURL:requestURL];
I'm not sure, however, if this will run JavaScript found in the page you load (the method name, loadHTMLString, is somewhat ambiguous, and the docs don't say much about it).
For more info:
UIWebView class reference
NSString class reference
NSURL class reference

if you want to extract the contents of an already-loaded UIWebView, -stringByEvaluatingJavaScriptFromString. For example:
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];

To get the whole HTML raw data (with <head> and <body>):
NSString *html = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.outerHTML"];

Note that the NSString stringWithContentsOfURL will report a totally different user-agent string than the UIWebView making the same request. So if your server is user-agent aware, and sending back different html depending on who is asking for it, you may not get correct results this way.
Also note that the #"document.body.innerHTML" mentioned above will only display what is in the body tag. If you use #"document.all[0].innerHTML" you will get both head and body. Which is still not the complete contents of the UIWebView, since it will not get back the !doctype or html tags, but it is a lot closer.

To read:-
NSString *html = [myWebView stringByEvaluatingJavaScriptFromString: #"document.getElementById('your div id').textContent"];
NSLog(html);
To modify:-
html = [myWebView stringByEvaluatingJavaScriptFromString: #"document.getElementById('your div id').textContent=''"];

In Swift v3:
let doc = webView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")

(Xcode 5 iOS 7) Universal App example for iOS 7 and Xcode 5. It is an open source project / example located here: Link to SimpleWebView (Project Zip and Source Code Example)

I use a swift extension like this:
extension UIWebView {
var htmlContent:String? {
return self.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
}
}

you should try this:
document.documentElement.outerHTML

UIWebView
get HTML from UIWebView`
let content = uiWebView.stringByEvaluatingJavaScript(from: "document.body.innerHTML")
set HTML into UIWebView
//Do not forget to extend a class from `UIWebViewDelegate` and nil the delegate
func someFunction() {
let uiWebView = UIWebView()
uiWebView.loadHTMLString("<html><body></body></html>", baseURL: nil)
uiWebView.delegate = self as? UIWebViewDelegate
}
func webViewDidFinishLoad(_ webView: UIWebView) {
//ready to be processed
}
[get/set HTML from WKWebView]

Related

Scan for images in website - Xcode

I am making an app which will give me the latest news, and the image. I achieve the text bit by making a scanner like this.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
/* set headers, etc. on request if needed */
[request setURL:[NSURL URLWithString:#"http://stackoverflow.com/questions/22671347/nsuinteger-should-not-be-used-in-format-strings"]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSScanner *scanner = [NSScanner scannerWithString:html];
NSString *token = nil;
[scanner scanUpToString:#"<p>" intoString:NULL];
[scanner scanUpToString:#"</p>" intoString:&token];
int length = 3;
token = [token substringFromIndex:length];
textView.text = token;
Now I was wondering if I could use the same type of code to scan the website to find the first image and put it an image view. Also it don't have to be same type of code , post what ever you know and any method.
Summary is.
Want a piece of code that will scan a webpage, pick up the first image and place it in a image view.
Thanks for the people who take the time to help me.
THANKS AGAIN!!!
BYE!!!
NSScanner its not a HTML parser only intended for scanning values from NSString object. If you doing the odd scan you probably could get away with it, but it doesn't seem like...
The CORRECT approach is to use Libxml2 library included in Xcode which is only written is C which doesn't have any Objective-C/Swift wrapper. Libxml2 is the XML C parser and toolkit developed for the Gnome project. Alternatively i would recommend using open-source project such as HTMLReader. Its a HTML parser with CSS selectors in Objective-C and Foundation. It parses HTML just like a browser and is all written in Objective-c.
Example (using HTMLReader):
HTMLDocument *document = [HTMLDocument documentWithString:html]; // get your html string
NSLog(#"IMG: %#", [document firstNodeMatchingSelector:#"img"].textContent); // => image returned here
To find images just change the tag to < img > and your set!!
IF your using Libxml2 take a look at HTMLparser.c header file to parse and retrieve HTML ltags

How to open/link external files inside a HTML-designed QuickLook plugin without using the <src> attribute?

After my previous question, it seems that I gotta definitely deal with the fact that I have to use HTML in order to design interactive GUIs...but now the problem is another one: I know that for security reasons is not possible (unlike on Xcode 4.2 with OSX 10.6.8) to open/link anymore files from external directories using the <src> attribute, and I was wondering if there might be other ways to achieve that goal.
On this page ("Generating Enriched HTML" paragraph) is shown a portion of code that includes a CSS file as a MIME attachment: do you recon that it could be possible to obtain the same result with a different type of file (such a JS library or an image/video/audio)?
Here's some code from my project:
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
#autoreleasepool {
if (QLPreviewRequestIsCancelled(preview)) return noErr;
NSMutableString *html=[[NSMutableString alloc] init];
NSDictionary *props;
props=#{
(__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey:#"UTF-8",
(__bridge NSString *)kQLPreviewPropertyMIMETypeKey:#"text/html",
};
[html appendString:#"<html>"];
[html appendString:#"<head>"];
[html appendString:#"<script type=\"text/javascript\" src=\"JQuery.js\">"];
[html appendString:#"</script>"];
[html appendString:#"<script>"];
//...
[html appendString:#"</script>"];
[html appendString:#"</head>"];
[html appendString:#"<body>"];
//...
[html appendString:#"</body>"];
[html appendString:#"</html>"];
QLPreviewRequestSetDataRepresentation(preview,(CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding],kUTTypeHTML,(CFDictionaryRef)props);
}
return noErr;
}
Thank you so much in advance!
Yes, all kinds of files (within the limits of how Quick Look sandboxes WebKit) can be attached using the cid: scheme. This is just a way to tell WebKit how to locate the data corresponding to a resource, so it can be used for all kinds of resources.
In your case, load JQuery.js in a NSData object, write src=\"cid:JQuery.js\" and add this to your props object for the kQLPreviewPropertyAttachmentsKey
#{
#"JQuery.js" : #{
(__bridge NSString *)kQLPreviewPropertyMIMETypeKey : #"text/javascript",
(__bridge NSString *)kQLPreviewPropertyAttachmentDataKey: dataContainingJQuery
},
},

Find link on webpage

I have a google webpage, with a search already loaded, and I need to find the first link on the webpage and get the information(the brief summary) under the link. I imagine that this requires some sort of HTML download of the webpage, and then a search through that file for a link tag, but I have no idea how to get a HTML file off of a webpage and save it using Xcode.
To get a HTML file off a webpage is very easy to do, just use NSStrings method +stringWithContentsOfURL:
NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com"] encoding:NSUTF8StringEncoding error:&error];
if(error)
{
// oh, thats bad
}
Then you can search for the first link e.g. by using -rangeOfString
NSRange rangeOfLink = [html rangeOfString:#"bla"];
if (rangeOfLink.location == NSNotFound)
{
// that's bad, too
}

Objective-C How to splice HTML source and set it as a UILabel

I have a URL that I am sending requests through
#"http://******.**********.com/?login=%#&password=%#&form.submitted=true",username.text, password.text];
Then I use the following code to get the source from that webpage:
NSURL *urlRequest = [NSURL URLWithString:url];
NSError *err = nil;
NSString *html = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:&err];
Now, I want to splice that html so I can get ONLY what is in a tag.
<td title="Verified Hours">
858.97
</td>
How can I splice this html source so I can result in just (for example) 858.97?
You can use the open source Code called DTCoreText
It can help you change the Html To Navtive View.
...UILabel,UIImageView,UIButton....
you can search in github or https://github.com/Cocoanetics/DTCoreText
Thanks for the help, I solved my problem by using this HTML Parser:
https://github.com/zootreeves/Objective-C-HMTL-Parser

Injecting local files into UIWebView without breaking links

I am working on an iOS app that needs to display webpages from a server inside a UIWebView while injecting relevant local png and css files as needed in order to speed up load time. Here is the code I am using to try to do this:
NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/index.html"]]];
NSString* myFileHtml = [[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding];
[myWebView loadHTMLString:myFileHtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
My problem is that some of the webpages have buttons in them that link to other webpages on the server, and because the UIWebView is only loading a string, the buttons when tapped don't cause the UIWebView to load the new webpage URL like it would if I had used the loadRequest method.
My question is how can I get the the UIWebView to behave like it is loading a request while still injecting local files from the baseurl?
Thanks
The relative links in the button cannot work, because the linked pages are on a remote server and not on the device's file system. However you can use a UIWebViewDelegate method to make it work:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *localRootPath = [NSString stringWithFormat:#"file://%#", [[NSBundle mainBundle] bundlePath]];
NSString *remoteRootPath = #"http://yourdomain.com";
NSString *remotePath = [[request.URL absoluteString] stringByReplacingOccurrencesOfString:localRootPath withString:remoteRootPath];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:remotePath]]];
// or you can use your own loading mechanism here
return NO;
}
return YES;
}
This method intercepts all requests from your WebView. If the request was triggered by a user tap / click the URL gets modified from a relative URL to a absolute URL so it can be loaded from the server. Don't forget to set the delegate on the WebView or this method will not be called.
NSURLPRotocol is a handler for NSURLConnection and will give you the opportunity to intercept the calls to the server and substitute your own content.
1) Derive a class from NSURlProtocol
2) Call NSURLProtocol registerClass: in your application:didFinishLaunchingWithOption
3) Read the documentation on implement these methods as necessary:
initWithRequest:cachedResponse:client:,
startLoading,
URLProtocol:didReceiveResponse:cacheStoragePolicy:
URLProtocolDidFinishLoading: