|
| 1 | +// |
| 2 | +// SVWebViewController.m |
| 3 | +// |
| 4 | +// Created by Sam Vermette on 08.11.10. |
| 5 | +// Copyright 2010 Sam Vermette. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#import "SVWebViewController.h" |
| 9 | + |
| 10 | +#define kButtonWidth 18 |
| 11 | +#define kSeparatorWidth 50 |
| 12 | + |
| 13 | +@interface SVWebViewController (private) |
| 14 | + |
| 15 | +- (void)setupToolbar; |
| 16 | + |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation SVWebViewController |
| 20 | + |
| 21 | +@synthesize urlString; |
| 22 | + |
| 23 | +- (void)dealloc { |
| 24 | + [backButton release]; |
| 25 | + [forwardButton release]; |
| 26 | + [actionButton release]; |
| 27 | + |
| 28 | + [super dealloc]; |
| 29 | +} |
| 30 | + |
| 31 | +- (id)initWithAddress:(NSString*)string { |
| 32 | + |
| 33 | + self = [super initWithNibName:@"SVWebViewController" bundle:[NSBundle mainBundle]]; |
| 34 | + |
| 35 | + self.urlString = string; |
| 36 | + |
| 37 | + return self; |
| 38 | +} |
| 39 | + |
| 40 | +- (void)viewDidLoad { |
| 41 | + [super viewDidLoad]; |
| 42 | + |
| 43 | + backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"webBack.png"] style:UIBarButtonItemStylePlain target:rWebView action:@selector(goBack)]; |
| 44 | + backButton.width = kButtonWidth; |
| 45 | + |
| 46 | + forwardButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"webForward.png"] style:UIBarButtonItemStylePlain target:rWebView action:@selector(goForward)]; |
| 47 | + forwardButton.width = kButtonWidth; |
| 48 | + |
| 49 | + actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showActions)]; |
| 50 | + |
| 51 | + if(self.navigationController == nil) { |
| 52 | + |
| 53 | + UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,44)]; |
| 54 | + [self.view addSubview:navBar]; |
| 55 | + [navBar release]; |
| 56 | + |
| 57 | + UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissController)]; |
| 58 | + |
| 59 | + UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:self.title]; |
| 60 | + navItem.rightBarButtonItem = doneButton; |
| 61 | + |
| 62 | + [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES]; |
| 63 | + [navItem release]; |
| 64 | + |
| 65 | + rWebView.frame = CGRectMake(0, CGRectGetMaxY(navBar.frame), 320, CGRectGetMinY(toolbar.frame)-CGRectGetMaxY(navBar.frame)); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +- (void)viewWillAppear:(BOOL)animated { |
| 70 | + [super viewWillAppear:YES]; |
| 71 | + |
| 72 | + NSURL *searchURL = [[NSURL alloc] initWithString:self.urlString]; |
| 73 | + [rWebView loadRequest:[NSURLRequest requestWithURL:searchURL]]; |
| 74 | + [searchURL release]; |
| 75 | + |
| 76 | + [self setupToolbar]; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +- (void)viewWillDisappear:(BOOL)animated { |
| 82 | + [super viewWillDisappear:animated]; |
| 83 | + |
| 84 | + [rWebView stopLoading]; |
| 85 | +} |
| 86 | + |
| 87 | +- (void)dismissController { |
| 88 | + |
| 89 | + [self dismissModalViewControllerAnimated:YES]; |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +#pragma mark - |
| 94 | +#pragma mark UIWebViewDelegate |
| 95 | + |
| 96 | +- (void)webViewDidStartLoad:(UIWebView *)webView { |
| 97 | + |
| 98 | + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; |
| 99 | + |
| 100 | + [self setupToolbar]; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +- (void)webViewDidFinishLoad:(UIWebView *)webView { |
| 105 | + |
| 106 | + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; |
| 107 | + |
| 108 | + [self setupToolbar]; |
| 109 | +} |
| 110 | + |
| 111 | +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { |
| 112 | + |
| 113 | + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +#pragma mark - |
| 118 | +#pragma mark Action Methods |
| 119 | + |
| 120 | +- (void)setupToolbar { |
| 121 | + |
| 122 | + if(![rWebView canGoBack]) |
| 123 | + backButton.enabled = NO; |
| 124 | + else |
| 125 | + backButton.enabled = YES; |
| 126 | + |
| 127 | + if(![rWebView canGoForward]) |
| 128 | + forwardButton.enabled = NO; |
| 129 | + else |
| 130 | + forwardButton.enabled = YES; |
| 131 | + |
| 132 | + UIBarButtonItem *sSeparator = [[UIBarButtonItem alloc] initWithCustomView:nil]; |
| 133 | + sSeparator.enabled = NO; |
| 134 | + |
| 135 | + if(rWebView.loading) { |
| 136 | + refreshStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:rWebView action:@selector(stopLoading)]; |
| 137 | + sSeparator.width = kSeparatorWidth+4; |
| 138 | + } |
| 139 | + |
| 140 | + else { |
| 141 | + refreshStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:rWebView action:@selector(reload)]; |
| 142 | + sSeparator.width = kSeparatorWidth+3; |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + UIBarButtonItem *rSeparator = [[UIBarButtonItem alloc] initWithCustomView:nil]; |
| 147 | + rSeparator.width = kSeparatorWidth; |
| 148 | + rSeparator.enabled = NO; |
| 149 | + |
| 150 | + NSArray *newButtons = [NSArray arrayWithObjects:backButton, rSeparator, forwardButton, rSeparator, refreshStopButton, sSeparator, actionButton, nil]; |
| 151 | + [toolbar setItems:newButtons]; |
| 152 | + |
| 153 | + [refreshStopButton release]; |
| 154 | + [sSeparator release]; |
| 155 | + [rSeparator release]; |
| 156 | +} |
| 157 | + |
| 158 | +- (void)showActions { |
| 159 | + |
| 160 | + UIActionSheet *actionSheet = [[UIActionSheet alloc] |
| 161 | + initWithTitle: nil |
| 162 | + delegate: self |
| 163 | + cancelButtonTitle: nil |
| 164 | + destructiveButtonTitle: nil |
| 165 | + otherButtonTitles: @"Open in Safari", nil]; |
| 166 | + |
| 167 | + |
| 168 | + if([MFMailComposeViewController canSendMail]) |
| 169 | + [actionSheet addButtonWithTitle:@"Email this"]; |
| 170 | + |
| 171 | + [actionSheet addButtonWithTitle:@"Cancel"]; |
| 172 | + actionSheet.cancelButtonIndex = [actionSheet numberOfButtons]-1; |
| 173 | + |
| 174 | + [actionSheet showFromToolbar:toolbar]; |
| 175 | + [actionSheet release]; |
| 176 | +} |
| 177 | + |
| 178 | +- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 179 | + |
| 180 | + if([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Open in Safari"]) |
| 181 | + [[UIApplication sharedApplication] openURL:rWebView.request.URL]; |
| 182 | + |
| 183 | + else if([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Email this"]) { |
| 184 | + |
| 185 | + MFMailComposeViewController *emailComposer = [[MFMailComposeViewController alloc] init]; |
| 186 | + |
| 187 | + [emailComposer setMailComposeDelegate: self]; |
| 188 | + [emailComposer setSubject:@"Link"]; |
| 189 | + [emailComposer setMessageBody:rWebView.request.URL.absoluteString isHTML:NO]; |
| 190 | + |
| 191 | + [self presentModalViewController:emailComposer animated:YES]; |
| 192 | + [emailComposer release]; |
| 193 | + } |
| 194 | + |
| 195 | +} |
| 196 | + |
| 197 | +- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { |
| 198 | + [controller dismissModalViewControllerAnimated:YES]; |
| 199 | +} |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +@end |
0 commit comments