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
executable file
·81 lines (61 loc) · 2.51 KB
/
Copy pathUIWebView+BlocksKit.m
File metadata and controls
executable file
·81 lines (61 loc) · 2.51 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
80
81
//
// UIWebView+BlocksKit.m
// BlocksKit
//
#import "UIWebView+BlocksKit.h"
#import "A2BlockDelegate+BlocksKit.h"
#pragma mark Custom delegate
@interface A2DynamicUIWebViewDelegate : A2DynamicDelegate
@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 shouldStartLoadBlock, didStartLoadBlock, didFinishLoadBlock, didFinishWithErrorBlock;
+ (void)load {
@autoreleasepool {
[self registerDynamicDelegate];
NSDictionary *methods = [NSDictionary dictionaryWithObjectsAndKeys:
@"webView:shouldStartLoadWithRequest:navigationType:", @"shouldStartLoadBlock",
@"webViewDidStartLoad:", @"didStartLoadBlock",
@"webViewDidFinishLoad:", @"didFinishLoadBlock",
@"webView:didFailLoadWithError:", @"didFinishWithErrorBlock",
nil];
[self linkDelegateMethods:methods];
}
}
@end