IOS---UIWebview用法点滴

本文详细介绍了如何使用UIWebView在iOS应用中加载、控制网页,并提供了自定义请求、查看返回、通过JS控制Webview等实用技巧。通过实例演示了如何在应用中集成外部网页内容,以及如何进行数据交互。

1.  load  from url:从url加载页面

NSURL *url = [NSURL URLWithString:"http://stackoverflow.com"];
NSString *body = @"laadeedaa";

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];


// Dump the response to the UIWebView
[webView loadRequest:request];


2. 自定义 webview的请求 ,load with customized request

// Do your POST
NSURL *url = [NSURL URLWithString:"http://stackoverflow.com"];
NSString *body = @"laadeedaa";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];

// Dump the response to the UIWebView
[webView loadRequest:request];


3.查看webveiw的返回

// Dump the response to the UIWebView
[webView loadRequest:request];

NSURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
NSString *respString = [[[NSString alloc] initWithData:respData
                                              encoding:NSUTF8StringEncoding] autorelease];

NSString *htmlString = [self doSomethingWith:respString];

[webView loadHTMLString:htmlString baseURL:@"http://stackoverflow.com"];


4.通过js控制webview

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

例如:  NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString:
                                         @"document.body.innerHTML"];


see also:

http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest


http://stackoverflow.com/questions/2061393/iphone-sdk-control-websites-in-uiwebview-programmatically


http://stackoverflow.com/questions/992348/reading-html-content-from-a-uiwebview

http://iphoneincubator.com/blog/tag/uiwebview

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值