从零开始设计搭建ios App框架(十三)

webView封装


App里面常会出现加载一些web页面,这个时候就会用到webView了。这篇文章我这没有太多要说的。直接上代码吧。

PGWebBaseController.h

@interface PGWebBaseController : PGBaseController
@property(nonatomic, strong)UIWebView *webView;
/*
 webView加载完成后会回调此block
 */
@property(nonatomic, copy)void(^webViewDidFinishLoadBlock)(UIWebView *webview);

- (id)initWithTitle:(NSString *)title;
- (void)loadWebRequestWithURLString:(NSString *)urlString home:(NSString *)homeUrl;
- (void)loadWebRequestWithHtmlString:(NSString *)htmlString;

@end

PGWebBaseController.m

@interface PGWebBaseController ()<UIWebViewDelegate>
@property(nonatomic, strong)NSURL *homeURL;
@property(nonatomic, strong)NSURL *firstURL;

@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, assign)int timeout;
@property(nonatomic, assign)int timeIndex;

@property(nonatomic, strong)NSString *szTitle;

@end

@implementation PGWebBaseController

- (id)initWithTitle:(NSString *)title
{
    if(self = [super init])
    {
        self.szTitle = title;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = self.szTitle;
}

- (void)createInitData
{
    [super createInitData];
    self.timeout = 15;
    self.timeIndex = 0;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if(_webView && self.firstURL != nil)
    {
        _webView.delegate = self;
        [_webView loadRequest:[NSURLRequest requestWithURL:self.firstURL]];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if(_webView)
    {
        _webView.delegate = nil;
    }
}

- (void)createSubViews
{
    float nOriginY = self.nNavMaxY;
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, nOriginY, self.view.frame.size.width, self.view.frame.size.height-nOriginY)];
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    _webView.scrollView.bounces = NO;
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_webView];
}

- (void)loadWebRequestWithURLString:(NSString *)urlString home:(NSString *)homeUrl
{
    self.homeURL = [NSURL URLWithString:homeUrl];
    self.firstURL = [NSURL URLWithString:urlString];
    if(_webView && self.firstURL != nil)
    {
        [_webView loadRequest:[NSURLRequest requestWithURL:self.firstURL]];
    }
}

- (void)loadWebRequestWithHtmlString:(NSString *)htmlString
{
    if(_webView)
    {
        [_webView loadHTMLString:htmlString baseURL:nil];
    }
}

- (void)webloadTimeOut
{
    if(self.timeIndex < self.timeout)
    {
        self.timeIndex++;
    }
    else
    {
        [self.webView stopLoading];
    }
}

#pragma mark -
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self showWaitingView:@"数据加载中"];
    [self hideDataLoadErrorView];
    self.timeIndex = 0;
    if(self.timer == nil)
    {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(webloadTimeOut) userInfo:nil repeats:YES];
    }
    
    [self.timer fire];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self hideWaitingView];
    
    self.timeIndex = 0;
    if(self.timer != nil && [self.timer isValid])
    {
        [self.timer invalidate];
    }
    
    [self hideDataLoadErrorView];
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '80%'"];
    
    if(self.webViewDidFinishLoadBlock)
    {
        self.webViewDidFinishLoadBlock(webView);
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self hideWaitingView];
    
    [self showDataLoadErrorView];
    
    self.timeIndex = 0;
    if(self.timer != nil && ![self.timer isValid])
    {
        [self.timer invalidate];
    }
}

#pragma mark -
- (void)reloadData
{
    if(_webView && self.firstURL != nil)
    {
        [_webView loadRequest:[NSURLRequest requestWithURL:self.firstURL]];
    }
}

- (void)errorleftMenuResponse:(id)sender
{
    if(_webView.canGoBack)
    {
        [_webView goBack];
    }
}

@end

使用示例

PGWebBaseController *control = [[PGWebBaseController alloc] init];
    [control loadWebRequestWithURLString:@"https://www.baidu.com/" home:@"https://www.baidu.com/"];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,597评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,726评论 2 7
  • 陆铭轩也半拥着苏晓荟坐在草地上,两人亲昵地靠在一起,享受着属于二人的甜蜜时光。 苏晓荟微抬起头看着陆铭轩,带着不安...
    邵悦婷阅读 283评论 1 3
  • 知乎上有句话特别应景"你每天忙着工作、聚会、旅行、交朋友,别人眼中的你日子充实而美好,可父母眼中的你,只不过是个年...
    兰卡公子阅读 1,433评论 0 1