0926-UIWebView

----------------------------------------

UIWebView

(网页也有一点作用:图文混排  新闻列表   用UIWebView最方便     大部分是用原生的   部分页面用网页   所以必须要掌握的)

 加载网页 :  拦截请求 返回 前进

 

//
//  HMViewController.m

#import "HMViewController.h"

@interface HMViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardItem;
- (IBAction)back;
- (IBAction)forward;

@property (nonatomic, weak) UIWebView *webView;
@end

@implementation HMViewController

- (void)viewDidLoad
{    [super viewDidLoad];
    // 1.创建webView
    UIWebView *webView = [[UIWebView alloc] init];
    CGRect frame = self.view.bounds;
    frame.origin.y = 64;
    webView.frame = frame;
    [self.view addSubview:webView];
    
    // 2.加载请求
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"login" withExtension:@"html"];//手机本地url
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    
    // 3.设置代理(监听网页的加载过程)
    webView.delegate = self;
    
    self.webView = webView;
}

- (IBAction)back{
    [self.webView goBack];
}

- (IBAction)forward {
    [self.webView goForward];
}

#pragma mark - UIWebViewDelegate
/**
 *  网页加载完毕就调用
 */
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    if ([webView canGoBack]) {
//        self.backItem.enabled = YES;
//    } else {
//        self.backItem.enabled = NO;
//    }
    self.backItem.enabled = [webView canGoBack];
    self.forwardItem.enabled = [webView canGoForward];
    NSLog(@"webViewDidFinishLoad");
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError");
}

/**
 *  作用:一般用来拦截webView发出的所有请求(加载新的网页)
 *  每当webView即将发送一个请求之前,会先调用这个方法
 *
 *  @param request        即将要发送的请求
 *
 *  @return YES :允许发送这个请求  NO :不允许发送这个请求,禁止加载这个请求
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // 如果在path中找不到@“baidu”这个字符串
    //    [path rangeOfString:@"baidu"].length == 0;
    //    [path rangeOfString:@"baidu"].location == NSNotFound
    
    // URL格式:协议头://主机名/路径
    // request.URL.path : 获得的仅仅是主机名(域名)后面的路径
    // request.URL.absoluteString : 获得的是一个完整的URL字符串
    
    // 1.获得完整的url字符串
    NSString *url = request.URL.absoluteString;
    NSUInteger loc = [url rangeOfString:@"baidu"].location;
    
    // 2.找到baidu字符串,返回NO
    if (loc != NSNotFound) { // 能找到
        return NO; // 禁止加载
    }
    
    // 3.如果没有找到,返回YES
    return YES;
    
//    return loc == NSNotFound ? YES : NO;
//    return loc == NSNotFound;
}
@end

 -------------------

加载字符串

[webView loadHTMLString:html baseURL:nil];

 

charles拿到手机网易新闻的地址  返回的是json    

json里包含  html-body字符串 用浏览器查看源代码
webView加载html字符串 
字符串替换<!—IMG#0—>  为 固定图片<img src=“aaa.jpg”/>

放到浏览器上看看  查看源文件

other->empty -> news.css   Xcode里加css

 

 

//
//  HMViewController.m
#import "HMViewController.h"
@interface HMViewController ()

@end

@implementation HMViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.url
    // http://c.m.163.com/nc/article/A7A94MCL00963VRO/full.html
    NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/article/A7AQOT560001124J/full.html"];
    
    // 2.requets
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 3.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSDictionary *news = dict[@"A7AQOT560001124J"];
        [self showNews:news];
    }];
}

- (void)showNews:(NSDictionary *)news
{
    // 1.取出网页内容
    NSString *body = news[@"body"];
    
    // 2.取出图片
    NSDictionary *img = [news[@"img"] lastObject];
    NSString *imgHTML = [NSString stringWithFormat:@"<img src=\"%@\" width=\"300\" height=\"171\">", img[@"src"]];
    
    // 2.创建一个webView,显示网页
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = self.view.bounds;
    [self.view addSubview:webView];
    
    // 3.拼接网页内容
    NSString *html = [body stringByReplacingOccurrencesOfString:img[@"ref"] withString:imgHTML];
    
    // 4.取出新闻标题
    NSString *title = news[@"title"];
    // 5.取出新闻的时间
    NSString *time = news[@"ptime"];
    
    // 头部内容
    NSString *header = [NSString stringWithFormat:@"<div class=\"title\">%@</div><div class=\"time\">%@</div>", title, time];
    html = [NSString stringWithFormat:@"%@%@", header, html];
    
    // 链接mainBundle中的CSS文件
    NSURL *cssURL = [[NSBundle mainBundle] URLForResource:@"news" withExtension:@"css"];
    html = [NSString stringWithFormat:@"%@<link rel=\"stylesheet\" href=\"%@\">", html, cssURL];
    
    // 5.加载网页内容
    [webView loadHTMLString:html baseURL:nil];
}

@end

 

------------------------------------------------

WebView执行js


- (void) webViewDidFinishLoad:(UIWebView *)webView

{

[js1 appendString:@“footer.parentNode.removeChild(footer);“];
[webView stringByEvaluatingJavascriptFromString:js1];js获取网页源代码
NSString *html = [webViewstringByEvaluationJavaScriptFromString:@“document.body.innerHTML”];
NSLog(@“%@“, html);

}

 ------------------------

JS与webView的沟通

网页JS跳转hm://call  方式被webView加载前拦截跳转地址    这样OC就知道跳转的Url里包含了 call方法

webView加载的网页里 用JS调转 到新地址 被webView拦截了协议地址 

OC通过地址的内容来   来执行相应操作

webView的代理方法  拦截网页调转前的地址   

webView加载的网页:

控制器:

---------

 

posted @ 2016-03-21 10:48  海龙王来了  阅读(123)  评论(0编辑  收藏  举报
友情链接:废钢破碎机  带式压滤机