Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mode Modal with optional navigation bar. #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
6FBF4B7D14F7EACF0048C98E /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "EmbeddedYoutubePatch/EmbeddedYoutubePatch-Prefix.pch";
INFOPLIST_FILE = "EmbeddedYoutubePatch/EmbeddedYoutubePatch-Info.plist";
Expand All @@ -298,6 +299,7 @@
6FBF4B7E14F7EACF0048C98E /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "EmbeddedYoutubePatch/EmbeddedYoutubePatch-Prefix.pch";
INFOPLIST_FILE = "EmbeddedYoutubePatch/EmbeddedYoutubePatch-Info.plist";
Expand Down Expand Up @@ -325,6 +327,7 @@
6FBF4B7E14F7EACF0048C98E /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
9 changes: 5 additions & 4 deletions TSMiniWebBrowser/TSMiniWebBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ typedef enum {
@property (nonatomic, assign) BOOL showPageTitleOnTitleBar;
@property (nonatomic, assign) BOOL showReloadButton;
@property (nonatomic, assign) BOOL showActionButton;
@property (nonatomic, assign) BOOL showNavigationBarInModalMode; // Only used in modal mode
@property (nonatomic, assign) UIBarStyle barStyle;
@property (nonatomic, strong) UIColor *barTintColor;
@property (nonatomic, strong) NSString *modalDismissButtonTitle;
@property (nonatomic, strong) NSString *domainLockList;
@property (nonatomic, strong) NSString *currentURL;
@property (nonatomic, retain) UIColor *barTintColor;
@property (nonatomic, retain) NSString *modalDismissButtonTitle;
@property (nonatomic, retain) NSString *domainLockList;
@property (nonatomic, retain) NSString *currentURL;

// Public Methods
- (id)initWithUrl:(NSURL*)url;
Expand Down
53 changes: 33 additions & 20 deletions TSMiniWebBrowser/TSMiniWebBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ @implementation TSMiniWebBrowser
@synthesize barTintColor;
@synthesize domainLockList;
@synthesize currentURL;
@synthesize showNavigationBarInModalMode;

#define kToolBarHeight 44
#define kTabBarHeight 49
Expand Down Expand Up @@ -88,21 +89,26 @@ -(void) dismissController {
}
}

-(UIBarButtonItem *)doneButton{
return [[[UIBarButtonItem alloc] initWithTitle:modalDismissButtonTitle style:UIBarButtonItemStyleBordered target:self action:@selector(dismissController)] autorelease];
}

//Added in the dealloc method to remove the webview delegate, because if you use this in a navigation controller
//TSMiniWebBrowser can get deallocated while the page is still loading and the web view will call its delegate-- resulting in a crash
-(void)dealloc
{
[webView setDelegate:nil];
[modalDismissButtonTitle release];
[domainLockList release];
[currentURL release];
[super dealloc];
}

#pragma mark - Init

// This method is only used in modal mode
-(void) initTitleBar {
UIBarButtonItem *buttonDone = [[UIBarButtonItem alloc] initWithTitle:modalDismissButtonTitle style:UIBarButtonItemStyleBordered target:self action:@selector(dismissController)];

UINavigationItem *titleBar = [[UINavigationItem alloc] initWithTitle:@""];
titleBar.leftBarButtonItem = buttonDone;
UINavigationItem *titleBar = [[[UINavigationItem alloc] initWithTitle:@""] autorelease];
titleBar.leftBarButtonItem = [self doneButton];

CGFloat width = self.view.frame.size.width;
navigationBarModal = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
Expand Down Expand Up @@ -132,29 +138,29 @@ -(void) initToolBar {

buttonGoBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouchUp:)];

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *fixedSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
fixedSpace.width = 30;

buttonGoForward = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(forwardButtonTouchUp:)];

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

UIBarButtonItem *buttonReload = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reload_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(reloadButtonTouchUp:)];
UIBarButtonItem *buttonReload = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reload_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(reloadButtonTouchUp:)] autorelease];

UIBarButtonItem *fixedSpace2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *fixedSpace2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
fixedSpace2.width = 20;

UIBarButtonItem *buttonAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonActionTouchUp:)];
UIBarButtonItem *buttonAction = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonActionTouchUp:)] autorelease];

// Activity indicator is a bit special
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(11, 7, 20, 20);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 43, 33)];
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 43, 33)] autorelease];
[containerView addSubview:activityIndicator];
UIBarButtonItem *buttonContainer = [[UIBarButtonItem alloc] initWithCustomView:containerView];
UIBarButtonItem *buttonContainer = [[[UIBarButtonItem alloc] initWithCustomView:containerView] autorelease];

// Add butons to an array
NSMutableArray *toolBarButtons = [[NSMutableArray alloc] init];
NSMutableArray *toolBarButtons = [[[NSMutableArray alloc] init] autorelease];
[toolBarButtons addObject:buttonGoBack];
[toolBarButtons addObject:fixedSpace];
[toolBarButtons addObject:buttonGoForward];
Expand All @@ -167,6 +173,9 @@ -(void) initToolBar {
[toolBarButtons addObject:fixedSpace2];
[toolBarButtons addObject:buttonAction];
}
if(showNavigationBarInModalMode == NO){
[toolBarButtons addObject:[self doneButton]];
}

// Set buttons to tool bar
[toolBar setItems:toolBarButtons animated:YES];
Expand All @@ -178,7 +187,9 @@ -(void) initToolBar {
-(void) initWebView {
CGSize viewSize = self.view.frame.size;
if (mode == TSMiniWebBrowserModeModal) {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, kToolBarHeight, viewSize.width, viewSize.height-kToolBarHeight*2)];
int webY = showNavigationBarInModalMode ? kToolBarHeight : 0;
int webHeight = showNavigationBarInModalMode ? kToolBarHeight*2 : kToolBarHeight*1;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, webY, viewSize.width, viewSize.height-webHeight)];
} else if(mode == TSMiniWebBrowserModeNavigation) {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height-kToolBarHeight)];
} else if(mode == TSMiniWebBrowserModeTabBar) {
Expand Down Expand Up @@ -208,6 +219,7 @@ - (id)initWithUrl:(NSURL*)url {
// Defaults
mode = TSMiniWebBrowserModeNavigation;
showURLStringOnActionSheetTitle = YES;
showNavigationBarInModalMode = YES;
showPageTitleOnTitleBar = YES;
showReloadButton = YES;
showActionButton = YES;
Expand Down Expand Up @@ -247,8 +259,8 @@ - (void)viewDidLoad
// Init web view
[self initWebView];

// Init title bar if presented modally
if (mode == TSMiniWebBrowserModeModal) {
// Init title bar if presented modally and needed
if (mode == TSMiniWebBrowserModeModal && showNavigationBarInModalMode) {
[self initTitleBar];
}

Expand Down Expand Up @@ -301,7 +313,7 @@ -(void) viewWillDisappear:(BOOL)animated {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
return YES;
}

/* Fix for landscape + zooming webview bug.
Expand Down Expand Up @@ -350,7 +362,7 @@ - (void)showActionSheet {
NSURL* url = [webView.request URL];
urlString = [url absoluteString];
}
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
UIActionSheet *actionSheet = [[[UIActionSheet alloc] init] autorelease];
actionSheet.title = urlString;
actionSheet.delegate = self;
[actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Safari", nil)];
Expand Down Expand Up @@ -470,7 +482,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
currentURL = request.URL.absoluteString;
self.currentURL = request.URL.absoluteString;
}

return YES;
Expand Down Expand Up @@ -500,7 +512,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
currentURL = request.URL.absoluteString;
self.currentURL = request.URL.absoluteString;
}

return YES;
Expand Down Expand Up @@ -544,6 +556,7 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"OK", nil), nil];
[alert show];
[alert release];
}

@end
2 changes: 2 additions & 0 deletions TSMiniWebBrowserDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
6F69CDCF14C6339C00BB6C57 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "TSMiniWebBrowserDemo/TSMiniWebBrowserDemo-Prefix.pch";
INFOPLIST_FILE = "TSMiniWebBrowserDemo/TSMiniWebBrowserDemo-Info.plist";
Expand All @@ -330,6 +331,7 @@
6F69CDD014C6339C00BB6C57 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "TSMiniWebBrowserDemo/TSMiniWebBrowserDemo-Prefix.pch";
INFOPLIST_FILE = "TSMiniWebBrowserDemo/TSMiniWebBrowserDemo-Info.plist";
Expand Down
3 changes: 2 additions & 1 deletion TSMiniWebBrowserDemo/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@interface ViewController : UIViewController <TSMiniWebBrowserDelegate> {

}
- (IBAction)buttonTouchUp:(id)sender;
- (IBAction)navigationTouchUp:(id)sender;
- (IBAction)modalTouchUp:(id)sender;

@end
79 changes: 21 additions & 58 deletions TSMiniWebBrowserDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,34 @@

@implementation ViewController

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)buttonTouchUp:(id)sender {
- (IBAction)navigationTouchUp:(id)sender {
TSMiniWebBrowser *webBrowser = [[TSMiniWebBrowser alloc] initWithUrl:[NSURL URLWithString:@"http://indiedevstories.com"]];
webBrowser.delegate = self;
// webBrowser.showURLStringOnActionSheetTitle = YES;
// webBrowser.showPageTitleOnTitleBar = YES;
// webBrowser.showActionButton = YES;
// webBrowser.showReloadButton = YES;
// [webBrowser setFixedTitleBarText:@"Test Title Text"]; // This has priority over "showPageTitleOnTitleBar".
// webBrowser.showURLStringOnActionSheetTitle = YES;
// webBrowser.showPageTitleOnTitleBar = YES;
// webBrowser.showActionButton = YES;
// webBrowser.showReloadButton = YES;
// [webBrowser setFixedTitleBarText:@"Test Title Text"]; // This has priority over "showPageTitleOnTitleBar".
webBrowser.mode = TSMiniWebBrowserModeNavigation;

webBrowser.barStyle = UIBarStyleBlack;

if (webBrowser.mode == TSMiniWebBrowserModeModal) {
webBrowser.modalDismissButtonTitle = @"Home";
[self presentModalViewController:webBrowser animated:YES];
} else if(webBrowser.mode == TSMiniWebBrowserModeNavigation) {
[self.navigationController pushViewController:webBrowser animated:YES];
}
[self.navigationController pushViewController:webBrowser animated:YES];
[webBrowser release];
}

- (IBAction)modalTouchUp:(id)sender {
TSMiniWebBrowser *webBrowser = [[TSMiniWebBrowser alloc] initWithUrl:[NSURL URLWithString:@"http://indiedevstories.com"]];
webBrowser.delegate = self;
webBrowser.mode = TSMiniWebBrowserModeModal;
webBrowser.barStyle = UIBarStyleBlack;
webBrowser.showActionButton = YES;
//webBrowser.showNavigationBarInModalMode = NO;

webBrowser.modalDismissButtonTitle = @"Close";
[self presentModalViewController:webBrowser animated:YES];
[webBrowser release];
}

#pragma mark - TSMiniWebBrowserDelegate
Expand Down
Loading