从零开始设计搭建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 ()
@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/"];

你可能感兴趣的:(从零开始设计搭建ios App框架(十三))