在iOS开发中少不了使用UIWebView,以下是自己使用的总结:
初始化webView 很简单
自定义了一个WebVC控制器
#import <UIKit/UIKit.h>
@interface WebVC : UIViewController
@property (nonatomic,copy) NSString *urlStr;
@property (nonatomic,strong) UIWebView *webView;
@end
WebVC.m文件
#import "WebVC.h"
@interface WebVC ()<UIWebViewDelegate>
@end
@implementation WebVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setUrlStr:(NSString *)urlStr
{
_urlStr = urlStr;
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
[self.view addSubview:self.webView];
}
- (UIWebView *)webView
{
if (!_webView) {
_webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
_webView.backgroundColor = [UIColor whiteColor];
_webView.delegate = self;
}
return _webView;
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"开始加载");
//在此可以利用MBProgressHUD为加载过程做一个动画
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"完成加载");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"加载失败");
//加载失败可以添加一个View显示加载失败也也可以添加一个按钮重新加载,tip:当页面加载失败时,webView的url会被置为空,所以需要将当前的请求链接重新赋值,重新让webView 进行 loadRequest
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"准备加载");
//通过request获取请求的链接
NSString *requestString = [[request URL] absoluteString];
NSLog(@"加载的链接 --- %@",requestString);
return YES;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
加载网页首先会调用这个方法 默认返回的是YES 当返回的是NO 网页就无法加载。
在这个方法里面我们可以监听请求的链接进行相应的操作
比如 我定义一个通用跳转枚举 根据请求链接的协议头和主机位判断是属于那种跳转
Tip:跳转协议头跟主机位需要事先跟web前端开发协商好
typedef enum {
WebVCRequestTypeCustomerService = 1001,//客服列表
WebVCRequestTypeProductDetail = 1002,//商品详情
WebVCRequestTypeOrderDetail = 1003//订单详情
}WebVCRequestType;
为了方便监听是属于那种跳转,在这里定义了一个全局跳转通知
WebVC.h 中声明了一个跳转属性
extern NSString * const JumpNotifacation;
WebVC.m 对该属性进行赋值定义
NSString * const JumpNotifacation = @"JumpNotifacation";
添加通知
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpAction:) name:JumpNotifacation object:nil];
}
在当前的网页上操作 监听操作链接
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"准备加载");
//通过request获取请求的链接
NSString *requestString = [[request URL] absoluteString];
NSURL *requestUrl = request.URL;
NSLog(@"加载的链接 --- %@",requestString);
//此处的协议头要事先跟web前端约定好
if ([requestUrl.scheme isEqualToString:@"saga"]) {//判断协议头
//发送通用跳转通知
[[NSNotificationCenter defaultCenter] postNotificationName:JumpNotifacation object:nil userInfo:@{@"url":requestUrl}];
return NO;
} else {
return YES;
}
}
执行通知方法
- (void)jumpAction:(NSNotification *)notification
{
NSDictionary *dict = notification.userInfo;
id url = dict[@"url"];
if (![url isKindOfClass:[NSURL class]]) {
NSLog(@"参数不是请求URL");
return;
}
NSURL *requestUrl = url;
//根据主机位判断是属于那种跳转类型 这主机位事先跟web前端约定好
WebVCRequestType type = requestUrl.host.intValue;
switch (type) {
case WebVCRequestTypeCustomerService:
NSLog(@"跳转到客服列表");
break;
case WebVCRequestTypeProductDetail:
NSLog(@"跳转到商品详情");
break;
case WebVCRequestTypeOrderDetail:
NSLog(@"跳转到订单详情");
break;
default:
break;
}
}
以上就是简述了项目中UIWebView与iOS端交互的操作。