Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix activity indicator counts and support delegating the webView and …
…view controllers events.
  • Loading branch information
Andrew W. Donoho committed Dec 9, 2013
commit 54fdfbc3c2252ca28c67c215abf65875fc63a84c
15 changes: 15 additions & 0 deletions SVWebViewController/SVWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,23 @@

#import "SVModalWebViewController.h"

@protocol SVWebViewControllerDelegate <NSObject>

@optional

- (void) webViewControllerWillAppear: (SVWebViewController *) wvc;
- (void) webViewControllerDidAppear: (SVWebViewController *) wvc;

- (void) webViewControllerWillDisappear: (SVWebViewController *) wvc;
- (void) webViewControllerDidDisappear: (SVWebViewController *) wvc;

@end

@interface SVWebViewController : UIViewController

@property (nonatomic, strong) UIWebView *webView;
@property (weak, nonatomic) id<UIWebViewDelegate, SVWebViewControllerDelegate> delegate;

- (id)initWithAddress:(NSString*)urlString;
- (id)initWithURL:(NSURL*)URL;

Expand Down
132 changes: 106 additions & 26 deletions SVWebViewController/SVWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ @interface SVWebViewController () <UIWebViewDelegate>
@property (nonatomic, strong) UIBarButtonItem *stopBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *actionBarButtonItem;

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSURL *URL;

@property (nonatomic) NSUInteger webViewLoads;

- (id)initWithAddress:(NSString*)urlString;
- (id)initWithURL:(NSURL*)URL;
- (void)loadURL:(NSURL*)URL;
Expand All @@ -41,12 +42,22 @@ @implementation SVWebViewController
#pragma mark - Initialization

- (void)dealloc {
// [self.webView stopLoading];
[self.webView loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString: @"about:blank"]]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.webView.delegate = nil;

UIWebView *wv = self.webView;

[wv stopLoading];

[NSOperationQueue.mainQueue addOperationWithBlock: ^{

[wv loadRequest:
[NSURLRequest requestWithURL: [NSURL URLWithString: @"about:blank"]]];
}];
if (!self.delegate) {

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
wv.delegate = nil;
self.delegate = nil;
}

- (id)initWithAddress:(NSString *)urlString {
Expand Down Expand Up @@ -96,6 +107,22 @@ - (void)viewWillAppear:(BOOL)animated {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.navigationController setToolbarHidden:NO animated:animated];
}
id<SVWebViewControllerDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webViewControllerWillAppear:)]) {

[delegate webViewControllerWillAppear: self];
}
}

- (void)viewDidAppear:(BOOL)animated {

id<SVWebViewControllerDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webViewControllerDidAppear:)]) {

[delegate webViewControllerDidAppear: self];
}
}

- (void)viewWillDisappear:(BOOL)animated {
Expand All @@ -104,11 +131,27 @@ - (void)viewWillDisappear:(BOOL)animated {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.navigationController setToolbarHidden:YES animated:animated];
}
id<SVWebViewControllerDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webViewControllerWillDisappear:)]) {

[delegate webViewControllerWillDisappear: self];
}
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

id<SVWebViewControllerDelegate> delegate = self.delegate;

if (!delegate) {

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
else if ([delegate respondsToSelector: @selector(webViewControllerDidDisappear:)]) {

[delegate webViewControllerDidDisappear: self];
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
Expand Down Expand Up @@ -177,13 +220,13 @@ - (UIBarButtonItem *)actionBarButtonItem {
- (void)updateToolbarItems {
self.backBarButtonItem.enabled = self.webView.canGoBack;
self.forwardBarButtonItem.enabled = self.webView.canGoForward;
self.actionBarButtonItem.enabled = !self.webView.isLoading;
UIBarButtonItem *refreshStopBarButtonItem = self.webView.isLoading ? self.stopBarButtonItem : self.refreshBarButtonItem;
self.actionBarButtonItem.enabled = !self.webViewLoads;

UIBarButtonItem *refreshStopBarButtonItem = self.webViewLoads ? self.stopBarButtonItem : self.refreshBarButtonItem;

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGFloat toolbarWidth = 250.0f;
fixedSpace.width = 35.0f;
Expand All @@ -198,14 +241,14 @@ - (void)updateToolbarItems {
fixedSpace,
self.actionBarButtonItem,
nil];

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, toolbarWidth, 44.0f)];
toolbar.items = items;
toolbar.barStyle = self.navigationController.navigationBar.barStyle;
toolbar.tintColor = self.navigationController.navigationBar.tintColor;
self.navigationItem.rightBarButtonItems = items.reverseObjectEnumerator.allObjects;
}

else {
NSArray *items = [NSArray arrayWithObjects:
fixedSpace,
Expand All @@ -218,7 +261,7 @@ - (void)updateToolbarItems {
self.actionBarButtonItem,
fixedSpace,
nil];

self.navigationController.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
self.navigationController.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
self.toolbarItems = items;
Expand All @@ -227,22 +270,59 @@ - (void)updateToolbarItems {

#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

id<UIWebViewDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webView:shouldStartLoadWithRequest:navigationType:)]) {

return [delegate webView: webView shouldStartLoadWithRequest: request navigationType: navigationType];
}
return YES;
}

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

self.webViewLoads++;
[self updateToolbarItems];
}

id<UIWebViewDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webViewDidStartLoad:)]) {

[delegate webViewDidStartLoad: webView];
}
else { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; }
}

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


self.webViewLoads = self.webViewLoads ? --self.webViewLoads : 0;
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

[self updateToolbarItems];

id<UIWebViewDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webViewDidFinishLoad:)]) {

[delegate webViewDidFinishLoad: webView];
}
else { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; }
}

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

self.webViewLoads = self.webViewLoads ? --self.webViewLoads : 0;
[self updateToolbarItems];

id<UIWebViewDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webView:didFailLoadWithError:)]) {

[delegate webView: webView didFailLoadWithError: error];
}
else { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; }
}

#pragma mark - Target actions
Expand All @@ -256,12 +336,15 @@ - (void)goForwardClicked:(UIBarButtonItem *)sender {
}

- (void)reloadClicked:(UIBarButtonItem *)sender {
self.webViewLoads = 0;
[self.webView reload];
[self updateToolbarItems];
}

- (void)stopClicked:(UIBarButtonItem *)sender {
self.webViewLoads = 0;
[self.webView stopLoading];
[self updateToolbarItems];
[self updateToolbarItems];
}

- (void)actionButtonClicked:(id)sender {
Expand All @@ -272,10 +355,7 @@ - (void)actionButtonClicked:(id)sender {
}

- (void)doneButtonClicked:(id)sender {
[self.webView loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString: @"about:blank"]]];

[self.webView stopLoading];
[self dismissViewControllerAnimated:YES completion:NULL];
}

Expand Down