{"id":523,"date":"2014-09-02T15:46:38","date_gmt":"2014-09-02T07:46:38","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=523"},"modified":"2016-07-15T17:24:18","modified_gmt":"2016-07-15T09:24:18","slug":"ios-webview%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/09\/02\/154638523.html","title":{"rendered":"iOS WebView\u7684\u7528\u6cd5"},"content":{"rendered":"<p>\u4e00\u3001UIWebView \u53ef\u4ee5\u52a0\u8f7d\u548c\u663e\u793a\u67d0\u4e2aURL\u7684\u7f51\u9875\uff0c\u4e5f\u53ef\u4ee5\u663e\u793a\u57fa\u4e8eHTML\u7684\u672c\u5730\u7f51\u9875\u6216\u90e8\u5206\u7f51\u9875\uff1a<br \/>\na. \u52a0\u8f7d URL<\/p>\n<pre lang=\"objc\" line=\"1\">WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];   \nNSString *path = @\"http:\/\/www.baidu.com\";   \nNSURL *url = [NSURL URLWithString:path];   \n[WebView loadRequest:[NSURLRequest requestWithURL:url]];   <\/pre>\n<p>b. \u52a0\u8f7d HTML<\/p>\n<pre lang=\"objc\" line=\"1\">NSBundle *bundle = [NSBundle mainBundle];\nNSString *resPath = [bundle resourcePath];\nNSString *filePath = [resPath stringByAppendingPathComponent:@\"Home.html\"];\n[webView loadHTMLString:[NSString stringWithContentsOfFile:filePath]\n  baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];<\/pre>\n<p>\u4e8c\u3001\u4f7f\u7528\u7f51\u9875\u52a0\u8f7d\u6307\u793a\uff0c\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u663e\u793a\u7f51\u9875\u51fa\u6765<br \/>\n    \u9996\u5148\u8981\u6307\u5b9a\u59d4\u6258\u65b9\u6cd5\uff1a<br \/>\n       webView.delegate =self;<br \/>\n    UIWebView\u4e3b\u8981\u6709\u4e0b\u9762\u51e0\u4e2a\u59d4\u6258\u65b9\u6cd5\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">- (void)webViewDidStartLoad:(UIWebView *)webView;\u5f00\u59cb\u52a0\u8f7d\u7684\u65f6\u5019\u6267\u884c\u8be5\u65b9\u6cd5\u3002\n- (void)webViewDidFinishLoad:(UIWebView *)webView;\u52a0\u8f7d\u5b8c\u6210\u7684\u65f6\u5019\u6267\u884c\u8be5\u65b9\u6cd5\u3002\n- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;\u52a0\u8f7d\u51fa\u9519\u7684\u65f6\u5019\u6267\u884c\u8be5\u65b9\u6cd5\u3002<\/pre>\n<p>     \u8fd9\u6837\uff0c\u53ef\u4ee5\u5229\u7528 webViewDidStartLoad \u548c webViewDidFinishLoad \u65b9\u6cd5\u5b9e\u73b0\u672c\u529f\u80fd\uff1a<!--more-->        <\/p>\n<pre lang=\"objc\" line=\"1\">- (void) webViewDidStartLoad:(UIWebView *)webView  \n{  \n    \/\/\u521b\u5efaUIActivityIndicatorView\u80cc\u5e95\u534a\u900f\u660eView        \n    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    \n    [view setTag:108];    \n    [view setBackgroundColor:[UIColor blackColor]];    \n    [view setAlpha:0.5];    \n    [self.view addSubview:view];    \n      \n    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];    \n    [activityIndicator setCenter:view.center];    \n    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    \n    [view addSubview:activityIndicator];    \n  \n    [activityIndicator startAnimating]; \n}\n- (void) webViewDidFinishLoad:(UIWebView *)webView  \n{  \n    [activityIndicator stopAnimating];  \n    UIView *view = (UIView*)[self.view viewWithTag:108];  \n    [view removeFromSuperview];  \n    NSLog(@\"webViewDidFinishLoad\");  \n}  <\/pre>\n<p>\u4e09\u3001UIWebView\u8fd8\u53ef\u4ee5\u901a\u8fc7 stringByEvaluatingJavaScriptFromString \u51fd\u6570\u6765\u6267\u884c javascript\u4ee3\u7801\uff0c\u4ece\u800c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u7f51\u9875\u6587\u4ef6<\/p>\n<pre lang=\"objc\" line=\"1\">1. \u83b7\u5f97UIWebView\u7684\u6807\u9898\nNSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@\"document.title\"];\n2. \u83b7\u53d6\u9875\u9762\u7684URL\nNSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@\"document.location.href\"];\n3. \u4fee\u6539\u9875\u9762\u5143\u7d20\u7684\u503c\nNSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@\"document.getElementsByName('q')[0].value='\u6731\u7941\u6797';\"];\n4. \u8868\u5355\u63d0\u4ea4\nNSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@\"document.forms[0].submit(); \"]\n<\/pre>\n<p>\u56db\u3001\u7efc\u5408\u8fd0\u7528\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u90e8\u5206\u7f51\u9875\u7684\u663e\u793a\uff0c\u57fa\u672c\u601d\u8def\u5982\u4e0b:<\/p>\n<pre lang=\"objc\" line=\"1\">1. \u7528 loadRequest \u5148\u83b7\u53d6\u7f51\u9875\uff0c\u4f46\u5148\u8bbe\u7f6e UIWebView.hidden = YES;\n2. \u5728 webViewDidFinishLoad \u4e2d\uff0c\u7528\nNSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@\"document.getElementByIdx_xx_x_x('xxxx').innerHTML; \"] \u83b7\u53d6\u6307\u5b9a\u6807\u7b7e\u5143\u7d20\u7684HTML\u4ee3\u7801\n3. \u518d\u4f7f\u7528 loadHTMLString \u52a0\u8f7d\u8be5HTML\u4ee3\u7801\uff0c\u5373\u53ef<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001UIWebView \u53ef\u4ee5\u52a0\u8f7d\u548c\u663e\u793a\u67d0\u4e2aURL\u7684\u7f51\u9875\uff0c\u4e5f\u53ef\u4ee5\u663e\u793a\u57fa\u4e8eHTML\u7684\u672c\u5730\u7f51\u9875\u6216\u90e8\u5206\u7f51\u9875\uff1a a. \u52a0\u8f7d URL WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)]; NSString *path = @&#8221;http:\/\/www.baidu.com&#8221;; NSURL *url = [NSURL URLWithString:path]; [WebView loadRequest:[NSURLRequest requestWithURL:url]]; b. \u52a0\u8f7d HTML NSBundle *bundle = [NSBundle mainBundle]; NSString *resPath = [bundle resourcePath]; NSString *filePath = [resPath stringByAppendingPathComponent:@&#8221;Home.html&#8221;]; [webView loadHTMLString:[NSString stringWithContentsOfFile:filePath] baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]]; \u4e8c\u3001\u4f7f\u7528\u7f51\u9875\u52a0\u8f7d\u6307\u793a\uff0c\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u663e\u793a\u7f51\u9875\u51fa\u6765 \u9996\u5148\u8981\u6307\u5b9a\u59d4\u6258\u65b9\u6cd5\uff1a webView.delegate =self; UIWebView\u4e3b\u8981\u6709\u4e0b\u9762\u51e0\u4e2a\u59d4\u6258\u65b9\u6cd5\uff1a &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[20,18],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics","tag-network"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=523"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/523\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}