Find link on webpage - html

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
}

Related

Display a preview of the website

I need to display a preview of the website with given url in the single image(e.g. like Facebook do in Messenger when you sending a url to someone). Is there any way to achieve that without actually loading the html file and reading it's metadata?
Try this,
NSString *urlStr = #"http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a";
NSURL *url=[NSURL URLWithString: urlStr];
NSData *data=[NSData dataWithContentsOfURL:url];
UIImage *image=[[UIImage alloc] initWithData:data];
UIImageView *imagview = [[UIImageView alloc]initWithImage:image];
imagview.frame = CGRectMake(250, 500, 100, 100);
[self.view addSubview:imagview];
This will give just thumb or image.
You can use URLEmbeddedView Library for more functionality. I think this is the library what you want.

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
},
},

Image not being displayed in UIImageView pulled from website

I have an app which is pulling the image and displaying it in a UIImageView. I have code that I know is working for pulling the images so I think it has to do something with the HTML. I triple check all of the connections in xcode and all are connected. If you could tell me why the image is not being pulled, that would be great! I am pulling from this website if you want to go look at all of the HTML yourself, but I want the "Right now" image.
Code to pull image:
- (void)viewDidLoad {
[super viewDidLoad];
self.urlForFeelsLike = #"http://www.weather.com/weather/today/CAXX0518:1:CA";
NSURL *myURL2 = [NSURL URLWithString: [self.urlForFeelsLike stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request2 = [NSURLRequest requestWithURL:myURL2];
[webViewForFeelLike loadRequest:request2];
webViewForFeelLike.delegate = self;
timer1 = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(loadImageForCurrentCondition) userInfo:nil repeats:YES];
}
-(void)loadImageForCurrentCondition {
js = #"document.getElementsByClassName('wx-weather-icon')[0].src;";
imgLink = [webViewForFeelLike stringByEvaluatingJavaScriptFromString:js];
imgURL = [NSURL URLWithString:imgLink];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
data = [NSData dataWithContentsOfURL:imgURL];
[self performSelectorOnMainThread:#selector(setImageViewImage) withObject:data waitUntilDone:YES];
});
}
- (void)setImageViewImage {
imgData = [NSData dataWithContentsOfURL:imgURL];
image = [UIImage imageWithData:imgData];
dispatch_async(dispatch_get_main_queue(),^{
if (image == nil) {
}
else {
[currentConditionImageView setImage:image];
}
});
NSLog(#"Weather image reloaded");
}
HTML line I am pulling from:
<div class="wx-data-part wx-first"><img src="http://s.imwx.com/v.20131006.214956/img/wxicon/120/26.png" height="120" width="120" alt="Cloudy" class="wx-weather-icon">
Your JavaScript is wrong.
document.getElementsByClassName('wx-weather-icon') //Always returns an array, so you want:
document.getElementsByClassName('wx-weather-icon')[0].src;
On another note:
dcorbatta is right below as well, you're using UIWebView incorrectly here. Also, if you're using a UIWebView just to get that image, you're doing things in a very slow, unnecessarily intensive way. UIWebView spends a lot of memory rendering and parsing (it's intended for browsing), when something simpler would accomplish the same feat faster and more efficiently. I'd recommend loading the page as an HTML string, and parsing that string instead.
Why do you use an NSTimer? You can use the WebView delegate method: webViewDidFinishLoad:
And why do you extract the image url from the html? The image src going to change in a near future?
As i have seen the link you gave. i Checked the css&HTML of the link the are not using the "Rightnow" image. with our web view we can create backgrounds. and you css code is good it is loading cloud image exactly.
i used the link [link]http://s.imwx.com/v.20131006.214956/img/wxicon/120/26.png where this is a cloud image.. go to w3schools site and type/paste
[link]http://www.w3schools.com/css/tryit.asp?filename=trycss_default

Reading HTML content from a UIWebView

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]