從零開始設計搭建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閱讀 172,820評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,180評論 4 61
  • iOS開發系列--網絡開發 概覽 大部分應用程序都或多或少會牽扯到網絡開發,例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 3,705評論 2 7
  • 陸銘軒也半擁著蘇曉薈坐在草地上,兩人親昵地靠在一起,享受著屬于二人的甜蜜時光。 蘇曉薈微抬起頭看著陸銘軒,帶著不安...
    邵悅婷閱讀 276評論 1 3
  • 知乎上有句話特別應景"你每天忙著工作、聚會、旅行、交朋友,別人眼中的你日子充實而美好,可父母眼中的你,只不過是個年...
    蘭卡公子閱讀 1,427評論 0 1