ios - 无法从我的 native iOS 应用程序中打开 google map ,网址为 "comgooglemaps-x-callback://"

标签 ios objective-c

在我的应用程序中,我想使用谷歌地图进行导航。所以我使用谷歌URL方案来实现同样的目的。导航完成后,我需要恢复我的应用程序,因此我使用了“comgooglemaps-x-callback://”选项。 我正在使用以下代码

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps-x-callback://"]])
    {
        NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
        NSString *string = [NSString stringWithFormat:@"comgooglemaps-x-callback://?saddr=%f,%f&daddr=%f,%f&directionsmode=driving&x-success=myapp://?resume=true&x-source=%@",sourceCoords.latitude,sourceCoords.longitude, destCoords.latitude, destCoords.longitude,appName];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string] options:@{} completionHandler:^(BOOL success)
         {
             if (success)
             {
                 NSLog(@"Opened");
                 ApplicationDelegate.isDirectedToGoogleMaps = YES;
             }
         }];
    }

这里“myapp”是我的应用程序的 URL 方案。 请检查我输入的 Google URL 方案是否正确。当我检查谷歌文档时,他们说“comgooglemaps-x-callback://”的参数与“comgooglemaps”的参数相同,需要两个额外的参数

[NSURL URLWithString:string]

上面的代码始终给出空值,并且不会定向到谷歌地图。

仅当我使用(“comgooglemaps-x-callback://”)回调网址时才会出现此问题。 当我使用没有回调选项的 Google URL 时,我可以启动 google map 。但就我而言,我想使用回调选项,以便我可以将用户引导回源应用程序。

最佳答案

您只能使用 comgooglemaps:// 打开 Google map 。

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {

        NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving", encodedString];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url] options:@{} completionHandler:^(BOOL success) {
            if (success) {
                //do something on success
            }
        }];

    } else {
     //if google map is not installed in device then we can open goole map in web
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.google.com/maps?daddr=%@", encodedString]] options:@{} completionHandler:^(BOOL success) {
            if (success) {
               //do something on success
            }
        }];
    }

不要忘记包含在 Info.plist 中,如下所示。

<key>LSApplicationQueriesSchemes</key>
<array>

    <string>comgooglemaps</string>

</array>

enter image description here

关于ios - 无法从我的 native iOS 应用程序中打开 google map ,网址为 "comgooglemaps-x-callback://",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54588331/

相关文章:

ios - 检测从推送通知打开的应用程序(后台获取)

iphone - 为什么使用 __weak 不会导致局部变量立即为零?

ios - 除了默认、黑色或白色之外,如何更改 UICollectionView 中滚动条指示器的颜色?

objective-c - 如何在 AppleScript 中使用变量?

iphone - UISegmentedControl 放在 tableView 标题中时行为异常

ios - swift : Pass UITableViewCell label to new ViewController ** CRASHING **

objective-c - 如何在 iphone 的 uilabel 上显示倒计时?

iphone - 在 UITableView 的顶部和底部添加按钮

iphone - Twitter API(从 ID 到屏幕名称)

ios - 使用 Swift 在 iOS 中远程控制事件