Skip to content

Commit 36c5894

Browse files
committed
Initial commit.
0 parents  commit 36c5894

21 files changed

+5119
-0
lines changed

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2011 Sam Vermette
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.textile

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
h1. SVWebViewController
2+
3+
SVWebViewController is a standard inline browser for iPhone. It's highly inspired by Tweetie's inline browser, which provides basic functionality with a UI design similar to Mobile Safari's. SVWebViewController features:
4+
5+
* back and forward buttons (get auto-enabled appropriately)
6+
* a stop/refresh button
7+
* a standard action button ("Open in Safari" and "Email this")
8+
* navbar auto-creation depending on how controller is presented
9+
* conforms to setNetworkActivityIndicatorVisible
10+
11+
!http://samvermette.com/files/svwebviewcontroller/push.png!
12+
!http://samvermette.com/files/svwebviewcontroller/present.png!
13+
!http://samvermette.com/files/svwebviewcontroller/actions.png!
14+
15+
h2. Installation
16+
17+
Easiest way to install is to drag the SVWebViewController/SVWebViewController folder from the Finder directly into your project. That will add the SVWebViewController controller class, the .xib file and the image ressources (back and forward buttons, 1x and 2x) to your project. SVWebViewController also requires the *MessageUI* framework, so make sure you add that to your project as well.
18+
19+
h2. Usage:
20+
21+
Just like any UIViewController, SVWebViewController can be pushed into a UINavigationController stack:
22+
23+
<pre>
24+
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:@"http://google.com"];
25+
[self.navigationController pushViewController:webViewController animated:YES];
26+
[webViewController release];
27+
</pre>
28+
29+
or be presented modally on top of the currently visible view controller:
30+
31+
<pre>
32+
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:@"http://google.com"];
33+
[self presentModalViewController:webViewController animated:YES];
34+
[webViewController release];
35+
</pre>
36+
37+
If you want the UINavigationBar to have a title, make sure you it __before__ you push the SVWebViewController:
38+
39+
<pre>
40+
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:@"http://google.com"];
41+
webViewController.title = @"Web Browser";
42+
[self presentModalViewController:webViewController animated:YES];
43+
[webViewController release];
44+
</pre>
207 Bytes
Loading
318 Bytes
Loading
197 Bytes
Loading
312 Bytes
Loading
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// SVWebViewController.h
3+
//
4+
// Created by Sam Vermette on 08.11.10.
5+
// Copyright 2010 Sam Vermette. All rights reserved.
6+
//
7+
8+
#import <MessageUI/MessageUI.h>
9+
10+
11+
@interface SVWebViewController : UIViewController <UIWebViewDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate> {
12+
IBOutlet UIWebView *rWebView;
13+
IBOutlet UIBarButtonItem *backButton, *forwardButton, *refreshStopButton, *actionButton;
14+
IBOutlet UIToolbar *toolbar;
15+
}
16+
17+
@property (nonatomic, retain) NSString *urlString;
18+
19+
- (id)initWithAddress:(NSString*)string;
20+
21+
@end
+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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

Comments
 (0)