我想让网址预览,比如在这张照片 - 如何在不使用UIWebView的情况下获得标题和网站描述?
所以,我得到使用谷歌网站图标的网站形象。 但我不明白我怎样才能得到网页的标题和描述。 我知道一个好的图书馆URLEmbeddedView
,它工作的很棒! 但我想写我自己的。
我在stackoverflow上发现了很多类似的问题,但是没有答案描述如何在没有UIWebView
的情况下获取网站标题。
我想让网址预览,比如在这张照片 - 如何在不使用UIWebView的情况下获得标题和网站描述?
所以,我得到使用谷歌网站图标的网站形象。 但我不明白我怎样才能得到网页的标题和描述。 我知道一个好的图书馆URLEmbeddedView
,它工作的很棒! 但我想写我自己的。
我在stackoverflow上发现了很多类似的问题,但是没有答案描述如何在没有UIWebView
的情况下获取网站标题。
使用库存API,你可以做到以下几点:
- (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];
}
此代码应在课程背景会话中运行。
你会得到一个字符串,像这样的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/[email protected]?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>
然后,必须解析字符串,特别是<head>
部分去寻找<title>
进入,<link >
到shortcut icon
和<meta>
twitter:description
来获得你想要的信息,而不需要UIWebView。
这可以通过XML解析器来完成。
@Sergei R.我可以知道你是如何做到这一点的,我也在寻找同样的要求。 –
您可以使用XML解析器解析HTML节点,因为它是标记语言(如SGML或XML),直到找到'
'节点,然后解析'您可以使用针对网页的API调用来获取HTML字符串。使用一些third party AP我解析HTML字符串并收集所需的信息。
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.
}
你能给我一些建议,你是如何为这个需求做实现的,目前我也在寻找同样的解决方案。 –