Skip to content

Commit

Permalink
Initial Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
designatednerd committed Jul 14, 2012
1 parent 6990b6a commit fe5244c
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 34 deletions.
46 changes: 42 additions & 4 deletions BrowserChooser.xcodeproj/project.pbxproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,22 +15,37 @@
F57FB4AE15B1F25C006A04B0 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F57FB4AD15B1F25C006A04B0 /* AppDelegate.m */; }; F57FB4AE15B1F25C006A04B0 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F57FB4AD15B1F25C006A04B0 /* AppDelegate.m */; };
F57FB4B115B1F25C006A04B0 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F57FB4B015B1F25C006A04B0 /* ViewController.m */; }; F57FB4B115B1F25C006A04B0 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F57FB4B015B1F25C006A04B0 /* ViewController.m */; };
F57FB4B415B1F25C006A04B0 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4B215B1F25C006A04B0 /* ViewController.xib */; }; F57FB4B415B1F25C006A04B0 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4B215B1F25C006A04B0 /* ViewController.xib */; };
F57FB4BC15B1F9A9006A04B0 /* NSURL+BrowserChooser.m in Sources */ = {isa = PBXBuildFile; fileRef = F57FB4BB15B1F9A9006A04B0 /* NSURL+BrowserChooser.m */; };
F57FB4C215B20483006A04B0 /* Icon-Small.png in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4BE15B20483006A04B0 /* Icon-Small.png */; };
F57FB4C315B20483006A04B0 /* Icon-Small@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4BF15B20483006A04B0 /* Icon-Small@2x.png */; };
F57FB4C415B20483006A04B0 /* Icon.png in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4C015B20483006A04B0 /* Icon.png */; };
F57FB4C515B20483006A04B0 /* Icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4C115B20483006A04B0 /* Icon@2x.png */; };
F57FB4C715B207CC006A04B0 /* MIT License.txt in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4C615B207CC006A04B0 /* MIT License.txt */; };
F57FB4C915B20958006A04B0 /* README.mdown in Resources */ = {isa = PBXBuildFile; fileRef = F57FB4C815B20958006A04B0 /* README.mdown */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */


/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
F57FB49915B1F25A006A04B0 /* BrowserChooser.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BrowserChooser.app; sourceTree = BUILT_PRODUCTS_DIR; }; F57FB49915B1F25A006A04B0 /* BrowserChooser.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BrowserChooser.app; sourceTree = BUILT_PRODUCTS_DIR; };
F57FB49D15B1F25A006A04B0 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; F57FB49D15B1F25A006A04B0 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
F57FB49F15B1F25A006A04B0 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; F57FB49F15B1F25A006A04B0 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
F57FB4A115B1F25A006A04B0 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; F57FB4A115B1F25A006A04B0 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
F57FB4A515B1F25A006A04B0 /* BrowserChooser-Info.plist */ = {isa = PBXFileReference; path = "BrowserChooser-Info.plist"; sourceTree = "<group>"; }; F57FB4A515B1F25A006A04B0 /* BrowserChooser-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "BrowserChooser-Info.plist"; sourceTree = "<group>"; };
F57FB4A715B1F25B006A04B0 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; }; F57FB4A715B1F25B006A04B0 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
F57FB4A915B1F25B006A04B0 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; }; F57FB4A915B1F25B006A04B0 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
F57FB4AB15B1F25B006A04B0 /* BrowserChooser-Prefix.pch */ = {isa = PBXFileReference; path = "BrowserChooser-Prefix.pch"; sourceTree = "<group>"; }; F57FB4AB15B1F25B006A04B0 /* BrowserChooser-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "BrowserChooser-Prefix.pch"; sourceTree = "<group>"; };
F57FB4AC15B1F25B006A04B0 /* AppDelegate.h */ = {isa = PBXFileReference; path = AppDelegate.h; sourceTree = "<group>"; }; F57FB4AC15B1F25B006A04B0 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
F57FB4AD15B1F25C006A04B0 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; }; F57FB4AD15B1F25C006A04B0 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
F57FB4AF15B1F25C006A04B0 /* ViewController.h */ = {isa = PBXFileReference; path = ViewController.h; sourceTree = "<group>"; }; F57FB4AF15B1F25C006A04B0 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
F57FB4B015B1F25C006A04B0 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; }; F57FB4B015B1F25C006A04B0 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
F57FB4B315B1F25C006A04B0 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = "<group>"; }; F57FB4B315B1F25C006A04B0 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = "<group>"; };
F57FB4BA15B1F9A9006A04B0 /* NSURL+BrowserChooser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURL+BrowserChooser.h"; sourceTree = "<group>"; };
F57FB4BB15B1F9A9006A04B0 /* NSURL+BrowserChooser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+BrowserChooser.m"; sourceTree = "<group>"; };
F57FB4BE15B20483006A04B0 /* Icon-Small.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-Small.png"; sourceTree = "<group>"; };
F57FB4BF15B20483006A04B0 /* Icon-Small@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-Small@2x.png"; sourceTree = "<group>"; };
F57FB4C015B20483006A04B0 /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon.png; sourceTree = "<group>"; };
F57FB4C115B20483006A04B0 /* Icon@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon@2x.png"; sourceTree = "<group>"; };
F57FB4C615B207CC006A04B0 /* MIT License.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "MIT License.txt"; sourceTree = "<group>"; };
F57FB4C815B20958006A04B0 /* README.mdown */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README.mdown; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */


/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
Expand All @@ -50,6 +65,7 @@
F57FB48E15B1F256006A04B0 = { F57FB48E15B1F256006A04B0 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
F57FB4C815B20958006A04B0 /* README.mdown */,
F57FB4A315B1F25A006A04B0 /* BrowserChooser */, F57FB4A315B1F25A006A04B0 /* BrowserChooser */,
F57FB49C15B1F25A006A04B0 /* Frameworks */, F57FB49C15B1F25A006A04B0 /* Frameworks */,
F57FB49A15B1F25A006A04B0 /* Products */, F57FB49A15B1F25A006A04B0 /* Products */,
Expand Down Expand Up @@ -79,9 +95,12 @@
children = ( children = (
F57FB4AC15B1F25B006A04B0 /* AppDelegate.h */, F57FB4AC15B1F25B006A04B0 /* AppDelegate.h */,
F57FB4AD15B1F25C006A04B0 /* AppDelegate.m */, F57FB4AD15B1F25C006A04B0 /* AppDelegate.m */,
F57FB4BA15B1F9A9006A04B0 /* NSURL+BrowserChooser.h */,
F57FB4BB15B1F9A9006A04B0 /* NSURL+BrowserChooser.m */,
F57FB4AF15B1F25C006A04B0 /* ViewController.h */, F57FB4AF15B1F25C006A04B0 /* ViewController.h */,
F57FB4B015B1F25C006A04B0 /* ViewController.m */, F57FB4B015B1F25C006A04B0 /* ViewController.m */,
F57FB4B215B1F25C006A04B0 /* ViewController.xib */, F57FB4B215B1F25C006A04B0 /* ViewController.xib */,
F57FB4BD15B20483006A04B0 /* icons */,
F57FB4A415B1F25A006A04B0 /* Supporting Files */, F57FB4A415B1F25A006A04B0 /* Supporting Files */,
); );
path = BrowserChooser; path = BrowserChooser;
Expand All @@ -94,10 +113,22 @@
F57FB4A615B1F25B006A04B0 /* InfoPlist.strings */, F57FB4A615B1F25B006A04B0 /* InfoPlist.strings */,
F57FB4A915B1F25B006A04B0 /* main.m */, F57FB4A915B1F25B006A04B0 /* main.m */,
F57FB4AB15B1F25B006A04B0 /* BrowserChooser-Prefix.pch */, F57FB4AB15B1F25B006A04B0 /* BrowserChooser-Prefix.pch */,
F57FB4C615B207CC006A04B0 /* MIT License.txt */,
); );
name = "Supporting Files"; name = "Supporting Files";
sourceTree = "<group>"; sourceTree = "<group>";
}; };
F57FB4BD15B20483006A04B0 /* icons */ = {
isa = PBXGroup;
children = (
F57FB4BE15B20483006A04B0 /* Icon-Small.png */,
F57FB4BF15B20483006A04B0 /* Icon-Small@2x.png */,
F57FB4C015B20483006A04B0 /* Icon.png */,
F57FB4C115B20483006A04B0 /* Icon@2x.png */,
);
path = icons;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */


/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -151,6 +182,12 @@
files = ( files = (
F57FB4A815B1F25B006A04B0 /* InfoPlist.strings in Resources */, F57FB4A815B1F25B006A04B0 /* InfoPlist.strings in Resources */,
F57FB4B415B1F25C006A04B0 /* ViewController.xib in Resources */, F57FB4B415B1F25C006A04B0 /* ViewController.xib in Resources */,
F57FB4C215B20483006A04B0 /* Icon-Small.png in Resources */,
F57FB4C315B20483006A04B0 /* Icon-Small@2x.png in Resources */,
F57FB4C415B20483006A04B0 /* Icon.png in Resources */,
F57FB4C515B20483006A04B0 /* Icon@2x.png in Resources */,
F57FB4C715B207CC006A04B0 /* MIT License.txt in Resources */,
F57FB4C915B20958006A04B0 /* README.mdown in Resources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand All @@ -164,6 +201,7 @@
F57FB4AA15B1F25B006A04B0 /* main.m in Sources */, F57FB4AA15B1F25B006A04B0 /* main.m in Sources */,
F57FB4AE15B1F25C006A04B0 /* AppDelegate.m in Sources */, F57FB4AE15B1F25C006A04B0 /* AppDelegate.m in Sources */,
F57FB4B115B1F25C006A04B0 /* ViewController.m in Sources */, F57FB4B115B1F25C006A04B0 /* ViewController.m in Sources */,
F57FB4BC15B1F9A9006A04B0 /* NSURL+BrowserChooser.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down
2 changes: 1 addition & 1 deletion BrowserChooser/AppDelegate.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BrowserChooser // BrowserChooser
// //
// Created by Ellen Shapiro on 7/14/12. // Created by Ellen Shapiro on 7/14/12.
// Copyright (c) 2012 Designated Nerd Software. All rights reserved. // Copyright (c) 2012 Designated Nerd Software.
// //


#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
Expand Down
2 changes: 1 addition & 1 deletion BrowserChooser/AppDelegate.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BrowserChooser // BrowserChooser
// //
// Created by Ellen Shapiro on 7/14/12. // Created by Ellen Shapiro on 7/14/12.
// Copyright (c) 2012 Designated Nerd Software. All rights reserved. // Copyright (c) 2012 Designated Nerd Software.
// //


#import "AppDelegate.h" #import "AppDelegate.h"
Expand Down
13 changes: 12 additions & 1 deletion BrowserChooser/BrowserChooser-Info.plist
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>en</string> <string>en</string>
<key>CFBundleDisplayName</key> <key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string> <string>Chooser</string>
<key>CFBundleExecutable</key> <key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string> <string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIconFiles</key>
<array>
<string>Icon.png</string>
<string>Icon@2x.png</string>
<string>Icon-Small.png</string>
<string>Icon-Small@2x.png</string>
</array>
<key>CFBundleIdentifier</key> <key>CFBundleIdentifier</key>
<string>com.designatednerd.${PRODUCT_NAME:rfc1034identifier}</string> <string>com.designatednerd.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key> <key>CFBundleInfoDictionaryVersion</key>
Expand All @@ -24,6 +33,8 @@
<string>1.0</string> <string>1.0</string>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>
<true/> <true/>
<key>UIPrerenderedIcon</key>
<true/>
<key>UIRequiredDeviceCapabilities</key> <key>UIRequiredDeviceCapabilities</key>
<array> <array>
<string>armv7</string> <string>armv7</string>
Expand Down
7 changes: 7 additions & 0 deletions BrowserChooser/MIT License.txt
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2012 Designated Nerd Software.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRNATY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions BrowserChooser/NSURL+BrowserChooser.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// NSURL+BrowserChooser.h
// BrowserChooser
//
// Created by Ellen Shapiro on 7/14/12.
// Copyright (c) 2012 Designated Nerd Software.
//



#import <Foundation/Foundation.h>

/**
* Enum for supported browser types.
*/
typedef enum {
BrowserTypeMobileSafari = 0,
BrowserTypeChromeForiOS
} BrowserType;

@interface NSURL (BrowserChooser)

/**
* Opens the NSURL in the indicated browser.
* @param browserType - The BrowserType as defined in the above enum you wish to use.
*/
-(void)openInBrowser:(BrowserType)browserType;
@end
66 changes: 66 additions & 0 deletions BrowserChooser/NSURL+BrowserChooser.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// NSURL+BrowserChooser.m
// BrowserChooser
//
// Created by Ellen Shapiro on 7/14/12.
// Copyright (c) 2012 Designated Nerd Software.
//

#import "NSURL+BrowserChooser.h"

@implementation NSURL (BrowserChooser)

-(void)openInBrowser:(BrowserType)browserType
{
switch (browserType) {
case BrowserTypeMobileSafari:
//Open in default browser. This part's easy.
[[UIApplication sharedApplication] openURL:self];
break;
case BrowserTypeChromeForiOS:
//Check to make sure chrome is installed first:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome://"]]) {
NSString *originalScheme = [self scheme];

NSString *chromeSchemeToUse = nil;
if ([originalScheme isEqualToString:@"http"]) {
chromeSchemeToUse = @"googlechrome";
} else if ([originalScheme isEqualToString:@"https"]) {
chromeSchemeToUse = @"googlechromes";
}

//Proceed if there is a valid URL scheme available (things like ftp etc. will fail):
if (chromeSchemeToUse) {
NSString *absoluteString = [self absoluteString];
NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
NSString *urlNoScheme = [absoluteString substringFromIndex:rangeForScheme.location];
NSString *chromeURLString = [chromeSchemeToUse stringByAppendingString:urlNoScheme];
NSURL *chromeURL = [NSURL URLWithString:chromeURLString];

[[UIApplication sharedApplication] openURL:chromeURL];
} else {
//Inform the user the scheme could not be opened.
NSString *title = NSLocalizedString(@"Sorry!", @"Sorry");
NSString *messagePrefix = NSLocalizedString(@"Google Chrome for iOS cannot open URLs that begin with", @"Chrome can't open prefix");
NSString *message = [NSString stringWithFormat:@"%@ %@.", messagePrefix, originalScheme];
NSString *ok = NSLocalizedString(@"OK", @"OK");

UIAlertView *noSchemeForYou = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:ok otherButtonTitles:nil];
[noSchemeForYou show];
}
} else {
//Inform the user they need to install chrome.
NSString *title = NSLocalizedString(@"Oops!", @"Oops");
NSString *message = NSLocalizedString(@"Chrome is not installed on your device. Please install Chrome on your device if you wish to open links with Chrome.", @"Chrome not installed, please install.");
NSString *ok = NSLocalizedString(@"OK", @"OK");
UIAlertView *noChromeForYou = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:ok otherButtonTitles:nil];
[noChromeForYou show];
}
break;
default:
NSLog(@"Unknown browser type. Please check your code and try again.");
break;
}
}

@end
8 changes: 6 additions & 2 deletions BrowserChooser/ViewController.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
// BrowserChooser // BrowserChooser
// //
// Created by Ellen Shapiro on 7/14/12. // Created by Ellen Shapiro on 7/14/12.
// Copyright (c) 2012 Designated Nerd Software. All rights reserved. // Copyright (c) 2012 Designated Nerd Software.
// //


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


@interface ViewController : UIViewController @interface ViewController : UIViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>


@property (strong, nonatomic) IBOutlet UIButton *prefixButton;
@property (strong, nonatomic) IBOutlet UITextField *urlTextField;
@property (strong, nonatomic) IBOutlet UIButton *openInSafariButton;
@property (strong, nonatomic) IBOutlet UIButton *openInChromeButton;
@end @end
97 changes: 89 additions & 8 deletions BrowserChooser/ViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,32 +3,113 @@
// BrowserChooser // BrowserChooser
// //
// Created by Ellen Shapiro on 7/14/12. // Created by Ellen Shapiro on 7/14/12.
// Copyright (c) 2012 Designated Nerd Software. All rights reserved. // Copyright (c) 2012 Designated Nerd Software.
// //


#import "ViewController.h" #import "ViewController.h"
#import "NSURL+BrowserChooser.h"


@interface ViewController () @interface ViewController ()


@property (nonatomic, strong) NSArray *prefixTypes;
@property (nonatomic, strong) UIPickerView *prefixPicker;
@end @end


@implementation ViewController @implementation ViewController
@synthesize prefixButton = _prefixButton;
@synthesize urlTextField = _urlTextField;
@synthesize openInSafariButton = _openInSafariButton;
@synthesize openInChromeButton = _openInChromeButton;
@synthesize prefixTypes = _prefixTypes;
@synthesize prefixPicker = _prefixPicker;


#pragma mark - View Lifecycle
- (void)viewDidLoad - (void)viewDidLoad
{ {
[super viewDidLoad]; [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //Setup supported prefix types.
_prefixTypes = [NSArray arrayWithObjects:@"http://", @"https://", nil];
} }


- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} }


#pragma mark - IBActions
-(IBAction)launchPrefixPicker
{
//If picker is null, set it up.
if (!_prefixPicker) {
_prefixPicker = [[UIPickerView alloc] init];
_prefixPicker.dataSource = self;
_prefixPicker.delegate = self;
_prefixPicker.showsSelectionIndicator = YES;
_prefixPicker.frame = CGRectOffset(_prefixPicker.frame, 0, self.view.frame.size.height);
}

//Add picker to view and animate in.
[self.view addSubview:_prefixPicker];
[UIView animateWithDuration:0.4 animations:^{
_prefixPicker.frame = CGRectOffset(_prefixPicker.frame, 0, -_prefixPicker.frame.size.height);
}];
}

-(IBAction)launchURL:(UIButton *)sender
{
//Construct URL based on the prefix and entered text.
NSString *prefix = _prefixButton.titleLabel.text;
NSString *www = _urlTextField.text;
NSString *urlString = [prefix stringByAppendingString:www];
NSURL *urlToOpen = [NSURL URLWithString:urlString];

//Open in the appropriate browser based on selected button.
if (sender == _openInSafariButton) {
NSLog(@"Open %@ in Safari", urlString);
[urlToOpen openInBrowser:BrowserTypeMobileSafari];
} else if (sender == _openInChromeButton) {
NSLog(@"Open %@ in Chrome", urlString);
[urlToOpen openInBrowser:BrowserTypeChromeForiOS];
} else {
NSLog(@"Unknown Sender. Ya done goofed, son.");
}
}

#pragma mark - UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [_prefixTypes count];
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_prefixTypes objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Update prefix button title
[_prefixButton setTitle:[_prefixTypes objectAtIndex:row] forState:UIControlStateNormal];

//Animate picker out then remove from view.
[UIView animateWithDuration:0.4 animations:^{
_prefixPicker.frame = CGRectOffset(_prefixPicker.frame, 0, _prefixPicker.frame.size.height);
} completion:^(BOOL finished) {
[_prefixPicker removeFromSuperview];
}];
}

#pragma mark - UITextFieldDelegate methods
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
//If the user hits return, dismiss the keyboard rather than adding a newline.
[_urlTextField resignFirstResponder];
return NO;
}
@end @end
Loading

0 comments on commit fe5244c

Please sign in to comment.