從零開始設(shè)計搭建ios App框架(十三)

webView封裝


App里面常會出現(xiàn)加載一些web頁面,這個時候就會用到webView了。這篇文章我這沒有太多要說的。直接上代碼吧。

PGWebBaseController.h

@interface PGWebBaseController : PGBaseController
@property(nonatomic, strong)UIWebView *webView;
/*
 webView加載完成后會回調(diào)此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:@"數(shù)據(jù)加載中"];
    [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/"];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容