diff --git a/src/interapp/src/NIInterapp.h b/src/interapp/src/NIInterapp.h index 4fec383ad..e58bfa436 100644 --- a/src/interapp/src/NIInterapp.h +++ b/src/interapp/src/NIInterapp.h @@ -26,6 +26,12 @@ */ @interface NIInterapp : NSObject +#pragma mark Chrome vs Safari + ++ (void)setPreferGoogleChrome:(BOOL)preferGoogleChromeOverSafari; ++ (BOOL)preferGoogleChrome; ++ (BOOL)openPreferredBrowserWithURL:(NSURL *)url; + #pragma mark Safari + (BOOL)safariWithURL:(NSURL *)url; @@ -38,11 +44,22 @@ #pragma mark Google Maps ++ (BOOL)googleMapsIsInstalled; ++ (BOOL)googleMaps; ++ (NSString *)googleMapsAppStoreId; + + (BOOL)googleMapAtLocation:(CLLocationCoordinate2D)location; -+ (BOOL)googleMapAtLocation: (CLLocationCoordinate2D)location - title: (NSString *)title; -+ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation - toLocation: (CLLocationCoordinate2D)toLocation; ++ (BOOL)googleMapAtLocation: (CLLocationCoordinate2D)location title: (NSString *)title; ++ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation toLocation: (CLLocationCoordinate2D)toLocation; + +// directionsMode can be nil. @"driving", @"transit", or @"walking". ++ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation toLocation: (CLLocationCoordinate2D)toLocation withMode:(NSString*)directionsMode; ++ (BOOL)googleMapDirectionsFromSourceAddress: (NSString*)srcAddr toDestAddress: (NSString*)destAddr withMode:(NSString*)directionsMode; + +// these just use the user's current location (even if your application doesn't have locations services on, the google maps site/app MIGHT ++ (BOOL)googleMapDirectionsToDestAddress: (NSString*)destAddr withMode:(NSString*)directionsMode; ++ (BOOL)googleMapDirectionsToLocation: (CLLocationCoordinate2D)toLocation withMode:(NSString*)directionsMode; + + (BOOL)googleMapWithQuery:(NSString *)query; #pragma mark Phone diff --git a/src/interapp/src/NIInterapp.m b/src/interapp/src/NIInterapp.m index 62dbacf15..8254d3091 100644 --- a/src/interapp/src/NIInterapp.m +++ b/src/interapp/src/NIInterapp.m @@ -28,6 +28,7 @@ @implementation NIInterapp + /////////////////////////////////////////////////////////////////////////////////////////////////// + (NSString *)sanitizedPhoneNumberFromString:(NSString *)string { if (nil == string) { @@ -41,6 +42,26 @@ + (NSString *)sanitizedPhoneNumberFromString:(NSString *)string { } +#pragma mark Chrome vs Safari + +BOOL sPreferGoogleChrome = NO; ++ (void)setPreferGoogleChrome:(BOOL)preferGoogleChrome +{ + sPreferGoogleChrome = preferGoogleChrome; +} ++ (BOOL)preferGoogleChrome { + return sPreferGoogleChrome; +} + ++ (BOOL)openPreferredBrowserWithURL:(NSURL *)url +{ + if (sPreferGoogleChrome && [NIInterapp googleChromeIsInstalled]) { + return [NIInterapp googleChromeWithURL:url]; + } + else { + return [NIInterapp safariWithURL:url]; + } +} /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -113,43 +134,127 @@ + (NSString *)googleChromeAppStoreId { * Source for URL information: http://mapki.com/wiki/Google_Map_Parameters */ +static NSString* const sGoogleMapsScheme = @"comgooglemaps:"; + /////////////////////////////////////////////////////////////////////////////////////////////////// -+ (BOOL)googleMapAtLocation:(CLLocationCoordinate2D)location { - NSString* urlPath = [NSString stringWithFormat: - @"http://maps.google.com/maps?q=%f,%f", - location.latitude, location.longitude]; - return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]]; ++ (BOOL)googleMapsIsInstalled { + return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:sGoogleMapsScheme]]; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)googleMaps { + BOOL didOpen = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:sGoogleMapsScheme]]; + + if (!didOpen) { + didOpen = [self appStoreWithAppId:[self googleMapsAppStoreId]]; + } + + return didOpen; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (NSString *)googleMapsAppStoreId { + return @"585027354"; } +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)openBestGoogleMapUrl:(NSString*)urlString{ + + if ([NIInterapp googleMapsIsInstalled]) { + NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"comgooglemaps://%@",urlString]]; + return [[UIApplication sharedApplication] openURL:url]; + } + else { + NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps%@",urlString]]; + return [NIInterapp openPreferredBrowserWithURL:url]; + } +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)googleMapAtLocation:(CLLocationCoordinate2D)location { + + NSString* urlPath = [NSString stringWithFormat: + @"?q=%f,%f", + location.latitude, location.longitude]; + return [NIInterapp openBestGoogleMapUrl:urlPath]; + +} + /////////////////////////////////////////////////////////////////////////////////////////////////// + (BOOL)googleMapAtLocation: (CLLocationCoordinate2D)location title: (NSString *)title { + NSString* urlPath = [NSString stringWithFormat: - @"http://maps.google.com/maps?q=%@@%f,%f", - [title stringByAddingPercentEscapesForURLParameter], location.latitude, location.longitude]; - return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]]; + @"?q=%@@%f,%f", + [title stringByAddingPercentEscapesForURLParameter], location.latitude, location.longitude]; + return [NIInterapp openBestGoogleMapUrl:urlPath]; + } /////////////////////////////////////////////////////////////////////////////////////////////////// + (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation toLocation: (CLLocationCoordinate2D)toLocation { - NSString* urlPath = [NSString stringWithFormat: - @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", - fromLocation.latitude, fromLocation.longitude, - toLocation.latitude, toLocation.longitude]; - return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]]; + return [NIInterapp googleMapDirectionsFromLocation:fromLocation toLocation:toLocation withMode:nil]; + } + + +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation + toLocation: (CLLocationCoordinate2D)toLocation + withMode:(NSString *)directionsMode +{ + NSString *saddr = [NSString stringWithFormat:@"%f,%f",fromLocation.latitude, fromLocation.longitude]; + NSString *daddr = [NSString stringWithFormat:@"%f,%f",toLocation.latitude, toLocation.longitude]; + + return [NIInterapp googleMapDirectionsFromSourceAddress:saddr toDestAddress:daddr withMode:directionsMode]; } +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)googleMapDirectionsToLocation: (CLLocationCoordinate2D)toLocation + withMode:(NSString *)directionsMode +{ + NSString *daddr = [NSString stringWithFormat:@"%f,%f",toLocation.latitude, toLocation.longitude]; + + return [NIInterapp googleMapDirectionsFromSourceAddress:nil toDestAddress:daddr withMode:directionsMode]; +} + + + +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)googleMapDirectionsToDestAddress: (NSString*)destAddr withMode:(NSString *)directionsMode { + return [NIInterapp googleMapDirectionsFromSourceAddress:nil toDestAddress:destAddr withMode:directionsMode]; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// ++ (BOOL)googleMapDirectionsFromSourceAddress: (NSString*)srcAddr toDestAddress: (NSString*)destAddr withMode:(NSString *)directionsMode { + + NSString* urlPath; + // source can be left blank == get current users location + if ([srcAddr length] > 0) { + urlPath = [NSString stringWithFormat: + @"?saddr=%@&daddr=%@", + srcAddr,destAddr]; + } + else { + urlPath = [NSString stringWithFormat: + @"?daddr=%@", + destAddr]; + } + if ([directionsMode length] > 0) { + urlPath = [NSString stringWithFormat:@"%@&directionsmode=%@",urlPath,directionsMode]; + } + return [NIInterapp openBestGoogleMapUrl:urlPath]; +} /////////////////////////////////////////////////////////////////////////////////////////////////// + (BOOL)googleMapWithQuery:(NSString *)query { NSString* urlPath = [NSString stringWithFormat: - @"http://maps.google.com/maps?q=%@", + @"?q=%@", [query stringByAddingPercentEscapesForURLParameter]]; - return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]]; + return [NIInterapp openBestGoogleMapUrl:urlPath ]; }