我想讓網址預覽,比如在這張照片 - 如何在不使用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.
}
你能給我一些建議,你是如何爲這個需求做實現的,目前我也在尋找同樣的解決方案。 –