Skip to content

Commit

Permalink
Add delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
gcamp committed Jan 14, 2015
1 parent 5be38e4 commit 9abad6b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions SVWebViewController/SVModalWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
- (instancetype)initWithURLRequest:(NSURLRequest *)request;

@property (nonatomic, strong) UIColor *barsTintColor;
@property (nonatomic, weak) id<UIWebViewDelegate> webViewDelegate;

@end
10 changes: 10 additions & 0 deletions SVWebViewController/SVModalWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ - (void)viewWillAppear:(BOOL)animated {
self.navigationBar.tintColor = self.barsTintColor;
}

#pragma mark - Delegate

- (void)setWebViewDelegate:(id<UIWebViewDelegate>)webViewDelegate {
self.webViewController.delegate = webViewDelegate;
}

- (id<UIWebViewDelegate>)webViewDelegate {
return self.webViewController.delegate;
}

@end
2 changes: 2 additions & 0 deletions SVWebViewController/SVWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
- (instancetype)initWithURL:(NSURL*)URL;
- (instancetype)initWithURLRequest:(NSURLRequest *)request;

@property (nonatomic, weak) id<UIWebViewDelegate> delegate;

@end
41 changes: 31 additions & 10 deletions SVWebViewController/SVWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ @implementation SVWebViewController

- (void)dealloc {
[self.webView stopLoading];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.webView.delegate = nil;
self.delegate = nil;
}

- (instancetype)initWithAddress:(NSString *)urlString {
Expand Down Expand Up @@ -62,7 +63,7 @@ - (void)loadView {
}

- (void)viewDidLoad {
[super viewDidLoad];
[super viewDidLoad];
[self updateToolbarItems];
}

Expand All @@ -79,8 +80,8 @@ - (void)viewDidUnload {
- (void)viewWillAppear:(BOOL)animated {
NSAssert(self.navigationController, @"SVWebViewController needs to be contained in a UINavigationController. If you are presenting SVWebViewController modally, use SVModalWebViewController instead.");

[super viewWillAppear:animated];
[super viewWillAppear:animated];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.navigationController setToolbarHidden:NO animated:animated];
}
Expand Down Expand Up @@ -126,7 +127,7 @@ - (UIBarButtonItem *)backBarButtonItem {
style:UIBarButtonItemStylePlain
target:self
action:@selector(goBackTapped:)];
_backBarButtonItem.width = 18.0f;
_backBarButtonItem.width = 18.0f;
}
return _backBarButtonItem;
}
Expand All @@ -137,7 +138,7 @@ - (UIBarButtonItem *)forwardBarButtonItem {
style:UIBarButtonItemStylePlain
target:self
action:@selector(goForwardTapped:)];
_forwardBarButtonItem.width = 18.0f;
_forwardBarButtonItem.width = 18.0f;
}
return _forwardBarButtonItem;
}
Expand Down Expand Up @@ -218,24 +219,44 @@ - (void)updateToolbarItems {
#pragma mark - UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self updateToolbarItems];

if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
[self.delegate webViewDidStartLoad:webView];
}
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

if (self.navigationItem.title == nil) {
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

[self updateToolbarItems];

if ([self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[self.delegate webViewDidFinishLoad:webView];
}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self updateToolbarItems];

if ([self.delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
[self.delegate webView:webView didFailLoadWithError:error];
}
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([self.delegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
return [self.delegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}

return YES;
}

#pragma mark - Target actions
Expand All @@ -254,7 +275,7 @@ - (void)reloadTapped:(UIBarButtonItem *)sender {

- (void)stopTapped:(UIBarButtonItem *)sender {
[self.webView stopLoading];
[self updateToolbarItems];
[self updateToolbarItems];
}

- (void)actionButtonTapped:(id)sender {
Expand Down

0 comments on commit 9abad6b

Please sign in to comment.