forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIWebView+BlocksKit.m
More file actions
79 lines (59 loc) · 2.48 KB
/
Copy pathUIWebView+BlocksKit.m
File metadata and controls
79 lines (59 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// UIWebView+BlocksKit.m
// BlocksKit
//
#import "A2DynamicDelegate.h"
#import "NSObject+A2BlockDelegate.h"
#import "UIWebView+BlocksKit.h"
#pragma mark Custom delegate
@interface A2DynamicUIWebViewDelegate : A2DynamicDelegate <UIWebViewDelegate>
@end
@implementation A2DynamicUIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
BOOL ret = YES;
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)])
ret = [realDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
BOOL (^block)(UIWebView *, NSURLRequest *, UIWebViewNavigationType) = [self blockImplementationForMethod:_cmd];
if (block)
ret &= block(webView, request, navigationType);
return ret;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(webViewDidStartLoad:)])
[realDelegate webViewDidStartLoad:webView];
void (^block)(UIWebView *) = [self blockImplementationForMethod:_cmd];
if (block) block(webView);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(webViewDidFinishLoad:)])
[realDelegate webViewDidFinishLoad:webView];
void (^block)(UIWebView *) = [self blockImplementationForMethod:_cmd];
if (block) block(webView);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)])
[realDelegate webView:webView didFailLoadWithError:error];
void (^block)(UIWebView *, NSError *) = [self blockImplementationForMethod:_cmd];
if (block) block(webView, error);
}
@end
#pragma mark Category
@implementation UIWebView (BlocksKit)
@dynamic bk_shouldStartLoadBlock, bk_didStartLoadBlock, bk_didFinishLoadBlock, bk_didFinishWithErrorBlock;
+ (void)load
{
@autoreleasepool {
[self bk_registerDynamicDelegate];
[self bk_linkDelegateMethods:@{
@"bk_shouldStartLoadBlock": @"webView:shouldStartLoadWithRequest:navigationType:",
@"bk_didStartLoadBlock": @"webViewDidStartLoad:",
@"bk_didFinishLoadBlock": @"webViewDidFinishLoad:",
@"bk_didFinishWithErrorBlock": @"webView:didFailLoadWithError:"
}];
}
}
@end