@interface ViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
//创建对象
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
//请求数据
//1.生成一个网址
NSString *path = @"http://www...";
//2.将网址放入NSURL对象 名称:统一资源定位器
NSURL *url = [NSURL URLWithString:path];
//3.请求数据的对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];//通过webView加载数据(请求数据)
_webView.delegate = self;
[self.view addSubview:_webView];
}
pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{//html页面的js动作触发时运行
NSString * str = [[request URL] absoluteString];//获取请求的绝对路径.
//将UTF8字符转化为string:stringByReplacingPercentEscapesUsingEncoding 字符串转为UTF8:stringByAddingPercentEscapesUsingEncoding
NSString *urlStr = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [urlStr componentsSeparatedByString:@"?"];//用?来截取字符串
if (array.count>=2) {
if ([[NSString stringWithFormat:@"%@",array[1]] hasPrefix:@"/msg"]) {
}else
{
}
return YES;
}