diff --git a/iphone/Barcodes/Classes/BarcodesAppDelegate.h b/iphone/Barcodes/Classes/BarcodesAppDelegate.h index 098aefd96..bb6cddaa8 100644 --- a/iphone/Barcodes/Classes/BarcodesAppDelegate.h +++ b/iphone/Barcodes/Classes/BarcodesAppDelegate.h @@ -7,14 +7,20 @@ // #import +#import "ZXMainViewController.h" @interface BarcodesAppDelegate : NSObject { UIWindow *window; UITabBarController *tabBarController; + ZXMainViewController *viewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; +@property (nonatomic, retain) IBOutlet ZXMainViewController *viewController; + +- (void)registerView:(ZXMainViewController*)controller; + @end diff --git a/iphone/Barcodes/Classes/BarcodesAppDelegate.m b/iphone/Barcodes/Classes/BarcodesAppDelegate.m index 43afb7f2f..fc7a7f9b1 100644 --- a/iphone/Barcodes/Classes/BarcodesAppDelegate.m +++ b/iphone/Barcodes/Classes/BarcodesAppDelegate.m @@ -12,12 +12,58 @@ @synthesize window; @synthesize tabBarController; +@synthesize viewController; #pragma mark - #pragma mark Application lifecycle +- (BOOL)myOpenURL:(NSURL*)url { + if (!url) return NO; + if ([[url scheme] isEqualToString:@"zxing"]) { + if ([[url host] isEqualToString:@"scan"]) { + NSArray *pairs = [[url query] componentsSeparatedByString:@"&"]; + + for (NSString *pair in pairs) { + NSArray *elements = [pair componentsSeparatedByString:@"="]; + NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + + if ([key isEqualToString:@"ret"]) { + [[NSUserDefaults standardUserDefaults] setObject:val forKey:@"returnURL"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [[self viewController] scan:nil]; + } + if ([key isEqualToString:@"SCAN_FORMATS"]) { + // Storing these, but they effect nothing yet. + NSArray *formats = [val componentsSeparatedByString:@","]; + [[NSUserDefaults standardUserDefaults] setObject:formats forKey:@"scanFormats"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + } + } + } + return YES; + } else { + return NO; + } +} + +- (void)registerView:(ZXMainViewController*)controller { + [self setViewController:controller]; +} + - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Check if launching from URL + NSURL *url = [launchOptions objectForKey: UIApplicationLaunchOptionsURLKey]; + if ([url isMemberOfClass: [NSURL class]]) { + return [self myOpenURL: url]; + } else { + // Clear the return URL so the application goes back to working as normal... + [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"returnURL"]; + [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"scanFormats"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + } + // Override point for customization after application launch. [self.window addSubview:tabBarController.view]; [self.window makeKeyAndVisible]; @@ -25,6 +71,10 @@ return YES; } +- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url +{ + return [self myOpenURL: url]; +} - (void)applicationWillResignActive:(UIApplication *)application { /* @@ -39,6 +89,11 @@ Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. If your application supports background execution, called instead of applicationWillTerminate: when the user quits. */ + + // Clear the return URL so the application goes back to working as normal... + [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"returnURL"]; + [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"scanFormats"]; + [[NSUserDefaults standardUserDefaults] synchronize]; } diff --git a/iphone/Barcodes/Classes/ZXMainViewController.mm b/iphone/Barcodes/Classes/ZXMainViewController.mm index 943a39666..f7f8c1c0e 100644 --- a/iphone/Barcodes/Classes/ZXMainViewController.mm +++ b/iphone/Barcodes/Classes/ZXMainViewController.mm @@ -15,6 +15,7 @@ #import "ArchiveController.h" #import "Database.h" #import "ScanViewController.h" +#import "BarcodesAppDelegate.h" @implementation ZXMainViewController @synthesize actions; @@ -36,6 +37,8 @@ - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"Barcodes"; + + [((BarcodesAppDelegate*)[[UIApplication sharedApplication] delegate]) registerView:self]; } @@ -111,7 +114,13 @@ #endif Scan * scan = [[Database sharedDatabase] addScanWithText:resultString]; [[NSUserDefaults standardUserDefaults] setObject:resultString forKey:@"lastScan"]; - + NSString *returnUrl = [[NSUserDefaults standardUserDefaults] stringForKey:@"returnURL"]; + if (returnUrl != nil) { + NSURL *ourURL = [NSURL URLWithString:[returnUrl stringByReplacingOccurrencesOfString:@"{CODE}" withString:resultString]]; + [[UIApplication sharedApplication] openURL:ourURL]; + return; + } + ParsedResult *parsedResult = [UniversalResultParser parsedResultForString:resultString]; self.result = [parsedResult retain]; self.actions = [self.result.actions retain];