最近用到了UIWebView这个控件,现在整理一下,方便以后查阅。
1> UIWebView的创建
-(UIWebView *)webView {
if (!_webView) {
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
_webView.delegate = self;
_webView.scrollView.scrollEnabled = NO;
}
return _webView;
}
在这里可以设置一些UIWebView的属性,UIWebView继承UIView内部有属性UIScrollView,通过来呈现网络信息。如果需要修改该UIWebView的滚动、回弹属性直接设置对应的 _webView.scrollView. 的属性即可。当然UIWebView的自身属性需要寻找对应属性加以设置,这里不再赘述。
2> UIWebView的代理方法
@optional
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
可选,可以根据需要实现。
3> UIWebView的方法
//重新加载,会重新加载缓存内的信息,在留有缓存的情况下使用
- (void)reload;
//顾名思义,停止加载
- (void)stopLoading;
//下面2个方法可以根据下面的3个属性进行判断,看是否可用
//返回上一个页面
- (void)goBack;
//进入到已经打开过的下一个页面
- (void)goForward;
@property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
@property (nonatomic, readonly, getter=canGoForward) BOOL canGoForward;
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
4> UIWebView 加载网页
- (void)loadRequest:(NSURLRequest *)request;
//加载连接
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HealthyConst_LOGIN_PAGE]]];
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
//加载本地html
NSError *error = nil;
NSString *pathHtml = [[NSBundle mainBundle]pathForResource:@"demo" ofType:@"html"];
NSString *strHtml = [[NSString alloc]initWithContentsOfFile:pathHtml encoding:NSUTF8StringEncoding error:&error];
[self.webView loadHTMLString:strHtml baseURL:[NSURL URLWithString:pathHtml]];
5> 在Safari中打开链接地址
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ( navigationType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
6> UIWebView 的缓存处理
//清除cookies
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
[[NSURLCache sharedURLCache]removeAllCachedResponses];
7> JS 与 OC通信
(1)JS调用本地方式
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//以下是与JS定义好的参数
//JSWithOC.app/signet?sign:?["实参1","实参2"]
NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//1:首次启动直接返回YES
if (![requestString containsString:@"health"]) {
return YES;
}
//2:其它情况下的调用
//参数的元素数组
NSArray *arrayParam = [requestString componentsSeparatedByString:@"?"];
//first param:重定向标识符
__unused NSString *paramMark = arrayParam[0];
//second param:要执行的函数名
NSString *paramMethod = arrayParam[1];
//third param:传递的参数
NSString *paramParam = arrayParam[2];
SEL selector = NSSelectorFromString(paramMethod);
if ([self respondsToSelector:selector]) {
[self performSelector:selector withObject:paramParam afterDelay:0];
} else {
[UIFactory showMessageWithSingleButton:BJCA_HTML_ERROR];
}
return NO;
}
(2) OC本地调用JS方法
//这是约定好的函数
NSString *strInit = @"initMethod('2')";
[self.webView stringByEvaluatingJavaScriptFromString:strInit];
8> 禁用UIWebView的一些特性,使其更像原生的app
禁用长按触控对象弹出的菜单
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];