using google map api with X-callback url - ios

I am working on a navigation app which launches GOOGLE MAP from my native app when user click on any of the pins displayed on the apple map. The location data was pulled from GOOGLE. So I used this code for block showing a callout accessory and let user get to the GOOGLE MAP
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
NSString* addr = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
Then I found out there is a x-callback url which can put a back button in the google map and let the user go back to my native app. but anyone know how to use these lines of codes? i have registered my app in the properties list but i have no idea what to do the next. the code from google is like this:
comgooglemaps-x-callback://?center=40.765819,-73.975866&zoom=14
&x-success=sourceapp://?resume=true
&x-source=SourceApp
Can anyone please tell me where shall i put these codes? and How to use that in my app?

If you scroll down in the Google documentation they give example code on how to use this in iOS.
Code provided:
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL])
{
NSString *directionsRequest = #"comgooglemaps-x-callback://" +
#"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
#"&x-success=sourceapp://?resume=true&x-source=AirApp";
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else
{
NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}
You would change everthing after daddr= with your address. Also change AirApp to the name of your application.

you should use below code :
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL])
{
NSMutableString *directionsRequest = [NSMutableString stringWithString:#"comgooglemaps-x-callback://"];
[directionsRequest appendFormat:#"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York"];
[directionsRequest appendFormat:#"&x-success=CouriorMap://?resume=true&x-source=AirApp"];
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else
{
NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}

To use the google map call back function:
Add the following content to info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.GoogleMapTest</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
</dict>
</plist>
com.example.GoogleMapTest is you app bundle id.
myapp is the url scheme just like google map's comgooglemaps://.
Call the function anywhere you wish:
- (void)useGoogleMapApp {
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"comgooglemaps://"]]) {
NSString *url = #"comgooglemaps-x-callback://?saddr=1.294094,103.853722&daddr=1.244310,103.833386&directionsmode=transit&x-success=myapp://?resume=true&x-source=MyAppName";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} else {
NSLog(#"Can't use comgooglemaps://");
}
}

Related

Open the instagram app via my app

I am trying to open the instagram app from my application and implemented the following code, but it does not do anything. I have instagram app installed on my iphone.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
Update info.plist for Key LSApplicationQueriesSchemes
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
Code:
NSURL *instagramURL = [NSURL URLWithString:#"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
Refer instagram docs

canOpenURL failing for system-wide URL schemes

I'm running iOS 9b5.
In my app, if a device can make a phone call, I want to color the text blue so it looks tappable. If not, I leave it black.
In order to determine the device capabilities, I use:
[[UIApplcation sharedApplication] canOpenURL:#"telprompt://5555555555"]
As we all know, iOS 9 requires we whitelist any URL schemes we'll be using in our app as a privacy measure.
I have this in my Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
</array>
No matter what I do, I still get canOpenURL: failed for URL: "telprompt://" - error: "(null)". I've tried tel:// and sms:// and I can't seem to avoid that syslog warning.
Does anybody know of a way to detect whether or not a device can make a phone call wtihout triggering these warnings?
What I discovered so far is, that if the console logs -canOpenURL: failed for URL: "xxx://" - error: "(null)", it actually works. As soon as there is any other error than null, it may not work. If the error is "This app is not allowed to query for scheme xxx", then you have to add this scheme to your app's .plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>xxx</string>
</array>
Strange behavior that the console output looks like an error although there is none, indeed.
I think you might need to try this on an actual device, or just try it again. I just got this working on my iPhone 5, it looks like you don't even need to add it to the LSApplicationQueriesSchemes. If the app is built with Xcode 7 Beta 6 and you use canOpenURL or openURL like below it seems to work just fine on device.
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel:555-555-5555"]]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:555-555-5555"]]
On the iOS sim I still get the error:
LaunchServices: ERROR: There is no registered handler for URL scheme tel
-canOpenURL: failed for URL: "tel:555-555-5555" - error: "This app is not allowed to query for scheme tel"
I got the same error in IOS9 devices. So I have used below code snippet to avoid this error.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
As iOS9 deprecates stringByAddingPercentEscapesUsingEncoding, the following can be used to clean the telprompt: URL.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
//NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
In iOS9 I'm using this code and it works:
NSString *assistanceNumber = [[NSUserDefaults standardUserDefaults] objectForKey:#"AssistanceCallMISDN"];
assistanceNumber= [[assistanceNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
assistanceNumber = [assistanceNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneUrl = [NSURL URLWithString:[#"telprompt://" stringByAppendingString:assistanceNumber]];
NSURL *phoneFallbackUrl = [NSURL URLWithString:[#"tel://" stringByAppendingString:assistanceNumber]];
if ([UIApplication.sharedApplication canOpenURL:phoneUrl]) {
[UIApplication.sharedApplication openURL:phoneUrl];
} else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]) {
[UIApplication.sharedApplication openURL:phoneFallbackUrl];
} else
{
[[[UIAlertView alloc] initWithTitle:#"" message:[NSString stringWithFormat:#"No se ha podido realizar la llamada a través de la aplicación. Puede llamar usted al %#", assistanceNumber] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
[_viewEmergency setHidden:YES];
}
My Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
<string>tel</string>
</array>
Try running this on a real device instead of simulator. No need to add LSApplicationQueriesSchemes for the tel scheme.
try this one:
NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", phone_number]]];

Open maps with specific address iOS 7

I am trying to make my application open the apple maps application and have the address be pulled up. I tried this :
- (IBAction)openInMaps:(id)sender {
NSString *addressString = #"http://maps.apple.com/?q=1 Infinite Loop, Cupertino, CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
and this :
- (IBAction)openInMaps:(id)sender {
NSString *addressString = #"http://maps.apple.com/?q=1_Infinite_Loop,_Cupertino,_CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
But the button just acts like its hooked to nothing. But this does work :
- (IBAction)openInMaps:(id)sender {
NSString *addressString = #"http://maps.apple.com/?q=Cupertino,CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
So, whenever their is a space it doesn't work. How can I open this address?
You need to properly escape the spaces in the URL:
NSString *addressString = #"http://maps.apple.com/?q=1%20Infinite%20Loop,%20Cupertino,%20CA";
Edit - it seems using + instead of %20 for the spaces solves the problem.
So to get it to work properly you must use this :
NSString *addressString = #"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA";
Swift 2 version that is formatted nicely, safely handles optionals, and won't crash your app:
let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "address".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) ?? ""
let finalUrl = baseUrl + encodedName
if let url = NSURL(string: finalUrl) {
UIApplication.sharedApplication().openURL(url)
}
This is how I do it in Objective C...I always try the Waze Map first, because to be honest, it's awesome sauce, I added the PLIST file permissions at the end:
- (IBAction)getDirectionsAction:(id)sender {
NSURL *googleURL = [[NSURL alloc]
initWithString:[NSString stringWithFormat:#"comgooglemaps://?daddr=%#", #"44.294349,-70.326973"]];
NSURL *googleWebURL =
[[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://www.maps.google.com/maps?daddr=%#",
#"44.294349,-70.326973"]];
NSURL *appleURL = [NSURL URLWithString:#"http://maps.apple.com/?daddr=311+East+Buckfield+Road+Buckfield+Maine"];
NSURL *wazeURL = [NSURL URLWithString:#"waze://?ll=44.294349,-70.326973&navigate=yes"];
// Lets try the Waze app first, cuz we like that one the most
if ([[UIApplication sharedApplication] canOpenURL:wazeURL]) {
[[UIApplication sharedApplication] openURL:wazeURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// Lets try the Apple Maps app second (great success rate)
if ([[UIApplication sharedApplication] canOpenURL:appleURL]) {
[[UIApplication sharedApplication] openURL:appleURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// If those 2 are unsuccessful, let's try the Google Maps app
if ([[UIApplication sharedApplication] canOpenURL:googleURL]) {
[[UIApplication sharedApplication] openURL:googleURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// Uh, oh...Well, then lets launch it from the web then.
else {
[[UIApplication sharedApplication] openURL:googleWebURL
options:#{}
completionHandler:^(BOOL success){
}];
}
}
FOR PLIST FILE
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://maps.apple.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>http://maps.google.com/</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>waze</string>
<string>comgooglemaps</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>For Use for directions</string>
Swift 3 version:
let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "yourAddress".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let finalUrl = baseUrl + encodedName
if let url = URL(string: finalUrl)
{
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Swift 2
let baseUrl : String = "http://maps.google.com/?q="
let name : String = tableCellData[indexPath.row]
let encodedName = "address of yours"
name.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let finalUrl = baseUrl + encodedName!
let url = NSURL(string: finalUrl)!
UIApplication.sharedApplication().openURL(url)

launch youtube channel in youtube app

For launching video on youtube app, I am using below code.
NSURL *instagramURL = [NSURL URLWithString:#"youtube://foo"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
NSLog(#"opening youtube app...");
NSString *stringURL = #"http://www.youtube.com/watch?v=H9VdapQyWfg";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
} else {
// open in UIWebView in WebViewViewController
WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"webinterface"];
secondView.headerLabel = #"YouTube";
secondView.webPath = #"http://www.youtube.com/watch?v=H9VdapQyWfg";
[self.navigationController pushViewController:secondView animated:YES];
}
Now client changed the mind and asking to put channel in iPhone app.
For testing, I used link http://www.youtube.com/user/richarddawkinsdotnet
BUT when I use this link, instead of youtube app, it always opens in SAFARI. :(
Any idea/ suggestion on how can I open channel in YouTube app with link provided?
You're code's going wrong because, although you're checking if you can open the youTube URL more or less correctly, you're then just opening a web address, which will always open in Safari.
This is the code I've just used, which is working for me. You might want to modify the else statement if you want to fallback to using a webviewcontroller as this will open Safari if the youtube app isn't installed.
NSString *channelName = #"TheNameOfTheChannel";
NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:#"youtube://user/%#",channelName]];
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.youtube.com/user/%#",channelName]];
if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) {
// Can open the youtube app URL so launch the youTube app with this URL
[[UIApplication sharedApplication] openURL:linkToAppURL];
}
else{
// Can't open the youtube app URL so launch Safari instead
[[UIApplication sharedApplication] openURL:linkToWebURL];
}
Same answer, but shorter:
if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: #"youtube://user/%channelName%"]]) {
NSURL *webURL = [NSURL URLWithString:#"http://www.youtube.com/user/%channelName%"];
[[UIApplication sharedApplication] openURL: webURL];
}
and twitter:
if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: #"twitter://user?screen_name=%channelName%"]]) {
NSURL *webURL = [NSURL URLWithString:#"http://twitter.com/%channelName%"];
[[UIApplication sharedApplication] openURL: webURL];
}
And here is a more exhaustive list of apps:
http://wiki.akosma.com/IPhone_URL_Schemes
youtube://user/<channel-id> stopped working in iOS 9 so i research and found this working well now in iOS 9
youtube://www.youtube.com/channel/<channel-id>

Cannot open Google Maps in an iOS Application

I'm trying to launch Google Maps iOS Application in my Application by doing this :
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Google Map";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
#"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic"]];
}
However, the app does not launch Google Maps iOS at all. I do have Google Maps iOS installed.
What I am missing over here?
Can somebody help me out?
Thanks.
Try this instead
CLLocationCoordinate2D coordinate = COORDINATE;
// Open Google Maps application
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"comgooglemaps://"]]) {
NSString *daddr = [NSString stringWithFormat:#"%g,%g",coordinate.latitude, coordinate.longitude];
NSString *urlString = nil;
BOOL showQuery = YES;
if (showQuery) {
// Query
urlString = [NSString stringWithFormat:#"comgooglemaps://?q=%#",daddr];
} else {
// Directions
urlString = [NSString stringWithFormat:#"comgooglemaps://?saddr=&daddr=%#", daddr];
}
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}
You'll need to include comgooglemaps url scheme to your project Info.plist. I recommend checking out Localide. It helps you open Google Maps, Waze, Apple Maps, Transit App, Citymapper, and Navigon very easily.

Resources