Permalink
Browse files

closes #13 - allow opening helpful links in Chrome, 1P

  • Loading branch information...
1 parent 9ca894f commit 3f839279170058eed3eaad7d3723bed7fe639d4f @cdzombak committed Mar 11, 2013
@@ -58,6 +58,8 @@
93ADFEE1168F487F00F8256C /* BVMAddEditServerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93ADFEE0168F487F00F8256C /* BVMAddEditServerViewController.m */; };
93BADD411683C19900B0CD6F /* UIColor+BVMColors.m in Sources */ = {isa = PBXBuildFile; fileRef = 93BADD401683C19900B0CD6F /* UIColor+BVMColors.m */; };
93BADD481684DE9D00B0CD6F /* BVMAPIResponseParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 93BADD471684DE9D00B0CD6F /* BVMAPIResponseParser.m */; };
+ 93E5D84416EE727B00C57111 /* BVMBrowserSelectorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E5D84316EE727A00C57111 /* BVMBrowserSelectorViewController.m */; };
+ 93E5D84716EE73D400C57111 /* BVMLinkOpenManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 93E5D84616EE73D200C57111 /* BVMLinkOpenManager.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -140,6 +142,10 @@
93BADD491684E47A00B0CD6F /* NSError+BVMErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+BVMErrors.h"; sourceTree = "<group>"; };
93BADD4A1684E47A00B0CD6F /* NSError+BVMErrors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+BVMErrors.m"; sourceTree = "<group>"; };
93BADD4C1684E60A00B0CD6F /* BVMErrorDomain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BVMErrorDomain.h; sourceTree = "<group>"; };
+ 93E5D84216EE727900C57111 /* BVMBrowserSelectorViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BVMBrowserSelectorViewController.h; sourceTree = "<group>"; };
+ 93E5D84316EE727A00C57111 /* BVMBrowserSelectorViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BVMBrowserSelectorViewController.m; sourceTree = "<group>"; };
+ 93E5D84516EE73D200C57111 /* BVMLinkOpenManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BVMLinkOpenManager.h; sourceTree = "<group>"; };
+ 93E5D84616EE73D200C57111 /* BVMLinkOpenManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BVMLinkOpenManager.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -206,6 +212,8 @@
937CCC441684F5B70075BD01 /* BVMErrorDomain.m */,
93270A031682726F0013DEB5 /* BVMHumanValueTransformer.h */,
93270A041682726F0013DEB5 /* BVMHumanValueTransformer.m */,
+ 93E5D84516EE73D200C57111 /* BVMLinkOpenManager.h */,
+ 93E5D84616EE73D200C57111 /* BVMLinkOpenManager.m */,
935FEECF1682BC3700BE3892 /* BVMServersManager.h */,
935FEED01682BC3700BE3892 /* BVMServersManager.m */,
938662D81691F7F60049D6A1 /* CDZViewControllerModalDismissal.h */,
@@ -268,6 +276,8 @@
9382C97116E3F36B00191302 /* BVMEmptyDetailViewController.xib */,
935FEEC016829B4700BE3892 /* BVMIPListViewController.h */,
935FEEC116829B4700BE3892 /* BVMIPListViewController.m */,
+ 93E5D84216EE727900C57111 /* BVMBrowserSelectorViewController.h */,
+ 93E5D84316EE727A00C57111 /* BVMBrowserSelectorViewController.m */,
935FEED21682C06900BE3892 /* BVMServersListViewController.h */,
935FEED31682C06900BE3892 /* BVMServersListViewController.m */,
932709F1168244E20013DEB5 /* BVMServerViewController.h */,
@@ -457,6 +467,8 @@
93ADFEDD168EB82C00F8256C /* KCMutableDictionary.m in Sources */,
93ADFEE1168F487F00F8256C /* BVMAddEditServerViewController.m in Sources */,
9382C97216E3F36B00191302 /* BVMEmptyDetailViewController.m in Sources */,
+ 93E5D84416EE727B00C57111 /* BVMBrowserSelectorViewController.m in Sources */,
+ 93E5D84716EE73D400C57111 /* BVMLinkOpenManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1,9 +1,12 @@
#import "BVMAboutSettingsViewController.h"
+#import "BVMLinkOpenManager.h"
+#import "BVMBrowserSelectorViewController.h"
#import "UIColor+BVMColors.h"
typedef NS_ENUM(NSUInteger, BVMAboutSettingsTableViewSections) {
BVMAboutSettingsTableViewSectionContactSupport = 0,
BVMAboutSettingsTableViewSectionUsefulLinks,
+ BVMAboutSettingsTableViewSectionSettings,
BVMAboutSettingsTableViewNumSections
};
@@ -18,14 +21,25 @@ typedef NS_ENUM(NSUInteger, BVMAboutSettingsTableUsefulLinksRows) {
BVMAboutSettingsTableUsefulLinksNumRows
};
+typedef NS_ENUM(NSUInteger, BVMAboutSettingsTableSettingsRows) {
+ BVMAboutSettingsTableSettingsRowBrowser = 0,
+ BVMAboutSettingsTableSettingsNumRows
+};
+
static NSString * BVMAboutSettingsTableRowTitles[BVMAboutSettingsTableViewNumSections][2];
+static NSUInteger BVMAboutSettingsTableRowsInSection[BVMAboutSettingsTableViewNumSections];
-__attribute__((constructor)) static void __NLReportSalesAnalyticsFilterViewControllerTableConstantsInit(void)
+__attribute__((constructor)) static void __BVMAboutSettingsViewControllerTableConstantsInit(void)
{
@autoreleasepool {
BVMAboutSettingsTableRowTitles[BVMAboutSettingsTableViewSectionContactSupport][BVMAboutSettingsTableContactSupportRowEmailAppAuthor] = NSLocalizedString(@"Email App Author", nil);
BVMAboutSettingsTableRowTitles[BVMAboutSettingsTableViewSectionUsefulLinks][BVMAboutSettingsTableUsefulLinksRowStallion] = NSLocalizedString(@"BuyVM Manager (Stallion)", nil);
BVMAboutSettingsTableRowTitles[BVMAboutSettingsTableViewSectionUsefulLinks][BVMAboutSettingsTableUsefulLinksRowClientArea] = NSLocalizedString(@"BuyVM Billing/Support", nil);
+ BVMAboutSettingsTableRowTitles[BVMAboutSettingsTableViewSectionSettings][BVMAboutSettingsTableSettingsRowBrowser] = NSLocalizedString(@"Select Browser", nil);
+
+ BVMAboutSettingsTableRowsInSection[BVMAboutSettingsTableViewSectionContactSupport] = BVMAboutSettingsTableContactSupportNumRows;
+ BVMAboutSettingsTableRowsInSection[BVMAboutSettingsTableViewSectionUsefulLinks] = BVMAboutSettingsTableUsefulLinksNumRows;
+ BVMAboutSettingsTableRowsInSection[BVMAboutSettingsTableViewSectionSettings] = BVMAboutSettingsTableSettingsNumRows;
}
}
@@ -74,19 +88,19 @@ - (void)sendSupportEmail
{
NSString *url = [NSString stringWithFormat:@"mailto:chris+bvmsupport@chrisdzombak.net?subject=BuyVM%%20Manager%%20Support%%20-%%20%@",
[BVMAboutSettingsViewController appVersion]];
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
+ [BVMLinkOpenManager openURLString:url];
}
- (void)openStallion
{
NSString *url = @"https://manage.buyvm.net";
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
+ [BVMLinkOpenManager openURLString:url];
}
- (void)openClientArea
{
NSString *url = @"https://my.frantech.ca";
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
+ [BVMLinkOpenManager openURLString:url];
}
- (void)doneButtonTapped
@@ -95,6 +109,12 @@ - (void)doneButtonTapped
else NSLog(@"%@ cannot dismiss without a dismissBlock", NSStringFromClass([self class]));
}
+- (void)pushToBrowserSelector
+{
+ UIViewController *browserSelector = [[BVMBrowserSelectorViewController alloc] init];
+ [self.navigationController pushViewController:browserSelector animated:YES];
+}
+
#pragma mark Helpers
+ (NSString *)appVersion
@@ -111,15 +131,7 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
- switch(section) {
- case BVMAboutSettingsTableViewSectionContactSupport:
- return BVMAboutSettingsTableContactSupportNumRows;
- case BVMAboutSettingsTableViewSectionUsefulLinks:
- return BVMAboutSettingsTableUsefulLinksNumRows;
- default:
- NSLog(@"unknown section %d in %s", section, __PRETTY_FUNCTION__);
- return 0;
- }
+ return BVMAboutSettingsTableRowsInSection[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@@ -158,6 +170,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}
break;
+ case BVMAboutSettingsTableViewSectionSettings:
+ NSParameterAssert(indexPath.row == 0);
+ [self pushToBrowserSelector];
+ break;
+
default:
NSLog(@"Unrecognized section %d in %s", indexPath.section, __PRETTY_FUNCTION__);
break;
@@ -0,0 +1,8 @@
+#import <UIKit/UIKit.h>
+
+@interface BVMBrowserSelectorViewController : UITableViewController
+
+// Designated initializer
+- (id)init;
+
+@end
@@ -0,0 +1,100 @@
+#import "BVMBrowserSelectorViewController.h"
+#import "BVMLinkOpenManager.h"
+#import "UIColor+BVMColors.h"
+
+@interface BVMBrowserSelectorViewController ()
+
+@property (nonatomic, strong) NSIndexPath *previouslySelectedIndexPath;
+
+@end
+
+@implementation BVMBrowserSelectorViewController
+
+- (id)init
+{
+ self = [super initWithStyle:UITableViewStyleGrouped];
+ if (self) { }
+ return self;
+}
+
+- (void)viewDidLoad
+{
+ [super viewDidLoad];
+
+ self.title = NSLocalizedString(@"Select Browser", nil);
+
+ self.tableView.backgroundColor = [UIColor bvm_tableViewBackgroundColor];
+ self.tableView.backgroundView = nil;
+
+ self.contentSizeForViewInPopover = CGSizeMake(320, 44*BVMNumBrowsers + 20);
+}
+
+#pragma mark UI Help
+
+- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
+{
+ cell.textLabel.text = [BVMLinkOpenManager nameForBrowser:indexPath.row];
+
+ if ([BVMLinkOpenManager browserAvailable:indexPath.row]) {
+ cell.textLabel.textColor = [UIColor darkTextColor];
+ cell.selectionStyle = UITableViewCellSelectionStyleGray;
+ } else {
+ cell.textLabel.textColor = [UIColor lightGrayColor];
+ cell.selectionStyle = UITableViewCellSelectionStyleNone;
+ }
+
+ if ([BVMLinkOpenManager selectedBrowser] == indexPath.row) {
+ cell.accessoryType = UITableViewCellAccessoryCheckmark;
+ } else {
+ cell.accessoryType = UITableViewCellAccessoryNone;
+ }
+}
+
+#pragma mark UITableViewDataSource
+
+- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
+{
+ return 1;
+}
+
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
+{
+ return BVMNumBrowsers;
+}
+
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ static NSString *CellIdentifier = @"Cell";
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+ if (!cell) {
+ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
+ }
+
+ [self configureCell:cell atIndexPath:indexPath];
+ return cell;
+}
+
+#pragma mark UITableViewDelegate
+
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ [BVMLinkOpenManager setSelectedBrowser:indexPath.row];
+ [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
+
+ [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
+
+ if (self.previouslySelectedIndexPath && [indexPath compare:self.previouslySelectedIndexPath] != NSOrderedSame) {
+ [self.tableView cellForRowAtIndexPath:self.previouslySelectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
+ }
+}
+
+- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ // hack:
+ self.previouslySelectedIndexPath = [NSIndexPath indexPathForRow:[BVMLinkOpenManager selectedBrowser] inSection:0];
+
+ if ([BVMLinkOpenManager browserAvailable:indexPath.row]) return indexPath;
+ else return nil;
+}
+
+@end
@@ -0,0 +1,27 @@
+#import <Foundation/Foundation.h>
+
+typedef NS_ENUM(NSInteger, BVMBrowser) {
+ BVMBrowserSafari = 0,
+ BVMBrowserChrome,
+ BVMBrowserOnePassword,
+ BVMNumBrowsers
+};
+
+@interface BVMLinkOpenManager : NSObject
+
++ (BOOL)browserAvailable:(BVMBrowser)browser;
+
++ (BVMBrowser)selectedBrowser;
++ (void)setSelectedBrowser:(BVMBrowser)browser;
+
+/**
+ * Open the given URL.
+ *
+ * If HTTP or HTTPS, uses the defaut browser.
+ */
++ (void)openURL:(NSURL *)url;
++ (void)openURLString:(NSString *)urlString;
+
++ (NSString *)nameForBrowser:(BVMBrowser)browser;
+
+@end
@@ -0,0 +1,101 @@
+#import "BVMLinkOpenManager.h"
+
+static NSString *kBVMBrowserPrefsKey = @"BVMBrowserPrefsKey";
+
+static NSString * BVMBrowserNames[BVMNumBrowsers];
+
+__attribute__((constructor)) static void __BVMBrowserConstantsInit(void)
+{
+ @autoreleasepool {
+ BVMBrowserNames[BVMBrowserSafari] = NSLocalizedString(@"Safari", nil);
+ BVMBrowserNames[BVMBrowserOnePassword] = NSLocalizedString(@"1Password", nil);
+ BVMBrowserNames[BVMBrowserChrome] = NSLocalizedString(@"Google Chrome", nil);
+ }
+}
+
+@implementation BVMLinkOpenManager
+
+#pragma mark Browser availability
+
++ (BOOL)browserAvailable:(BVMBrowser)browser
+{
+ switch(browser) {
+ case BVMBrowserSafari:
+ return YES;
+ case BVMBrowserChrome:
+ return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome://google.com"]];
+ case BVMBrowserOnePassword:
+ return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"onepassword://search"]];
+ default:
+ NSLog(@"Unknown browser %d in %s", browser, __PRETTY_FUNCTION__);
+ return NO;
+ }
+}
+
+#pragma mark Default browser management
+
++ (BVMBrowser)selectedBrowser {
+ return [[NSUserDefaults standardUserDefaults] integerForKey:kBVMBrowserPrefsKey];
+}
+
++ (void)setSelectedBrowser:(BVMBrowser)browser
+{
+ [[NSUserDefaults standardUserDefaults] setInteger:browser forKey:kBVMBrowserPrefsKey];
+}
+
+#pragma mark URL Actions
+
++ (void)openURL:(NSURL *)url
+{
+ NSString *replacementScheme = [BVMLinkOpenManager replacementSchemeForScheme:url.scheme];
+
+ if (replacementScheme) {
+ // https://developers.google.com/chrome/mobile/docs/ios-links
+ NSString *absoluteString = [url absoluteString];
+ NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
+ NSString *urlNoScheme = [absoluteString substringFromIndex:rangeForScheme.location];
+ NSString *replacedURLString = [replacementScheme stringByAppendingString:urlNoScheme];
+ url = [NSURL URLWithString:replacedURLString];
+ }
+
+ [[UIApplication sharedApplication] openURL:url];
+}
+
++ (void)openURLString:(NSString *)urlString
+{
+ [BVMLinkOpenManager openURL:[NSURL URLWithString:urlString]];
+}
+
++ (NSString *)replacementSchemeForScheme:(NSString *)scheme
+{
+ NSString *urlScheme = [scheme lowercaseString];
+ NSString *replacementScheme = nil;
+
+ BVMBrowser selectedBrowser = [BVMLinkOpenManager selectedBrowser];
+ if (selectedBrowser == BVMBrowserSafari) return nil;
+
+ if (selectedBrowser == BVMBrowserChrome && ![BVMLinkOpenManager browserAvailable:BVMBrowserChrome]) return nil;
+ if (selectedBrowser == BVMBrowserOnePassword && ![BVMLinkOpenManager browserAvailable:BVMBrowserOnePassword]) return nil;
+
+ if ([urlScheme isEqualToString:@"http"]) {
+ if (selectedBrowser == BVMBrowserOnePassword) replacementScheme = @"ophttp";
+ else if (selectedBrowser == BVMBrowserChrome) replacementScheme = @"googlechrome";
+ } else if ([urlScheme isEqualToString:@"https"]) {
+ if (selectedBrowser == BVMBrowserOnePassword) replacementScheme = @"ophttps";
+ else if (selectedBrowser == BVMBrowserChrome) replacementScheme = @"googlechromes";
+ }
+
+ return replacementScheme;
+}
+
+#pragma mark UI/Naming
+
++ (NSString *)nameForBrowser:(BVMBrowser)browser
+{
+ NSParameterAssert(browser < BVMNumBrowsers);
+ NSParameterAssert(browser >= 0);
+
+ return BVMBrowserNames[browser];
+}
+
+@end
Oops, something went wrong.

0 comments on commit 3f83927

Please sign in to comment.