检查iOS 6中是否安装了Google Maps应用程序

10

我正在尝试弄清楚如何处理此代码的结果,以查看应用程序中是否安装了Google Maps。

[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]];

我正在创建一个带有选项的UIAlertView,如果选中或未选中,我希望为用户提供不同的选项。

如何将上述代码的结果转换为布尔值?

先行感谢。

2个回答

23

结果已经是canOpenURL:的布尔值:

BOOL canHandle = [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]];

if (canHandle) {
   // Google maps installed
} else {
   // Use Apple maps?
}

它返回false并且谷歌地图已经在iPhone上了,是否有其他iOS9的解决方案? - Patel Jigar
1
您需要将 comgooglemaps:// 添加到您在 iOS 9 中将调用的应用程序 URL 方案列表中。只需将要调用的URL添加到应用程序的 info.plist 中的 LSApplicationQueriesSchemes 中即可。 - rckoenes
1
在iOS9.0及以上版本中,我们需要从“comgooglemaps://”中删除“//”。 - kb920
如果未安装谷歌地图并且甚至删除了苹果地图(适用于iOS 10+),会发生什么? - Moin Shirazi
什么都不会发生。但你可能想添加一个检查。 - rckoenes
显示剩余6条评论

5

iOS 9.0及以上版本

步骤1:在应用程序的info.plist中将comgooglemaps添加到LSApplicationQueriesSchemes中

步骤2:

BOOL isGoogleMap = [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];
UIAlertView *alert;

if(isGoogleMap)
{
    alert = [[UIAlertView alloc]
             initWithTitle:@"Get Directions"
             message:@"Show Map"
             delegate:self
             cancelButtonTitle:@"Cancel"
             otherButtonTitles:@"View in Apple Maps", @"View in Google Maps", nil];
}
else
{
    alert = [[UIAlertView alloc]
             initWithTitle:@"Get Directions"
             message:@"Show Map"
             delegate:self
             cancelButtonTitle:@"Cancel"
             otherButtonTitles:@"View in Apple Maps", nil];
}
alert.tag = 1010;
[alert show];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接