封装技巧

来源:互联网 发布:mac系统怎么安装ps 编辑:程序博客网 时间:2024/05/18 02:11

https://github.com/wangyangcc/IMYWebView


参考了这个统一UIWebView 和 WKWebview的方式,封装得非常好:


大体的思想如下:



而具体实现的代码:

- (void)callback_webViewDidFinishLoad{    if ([self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {        [self.delegate webViewDidFinishLoad:self];    }}- (void)callback_webViewDidStartLoad{    if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {        [self.delegate webViewDidStartLoad:self];    }}- (void)callback_webViewDidFailLoadWithError:(NSError*)error{    if ([self.delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {        [self.delegate webView:self didFailLoadWithError:error];    }}- (BOOL)callback_webViewShouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(NSInteger)navigationType{    BOOL resultBOOL = YES;    if ([self.delegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {        if (navigationType == -1) {            navigationType = UIWebViewNavigationTypeOther;        }        resultBOOL = [self.delegate webView:self shouldStartLoadWithRequest:request navigationType:navigationType];    }    return resultBOOL;}

反射加代理,就可以很好的实现了



还有适当重写一下response方法也是非常实用的:




还有一些直接改写函数和用KVC来绑定属性和成员变量的也可以参考一下

0 0