IOS学习之UIWebView

16 篇文章 0 订阅
#import <UIKit/UIKit.h>
@interface UIWebViewController : UIViewController<UIWebViewDelegate>

@end
#import "UIWebViewController.h"

@interface UIWebViewController ()

@end

@implementation UIWebViewController

UIWebView * webView;
UIActivityIndicatorView *activityIndicator;

- (void)viewDidLoad {
    [super viewDidLoad];
    //  Xcode 7, 涉及到了访问http资源,测试时碰到如下报错:
    //  App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
    //  解决方法:打开工程主目录下info.plist
    //  增加属性字典 App Transport Security Settings
    //  这个属性下增加节点 Allow Arbitrary Loads, value 为 YES

    
    CGRect rect=[[UIScreen mainScreen]applicationFrame];
    rect=self.view.frame;
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 120, rect.size.width, rect.size.height)];
    
    //加载网页
    //[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    
    //加载本地html
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString *filePath = [resourcePath stringByAppendingPathComponent:@"serviceAgreement.html"];
    NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
    

    
    //自动对页面进行缩放以适应屏幕
    webView.scalesPageToFit = YES;

    //自动检测网页上的电话号码,单击可以拨打
    webView.detectsPhoneNumbers = YES;
    
    //为webView添加背景图片
    webView.backgroundColor=[UIColor clearColor];
    webView.opaque=NO;//这句话很重要 webView是否是不透明的 NO为透明 在webView下添加个imageView展示图片就可以了
    
    //UIWebView类内部会管理浏览器的导航动作
    //[webView goBack];//后退
    //[webView goForward];//前进
    //[webView reload];//重载
    //[webView stopLoading];//取消载入内容
    
    //实现协议
    webView.delegate=self;
    
    [self.view addSubview:webView];
}

- (void) webViewDidStartLoad:(UIWebView *)webView
{
    //创建UIActivityIndicatorView背底半透明View
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [view setTag:108];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAlpha:0.5];
    [self.view addSubview:view];
    
    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40, 40)];
    [activityIndicator setCenter:view.center];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.view viewWithTag:108];
    [view removeFromSuperview];
    NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.view viewWithTag:108];
    [view removeFromSuperview];
    NSLog(@"didFailLoadWithError:%@", error);
}
@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值