iOS UIWebView 鐨勫姞杞�

OS涔婾IWebView鐨勪娇鐢�

鍒氭帴瑙OS寮�鍙�1骞村锛岀幇鍦ㄥ浜� 娣峰悎寮� 绉诲姩绔紑鍙戣秺鏉ヨ秺娴佽锛屽洜涓哄紑鍙戞垚鏈笂銆侀�熷害涓婇兘姣斾紶缁熺殑APP寮�鍙戣濂斤紝娣峰悎寮忓紑鍙� 鏄紶缁熸ā寮忎笌PC缃戦〉绔浉缁撳悎鐨勬ā寮忋�傞偅涔堟彁鍒颁簡 APP鐨勬贩鍚堟ā寮忓紑鍙戯紝鍦ˋndroid寮�鍙戜腑鏈塛ebView浣滀负娣峰悎妯″紡寮�鍙戠殑妗ユ锛屽綋鐒跺湪IOS涓篃鍚屾牱鏈変竴涓� UIWebView 缁勪欢鏉ヤ綔涓烘贩鍚堟ā寮忓紑鍙戠殑妗ユ锛岄偅涔堜笅闈㈠氨瀵筓IWebView鐨勪竴浜涘熀鏈煡璇嗚瑙d竴涓嬨��

涓�銆乁IWebView鐨勫熀纭�浣跨敤

1銆佸垱寤篣IWebView锛�

CGRect bouds = [[UIScreen manScreen]applicationFrame]; UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];

2銆佽缃睘鎬э細

webView.scalespageToFit = YES;//鑷姩瀵归〉闈㈣繘琛岀缉鏀句互閫傚簲灞忓箷 webView.detectsPhoneNumbers = YES;//鑷姩妫�娴嬬綉椤典笂鐨勭數璇濆彿鐮侊紝鍗曞嚮鍙互鎷ㄦ墦

3銆佹樉绀虹綉椤佃鍥綰IWebView锛�

[self.view addSubview:webView];

4銆佸姞杞藉唴瀹�

NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];//鍒涘缓URL NSURLRequest* request = [NSURLRequest requestWithURL:url];//鍒涘缓NSURLRequest [webView loadRequest:request];//鍔犺浇

涔熷彲浠ュ姞杞戒竴涓湰鍦拌祫婧�:

NSURL* url = [NSURL fileURLWithPath:filePath];//鍒涘缓URL NSURLRequest* request = [NSURLRequest requestWithURL:url];//鍒涘缓NSURLRequest [webView loadRequest:request];//鍔犺浇

UIWebView 杩樻敮鎸佸皢涓�涓狽SString瀵硅薄浣滀负婧愭潵鍔犺浇銆備綘鍙互涓哄叾鎻愪緵涓�涓熀纭�URL锛屾潵鎸囧UIWebView瀵硅薄濡備綍璺熼殢閾炬帴鍜屽姞杞借繙绋嬭祫婧愶細

[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];

5銆佸鑸�

UIWebView绫诲唴閮ㄤ細绠$悊娴忚鍣ㄧ殑瀵艰埅鍔ㄤ綔锛岄�氳繃goForward鍜実oBack鏂规硶浣犲彲浠ユ帶鍒跺墠杩涗笌鍚庨��鍔ㄤ綔锛�

[webView goBack]; [webView goForward]; [webView reload];//閲嶈浇 [webView stopLoading];//鍙栨秷杞藉叆鍐呭

6銆乁IWebViewDelegate濮旀墭浠g悊

UIWebView鏀寔涓�缁勫鎵樻柟娉曪紝杩欎簺鏂规硶灏嗗湪鐗瑰畾鏃堕棿寰楀埌閫氱煡銆傝浣跨敤杩欎簺鏂规硶锛屽繀椤诲厛璁惧畾webView鐨勫鎵橈細

webView.delegate = self;

涓嬮潰姣忎釜濮旀墭鏂规硶鐨勭涓�涓弬鏁伴兘鏄寚鍚戜竴涓猆Iwebview鐨勬寚閽堬紝鍥犳浣犲彲浠ュ皢涓�涓鎵樼敤浜庡涓綉椤佃鍥俱��

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationType;//褰撶綉椤佃鍥捐鎸囩ず杞藉叆鍐呭鑰屽緱鍒伴�氱煡銆傚簲褰撹繑鍥瀁ES锛岃繖鏍蜂細杩涜鍔犺浇銆傞�氳繃瀵艰埅绫诲瀷鍙傛暟鍙互寰楀埌璇锋眰鍙戣捣鐨勫師鍥狅紝鍙互鏄互涓嬩换鎰忓�硷細 UIWebViewNavigationTypeLinkClicked UIWebViewNavigationTypeFormSubmitted UIWebViewNavigationTypeBackForward UIWebViewNavigationTypeReload UIWebViewNavigationTypeFormResubmitted UIWebViewNavigationTypeOther

UIWebView鎺т欢鍔犺浇缃戦〉鐨勭洃鍚嚱鏁版柟娉曪細

-(void)webViewDidStartLoad:(UIWebView*)webView ;//褰撶綉椤佃鍥惧凡缁忓紑濮嬪姞杞戒竴涓姹傚悗锛屽緱鍒伴�氱煡銆� -(void)webViewDidFinishLoad:(UIWebView*)webView ;//褰撶綉椤佃鍥剧粨鏉熷姞杞戒竴涓姹備箣鍚庯紝寰楀埌閫氱煡銆� -(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;//褰撳湪璇锋眰鍔犺浇涓彂鐢熼敊璇椂锛屽緱鍒伴�氱煡銆備細鎻愪緵涓�涓狽SSError瀵硅薄锛屼互鏍囪瘑鎵�鍙戠敓閿欒绫诲瀷銆�

浠ヤ笂鏄疘OS涓璘IWebView鐨勫熀纭�浣跨敤瑕佺偣璇﹁В锛屾帴涓嬫潵涓�浜沀IWebView鐨勫父鐢ㄦ敞鎰忕偣銆�

浜屻�両OS涓璘IWebView甯哥敤娉ㄦ剰鐐癸細

1銆佷笌UIWebView杩涜浜や簰锛岃皟鐢╳eb椤甸潰涓殑闇�瑕佷紶鍙傜殑鍑芥暟鏃讹紝鍙傛暟闇�瑕佸甫鍗曞紩鍙凤紝鎴栬�呭弻寮曞彿锛堝弻寮曞彿闇�瑕佽繘琛岃浆涔夊湪杞箟瀛楃鍓嶅姞\锛�,鍦ㄤ紶閫抝son瀛楃涓叉椂涓嶉渶瑕佸姞鍗曞紩鍙锋垨鍙屽紩鍙凤細

-(void)webViewDidFinishLoad:(UIWebView *)webView{NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];[webView stringByEvaluatingJavaScriptFromString:sendJsStr];}

2銆佸湪璇ヤ唬鐞嗘柟娉曚腑鍒ゆ柇涓巜ebView鐨勪氦浜掞紝鍙�氳繃html閲屽畾涔夌殑鍗忚瀹炵幇锛�

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

3銆佸彧鏈夊湪webView鍔犺浇瀹屾瘯涔嬪悗鍦ㄨ兘澶熻皟鐢ㄥ搴旈〉闈腑鐨刯s鏂规硶銆傦紙瀵瑰簲鏂规硶濡傜1鏉★級.

4銆佷负webView娣诲姞鑳屾櫙鍥剧墖锛�

approvalWebView.backgroundColor=[UIColor clearColor];approvalWebView.opaque=NO;//杩欏彞璇濆緢閲嶈锛寃ebView鏄惁鏄笉閫忔槑鐨勶紝no涓洪�忔槑 鍦╳ebView涓嬫坊鍔犱釜imageView灞曠ず鍥剧墖灏卞彲浠ヤ簡

5銆佽幏鍙杦ebView椤甸潰鍐呭淇℃伅锛�

NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//鑾峰彇web椤甸潰鍐呭淇℃伅锛屾澶勮幏鍙栫殑鏄釜json瀛楃涓睸BJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];NSDictionary *contentDic=[parserJson objectWithString:docStr];//灏唈son瀛楃涓茶浆鍖栦负瀛楀吀

6銆� 鍔犺浇鏈湴鏂囦欢鐨勬柟娉曪細

//绗竴绉嶆柟娉曪細NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"html" inDirectory:@"mobile"];//mobile鏄牴鐩綍锛宯ame鏄枃浠跺悕绉帮紝html鏄枃浠剁被鍨媅webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; //鍔犺浇鏈湴鏂囦欢//绗簩绉嶆柟娉曪細NSString *resourcePath = [[NSBundle mainBundle] resourcePath]

NSString*filePath=[resourcePath stringByAppendingPathComponent:@"mobile.html"]; NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; [uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

7銆佸皢鏂囦欢涓嬭浇鍒版湰鍦板潃鐒跺悗鍐嶇敤webView鎵撳紑锛�

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];self.filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@"maydoc%@",docType]];NSData *attachmentData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theUrl]];[attachmentData writeToFile:filePath atomically:YES];NSURL *url = [NSURL fileURLWithPath:filePath];NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];[attachmentWebView loadRequest:requestObj];//鍒犻櫎鎸囧畾鐩綍涓嬬殑鏂囦欢NSFileManager *magngerDoc=[NSFileManager defaultManager];[magngerDoc removeItemAtPath:filePath error:nil];

8銆佸鐞唚ebView灞曠ずtxt鏂囨。涔辩爜闂锛�

if ([theType isEqualToString:@".txt"]) {//txt鍒嗗甫缂栫爜鍜屼笉甯︾紪鐮佷袱绉嶏紝甯︾紪鐮佺殑濡俇TF-8鏍煎紡txt锛屼笉甯︾紪鐮佺殑濡侫NSI鏍煎紡txt//涓嶅甫鐨勶紝鍙互渚濇灏濊瘯GBK鍜孏B18030缂栫爜NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];if (!aStr) {//鐢℅BK杩涜缂栫爜aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];}if (!aStr) {//鐢℅BK缂栫爜涓嶈,鍐嶇敤GB18030缂栫爜aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];}//閫氳繃html璇█杩涜鎺掔増NSString* responseStr = [NSString stringWithFormat:@"""""""""""

"

"%@"

"/pre>"

""

"",

aStr];

[attachmentWebView loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

return;

}

9銆佷娇鐢╳ebView鍔犺浇鏈湴鎴栫綉缁滄枃浠舵暣涓祦绋嬶細

1銆丩oading a local PDF file into the web view

- (void)viewDidLoad {

[super viewDidLoad];

//浠庢湰鍦板姞杞�

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"iPhone_User_Guide" ofType:@"pdf"];

if (thePath) {

NSData *pdfData = [NSData dataWithContentsOfFile:thePath];

[(UIWebView *)self.view loadData:pdfData MIMEType:@"application/pdf"

textEncodingName:@"utf-8" baseURL:nil];

}

//浠庣綉缁滃姞杞�

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];

}

2銆乀he web-view delegate managing network loading

- (void)webViewDidStartLoad:(UIWebView *)webView

{

// starting the load, show the activity indicator in the status bar

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

// finished loading, hide the activity indicator in the status bar

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

// load error, hide the activity indicator in the status bar

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

// report the error inside the webview

NSString* errorString = [NSString stringWithFormat:

@"

An error occurred:

%@

",

error.localizedDescription];

[self.myWebView loadHTMLString:errorString baseURL:nil];

}

3銆丼topping a load request when the web view is to disappear

- (void)viewWillDisappear:(BOOL)animated

{

if ( [self.myWebView loading] ) {

[self.myWebView stopLoading];

}

self.myWebView.delegate = nil;聽聽聽 // disconnect the delegate as the webview is hidden

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

/************/

寮曠敤鑷嫻鏋滃畼鏂规枃妗o紙displaying web content锛�

10銆佹煡鎵緒ebView涓殑scrollview锛�

- (void) addScrollViewListener

{

UIScrollView* currentScrollView;

for (UIView* subView in self.webView.subviews) {

if ([subView isKindOfClass:[UIScrollView class]]) {

currentScrollView = (UIScrollView*)subView;

currentScrollView.delegate = self;

}

}

}

11銆佸幓鎺墂ebView鐨勯槾褰憋紝鍋氭垚绫讳技scrollView锛�

- (void)clearBackgroundWithColor:(UIColor*)color

{

// 鍘绘帀webview鐨勯槾褰�

self.backgroundColor = color;

for (UIView* subView in [self subviews])

{

if ([subView isKindOfClass:[UIScrollView class]]) {

for (UIView* shadowView in [subView subviews])

{

if ([shadowView isKindOfClass:[UIImageView class]]) {

[shadowView setHidden:YES];

}

}

}

}

}

12銆佸彇娑堥暱鎸墂ebView涓婄殑閾炬帴寮瑰嚭actionSheet鐨勯棶棰橈細

-(void)webViewDidFinishLoad:(UIWebView *)webView

{

[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout = 'none';"];

}

13銆佸彇娑坵ebView涓婄殑瓒呯骇閾炬帴鍔犺浇闂锛�

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

if (navigationType==UIWebViewNavigationTypeLinkClicked) {

return NO;

}

else {

return YES;

}

}

14銆亀ebView鍦╥os5.1涔嬪墠鐨刡ug锛氬湪涔嬪墠鐨勫伐绋嬩腑浣跨敤webView鍔犺浇闄勪欢锛寃ebView鏀寔doc锛宔xcel锛宲pt锛宲df绛夋牸寮忥紝浣嗚繖浜涢檮浠跺繀椤诲厛涓嬭浇鍒版湰鍦扮劧鍚庡湪鍔犺浇鍒皐ebView涓婃墠鍙互鏄剧ず锛�

褰撻檮浠朵笅杞藉埌鏈湴涔嬪悗鍒氬垰寮�濮嬪姞杞藉埌webView涓婃椂锛屾鏃堕��鍑洪檮浠堕〉闈細瀵艰嚧绋嬪簭宕╂簝銆備細宕╂簝鏄敱浜巜ebView鎺т欢鍐呴儴娌℃湁鎶婄浉鍏充唬鐞嗗彇娑堟帀锛屾墍浠ュ鑷撮��鍑轰箣鍚庣▼搴忓穿婧冦��

webView鍦�5.1涓婄殑bug锛氫箣鍓嶉」鐩渶姹傝webView鍙互宸﹀彸娲诲姩锛屼絾鍦ㄥ線webView涓婂姞杞介〉闈㈡椂瀵艰嚧椤甸潰鍔犺浇涓嶅叏锛岃繖涓猙ug鏄敱浜巜ebView鏈韩鐨勭紦瀛樻墍鑷淬�傦紙杩樻湁寰呯爺绌讹級

15銆佸湪浣跨敤webView杩涜鏂版氮寰崥鍒嗕韩鏃讹紝webView浼氳嚜鍔ㄤ繚瀛樼櫥闄嗙殑cookie瀵艰嚧椤圭洰涓殑鍒嗕韩妯″潡鏈変簺闂锛屽垹闄� webView鐨刢ookie鐨勬柟娉曪細

-(void)deleteCookieForDominPathStr:(NSString *)thePath

{

//鍒犻櫎鏈湴cookie锛宼hePath涓篶ookie璺緞閫氳繃鎵撳嵃cookie鍙煡閬撳叾璺緞

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

if([[cookie domain] isEqualToString:thePath]) {

[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];

}

}

}

16銆佸湪UIWebView涓娇鐢╢lashScrollIndicators

浣跨敤UIScrollView鏃讹紝鎴戜滑鍙互浣跨敤flashScrollIndicators鏂规硶鏄剧ず婊氬姩鏍囪瘑鐒跺悗娑堝け锛屽憡鐭ョ敤鎴锋椤甸潰鍙互婊氬姩锛屽悗闈㈣繕鏈夋洿澶氬唴瀹广�俇IWebView鍐呴儴渚濊禆浜嶶IScrollView,浣嗘槸鍏舵病鏈塮lashScrollIndicators鏂规硶锛屼絾鍙互閫氳繃鍏朵粬閫斿緞浣跨敤姝ゆ柟娉曪紝濡備笅鎵�绀恒��

for (id subView in [webView subviews])

{聽 if ([subView respondsToSelector:@selector(flashScrollIndicators)])

{

[subView flashScrollIndicators];

}

}

涓婅堪浠g爜鐗囨鍙互鍒皐ebViewDidFinishLoad鍥炶皟涓娇鐢紝鍔犺浇瀹岀綉椤靛唴瀹瑰悗flash鏄剧ず婊氬姩鏍囪瘑銆� 17銆佹牴鎹唴瀹硅幏鍙朥IWebView鐨勯珮搴︼細

鏈夋椂鍊欓渶瑕佹牴鎹笉鍚岀殑鍐呭璋冩暣UIWebView鐨勯珮搴︼紝浠ヤ娇UIWebView鍒氬ソ瑁呬笅鎵�鏈夊唴瀹癸紝涓嶇敤鎷栧姩锛屽悗闈篃涓嶄細鐣欑櫧銆傛湁涓ょ鏂瑰紡鍙牴鎹姞杞藉唴瀹硅幏鍙朥IWebView鐨勫悎閫傞珮搴︼紝浣嗛兘闇�瑕佸湪缃戦〉鍐呭鍔犺浇瀹屾垚鍚庢墠鍙互锛屽嵆闇�瑕佸湪webViewDidFinishLoad鍥炶皟涓娇鐢�

鈶�.浣跨敤sizeThatFits鏂规硶銆�

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

CGRect frame = webView.frame;

frame.size.height = 1;

webView.frame = frame;

CGSize fittingSize = [webView sizeThatFits:CGSizeZero];

frame.size = fittingSize;

webView.frame = frame;

}

sizeThatFits鏂规硶鏈変釜闂锛屽鏋滃綋鍓峌IView鐨勫ぇ灏忔瘮鍒氬ソ鍚堥�傜殑澶у皬杩樺ぇ锛屽垯杩斿洖褰撳墠鐨勫ぇ灏忥紝涓嶄細杩斿洖鏈�鍚堥�傜殑澶у皬鍊硷紝鎵�浠ヤ娇鐢╯izeThatFits鍓嶏紝鍏堝皢UIWebView鐨勯珮搴﹁涓烘渶灏忥紝鍗�1锛岀劧鍚庡啀浣跨敤sizeThatFits灏变細杩斿洖鍒氬ソ鍚堥�傜殑澶у皬銆�

鈶°�佷娇鐢↗avaScript

- (void)webViewDidFinishLoad:(UIWebView *)webView

{聽聽聽 CGRect frame = webView.frame;

NSString *fitHeight = [webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

frame.size.height = [fitHeight floatValue];

webView.frame = frame;

你可能感兴趣的:(iOS UIWebView 鐨勫姞杞�)