Skip to content

Commit

Permalink
Merge pull request #4 from OpenWatch/master
Browse files Browse the repository at this point in the history
Support ARC and use delegate and category
  • Loading branch information
nbuggia committed Sep 7, 2013
2 parents 5a3c9f2 + cc60a74 commit 204b464
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@


#import <UIKit/UIKit.h>
#import "MyApplication.h"

@interface UIApplication(Browser)
-(BOOL)openURL:(NSURL *)url forceOpenInSafari:(BOOL)forceOpenInSafari;
@end

@protocol BrowserViewDelegate <NSObject>
- (BOOL)openURL:(NSURL*)url;
@end

// The names of the images for the 'back' and 'forward' buttons in the toolbar.
#define PNG_BUTTON_FORWARD @"right.png"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,47 @@

#import "BrowserViewController.h"

@implementation UIApplication(Browser)

-(BOOL)openURL:(NSURL *)url forceOpenInSafari:(BOOL)forceOpenInSafari
{
if(forceOpenInSafari)
{
// We're overriding our app trying to open this URL, so we'll let UIApplication federate this request back out
// through the normal channels. The return value states whether or not they were able to open the URL.
return [self openURL:url];
}

//
// Otherwise, we'll see if it is a request that we should let our app open.

BOOL couldWeOpenUrl = NO;

NSString* scheme = [url.scheme lowercaseString];
if([scheme compare:@"http"] == NSOrderedSame
|| [scheme compare:@"https"] == NSOrderedSame)
{
// TODO - Here you might also want to check for other conditions where you do not want your app opening URLs (e.g.
// Facebook authentication requests, OAUTH requests, etc)

// TODO - Update the cast below with the name of your AppDelegate
// Let's call the method you wrote on your AppDelegate to actually open the BrowserViewController
couldWeOpenUrl = [(id<BrowserViewDelegate>)self.delegate openURL:url];
}

if(!couldWeOpenUrl)
{
return [self openURL:url];
}
else
{
return YES;
}
}
@end



@implementation BrowserViewController

@synthesize webView;
Expand All @@ -49,7 +90,7 @@ - (void)actionSheet:(UIActionSheet *)uias clickedButtonAtIndex:(NSInteger)button
// user pressed "Open in Safari"
if([[uias buttonTitleAtIndex:buttonIndex] compare:ACTION_OPEN_IN_SAFARI] == NSOrderedSame)
{
[(MyApplication*)[UIApplication sharedApplication] openURL:self.url forceOpenInSafari:YES];
[[UIApplication sharedApplication] openURL:self.url forceOpenInSafari:YES];
}

// TODO add your own actions here, like email the URL.
Expand All @@ -60,23 +101,6 @@ - (void)actionSheet:(UIActionSheet *)uias clickedButtonAtIndex:(NSInteger)button
#pragma mark - Object lifecycle


- (void)dealloc
{
[webView setDelegate:nil];
[webView release];
[url release];
[activityIndicator release];

[forwardButton release];
[backButton release];
[stopButton release];
[reloadButton release];
[actionButton release];

[super dealloc];
}


- (id)initWithUrls:(NSURL*)u
{
self = [self initWithNibName:@"BrowserViewController" bundle:nil];
Expand Down Expand Up @@ -140,8 +164,6 @@ - (void)updateToolbar
if([activityIndicator isAnimating]) [toolbarButtons replaceObjectAtIndex:4 withObject:self.stopButton];

[self.toolbar setItems:toolbarButtons animated:YES];
[toolbarButtons release];
[flexibleSpace release];

// page title
NSString *pageTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
Expand Down Expand Up @@ -183,7 +205,7 @@ - (void)viewDidLoad
[super viewDidLoad];

self.webView.scalesPageToFit = YES;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:activityIndicator] autorelease];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];

[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
[self updateToolbar];
Expand Down Expand Up @@ -248,7 +270,6 @@ - (void)actionButtonPressed:(id)sender
otherButtonTitles:ACTION_OPEN_IN_SAFARI, nil];

[uias showInView:self.view];
[uias release];
}


Expand Down

0 comments on commit 204b464

Please sign in to comment.