Get Information About a Website Using there URL in Xcode - objective-c

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;

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.

Google image from cocoa

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...

How to share picture on Google+?

Our two teams are developing same app: adroid team and iOS team. Android team was able to do text + photo sharing. Simple post with text is done normally but text + image requires google+ app and the code that opens small center'ed web view looks like this:
Intent shareIntent = ShareCompat.IntentBuilder.from(act)
.setText(message).setStream(uri).setType("image/jpeg")
.getIntent().setPackage("com.google.android.apps.plus");
act.startActivityForResult(shareIntent, SocialNetworksConstants.GOOGLE_POST_REQUERST);
However, I can't find anything similar in the newest Google+ iOS sdk. GPPShareBuilder protocol only has:
- (id<GPPShareBuilder>)setPrefillText:(NSString *)prefillText;
and
- (id<GPPShareBuilder>)setTitle:(NSString *)title
description:(NSString *)description
thumbnailURL:(NSURL *)thumbnailURL;
Has anybody faced this problem?
UPDATE: Here's a link to solution for Android. It looks like Android has some "ShareCompat" Android native library that extends functionality for sharing data between apps.
UPDATE2 Tried to use "setTitle:description:thumbnailURL:" with google+ sharing sample code in recent Google+ sdk. Tried to push NSURL of some image from bundle:
- (IBAction)shareButton:(id)sender{
...
if (title && description) {
//NSURL *imageUrl = [NSURL URLWithString:[deepLinkThumbnailURL_ text]];
NSString *imagePathString = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
NSURL *imageUrl = [[NSURL alloc] initWithString:imagePathString];
shareBuilder = [shareBuilder setTitle:title
description:description
thumbnailURL:imageUrl]; // throw image path
}
However that didn't work out. Image placeholder is empty when google+ opens sharing window in safari. Only some external URL link is working.
Use this :
NSData *imageData = UIImagePNGRepresentation(myImage.image);
[(id< GPPNativeShareBuilder >)shareBuilder attachImageData:imageData];

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];

Parse XML and generate UI in UIWebView iPad App

I will receive an XML from Server, and have to parse the xml file, and based on that have to populate textbox, listbox etc(a form) in UIWebView.
I know XML parsing for iOS using NSXML, I do not know how to fire events to generate Form/UI at runtime.
Any tutorials or idea pls.
If you have created your HTML as a NSString already, you can load it into the UIWebView by using the method loadHTMLString:baseURL:. For example:
NSString *htmlString = [[NSString alloc] init];
//Download XML, parse it and turn it into HTML.
[myWebView loadHTMLString:htmlString baseURL:nil];
//if you have to do any thing else to the HTML string do it here
[htmlString release];
Changing the htmlString after calling loadHTMLString:baseURL: won't update the UIWebView. You will need to call the method again, sending the modified string.
If you need further help, comment below and I'll edit my answer or comment back.