iOS - WebView

- (void)createdWebView{

    [self.certificateWebView setUserInteractionEnabled:YES];

    //1.创建并加载远程网页
    NSURL *url = [NSURL URLWithString:@"http://www.taobao.com"];
    [self.certificateWebView loadRequest:[NSURLRequest requestWithURL:url
                                          ]];
    2.加载本地文件资源
    NSString *filePath = @"列如webapp.plist";
    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_certificateWebView loadRequest:request];

  3.读入一个HTML,直接写入一个HTML代码
    拖动文件到Xcode,提示两个选择,“create groups”和“create folder references”,默认情况下为第一种,即所有加入到项目的文件都会在mainBundle根路径下,即不管加入项目的文件的目录结构如何,在APP中都可以通过mainBundlePath/filename来访问到;如果采用第二种方式,则就会保留相对路径,需要通过mainBundlePath/path/filename来访问。通过这两种方式到项目的文件夹显示具有不同的颜色。 选择“create groups”,文件夹颜色为黄色。

  (1)选择“create groups”,html里有一个路径的问题(  link href= “”),在Xcode中, Xcode不能找到像url(”../images/photo.png”)这种路径,所以应该在Xcode中调整下html文件访问本页面图片和css样式文件的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"touBio-add" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSString *basePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:basePath];
    [self.certificateWebView loadHTMLString:htmlString baseURL:baseURL];



  (2)选择“create folder references”。蓝色的文件夹。文件夹名字为urlName,里面包含一个名为index的html文件
    NSURL *htmlPath = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"touBio-add" ofType:@"html" inDirectory:@"appPhone"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:htmlPath];
    [self.certificateWebView loadRequest:request];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [webView stringByEvaluatingJavaScriptFromString:@"rewrite();"];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSURL *url = [NSURL URLWithString:@"www.taobao.com"];
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if ((([httpResponse statusCode] / 100) == 2)) {

        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

        [_certificateWebView loadRequest:[NSURLRequest requestWithURL:url]];

    } else {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"HTTP Error", @"Error message displayed when receving a connection error.") forKey:NSLocalizedDescriptionKey];

        NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];

        if ([error code] == 404) {
            NSLog(@"断网");
            _certificateWebView.hidden = YES;
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值