问题
I want to make URL preview, such as in this picture -
So, I get image of website using google favicons.
But I don't understand how can I get the title and description of the web-page.
I know a good library URLEmbeddedView
and it works great!.
But I want to write my own.
I found many similar questions on stackoverflow, but there are no answers that describe how to get website title without UIWebView
.
回答1:
Using stock API, you can do the following:
- (void)getSiteHtml
{
NSURLSession *session = NSURLSession.sharedSession;
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.stackoverflow.com"]
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *htmlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"http string: %@", htmlString);
}];
[dataTask resume];
}
This code should be run in background session of course.
You will get back a string like this for StackOverflow:
<!DOCTYPE html>
<html>
<head>
<title>Stack Overflow</title>
<link rel="shortcut icon" href="//cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
<link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
<link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="stackoverflow.com"/>
<meta property="og:type" content="website" />
<meta property="og:image" itemprop="image primaryImageOfPage" content="http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a" />
<meta name="twitter:title" property="og:title" itemprop="title name" content="Stack Overflow" />
<meta name="twitter:description" property="og:description" itemprop="description" content="Q&A for professional and enthusiast programmers" />
<meta property="og:url" content="http://stackoverflow.com/"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//cdn.sstatic.net/Js/stub.en.js?v=b84e3ec1d0b3"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/stackoverflow/all.css?v=ef9c49b839e0">
<link rel="alternate" type="application/atom+xml" title="Feed of recent questions" href="/feeds">
<script>
You must then parse the string and especially the <head>
section to look for the <title>
entry, the <link >
to the shortcut icon
and the <meta>
twitter:description
to get the information you want, without needing a UIWebView.
That can be done with a XML parser for instance.
回答2:
You can get a HTML string using API call for webpage. Use some third party API to parse HTML string and gather required information.
NSURL *URL = [NSURL URLWithString:@"http://stackoverflow.com/questions/39526606/objective-c-how-can-i-get-title-and-description-of-website-without-using-uiw"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString* htmlString = [operation responseString]
// parse HTML string to gather required information.
}
来源:https://stackoverflow.com/questions/39526606/how-can-i-get-title-and-description-of-website-without-using-uiwebview