您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 獲取當前網頁的 URL、Title 與 HTML

編輯:iOS 相關資訊

下面演示如何在 iOS 中獲取當前網頁的 URL、title 和 HTML 內容。

[objc]
@property (strong, nonatomic) UIWebView *webView;
@property (strong, nonatomic) NSString *currentURL;
@property (strong, nonatomic) NSString *currentTitle;
@property (strong, nonatomic) NSString *currentHTML;

[objc]
-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//獲取當前頁面的 title
    self.currentURL = webView.request.URL.absoluteString;
    NSLog(@"title-%@--url-%@--", self.title, self.currentURL);
    NSString *lJs = @"document.documentElement.innerHTML";//獲取當前網頁的 html
    self.currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs];
}

透過 webViewDidFinishLoad: 回調,利用 stringByEvaluatingJavaScriptFromString: 執行 JavaScript 取得頁面標題與 HTML,並從 webView.request.URL 讀取當前網址。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved