Google image from cocoa - objective-c

I'd like to write a program in cocoa that parse a google image webpage and extract the images.
i use a code like this:
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: #"https://www.google.it/search?q=%#&tbm=isch", searchString] ];
NSStringEncoding enc;
NSString *test = [NSString stringWithContentsOfURL:url usedEncoding:&enc error:NULL];
The problem is that the page that is returned in this way is different from what it is in a browser.
I'don't get the imgurl parameter with the url of the full image. only the thumbnails.
There is a way to have the complete google images results in cocoa like i have in firefox?
Thank you

What you are doing is not correct way.
To get list of images, you should use Google API for image Search
Follow below link for more details.
https://developers.google.com/image-search/v1/jsondevguide
The URL for webservice would look like below.
https://ajax.googleapis.com/ajax/services/search/images?q=soccer&v=1.0
^^^^^^ your search keyword here...

Related

Extracting Text from url Xcode?

I am new to Objective C.
I was trying to extract text from a webpage and display it in a textView;
Except for when I run the app it appears to show html instead of the article.
NSURL *URL = [NSURL URLWithString:[self.url
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSData *theData = [NSData dataWithContentsOfURL:URL];
NSString *content = [[NSString alloc] initWithData:theData encoding:NSStringEncodingConversionAllowLossy];
_viewPage.text = content;
The viewPage is the textview itself. How do I extract the text only?
You have not included any code to extract the text from the web page. When you run "dataWithContentsOfURL" and it's a web page, then, as you have seen, you get the whole page.
To extract the data you need to process the results. In a simple case you can get the text with some string manipulation. In more complex cases you should look for a library which will parse the whole page for you. You can then access the parsed structure to get the content that you want.
Look here for an example of the simple case;
http://natashatherobot.com/html-css-parser-objective-c/
There are libraries for the more complex case.

Get Information About a Website Using there URL in Xcode

Im trying to Get Information About a Website like title (Like what you see in safari in the Tabs and Windows)and Description of that Website from its URL Without Using the WebKit Framework in Xcode and Displaying it on screen as text. I Just want to know have to obtain the title of the Website/Page that the URL Belongs to using Cocoa. Is There a Tutorial or Something. It will be a Big Help.
Try this:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* title = [webView stringByEvaluatingJavaScriptFromString: #"document.title"];
navbar.title = title;
}
Taken from: http://blog.mcohen.me/2008/12/12/getting-the-title-of-a-web-view-in-cocoa-touch/
If you don't want to display the webview, make it hidden and use above delegate method to get what you want...
A way you could do it is by downloading the HTML content at the target url and parse it, for example using hpple.
Example (not tested):
NSURL *url = [NSURL URLWithString:#"http://..."];
//Only using this for simplicity of the example.
//You should really use an asynchronous method instead (see `NSURLSession`).
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Parse HTML and search for title element
TFHpple *hppleParser = [[TFHpple alloc] initWithHTMLData:data];
TFHppleElement *titleElement = [hppleParser search:#"//title"][0];
NSString titleString = titleElement.text;

Url encodings with greek characters

I get the html source of a page to a NSString like this
NSString* url = #"example url";
NSURL *urlRequest = [NSURL URLWithString:url];
NSError *err = nil;
NSString *response = [NSString stringWithContentsOfURL:urlRequest encoding:kCFStringEncodingUTF8 error:&err];
a part of the response is like : 2 \u00cf\u0083\u00cf\u0087\u00cf\u008c\u00ce\u00bb\u00ce\u00b9\u00ce\u00b1
How can i have the Greek characters shown as they should in the NSString response?
The encoding of the page is "charset=iso-8859-7"
Ahhh, I understand your question a little bit better now.
The Apple-supplied native implementation of NSString doesn't know what to do with iso-8859-7 encoding.
You have two options.
1)
Try requesting different encodings to [NSString stringWithContentsOfURL: encoding: error:] to see if one successfully loads. My first attempt would be with NSISOLatin1StringEncoding.
2)
I found a third party library (and NSString category extension) that does do iso-8859-7 conversion. But to get access to CkoCharset will cost you (or your client) $290 USD. It might be a worthwhile investment to save time & hassle.
https://chilkatsoft.com/charset-objc.asp
and documentation is here:
http://www.chilkatsoft.com/refdoc/objcCkoCharsetRef.html

Get url variable

I have a application on iPad 1 and a PDF with several links.
The client will click on that link and I need to get its variables values and use it on my application.
Is it possible?
Thanks in advance
Use a UIWebView and load the PDF in it. Something like this (note - it's untested but should get you on the right track!):
NSString* basePath = [[NSBundle mainBundle] bundlePath];
NSURL* baseurl = [NSURL fileURLWithPath:basePath];
[self.webView loadData:MIMEType:#"application/pdf" textEncodingName:nil baseURL:baseurl];

Unable to retrieve certain pages using stringWithContentsOfURL

I am trying to get HTML files from the web, using stringWithContentsOfURL:. My problem is, sometimes it works but sometimes it doesn't. For example, I tried:
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
encoding:encoding1
error:nil];
NSLog(#"html = %#",string);
This works fine, but when I replace the URL with #"http://www.youtube.com/" then I only get "NULL". Is there anyone that knows what's going on? Is it because of YouTube having some sort of protection?
Google's home page uses ISO-8859-1 encoding (aka "Latin-1", or NSISOLatin1StringEncoding). YouTube uses UTF-8 (NSUTF8StringEncoding), and the encoding you've specified with your encoding1 variable has to match the web page in question.
If you just want the web page and don't really care what encoding it's in, try this:
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
usedEncoding:&encoding
error:&error];
NSLog(#"html = %#",string);
This method will tell you what the encoding was (by writing it to the encoding variable), but you can just throw that away and focus on the string.