打开要求select应用程序打开地图的警报
我有一个集成了地图工具包的视图控制器。 我需要在打开地图之前拍摄一个警报,要求从所有类似的地图应用程序中select打开。 例如,如果谷歌地图应用程序安装在我的iPhone中,应该有一个选项,以及默认的地图包视图。 是否有可能实现这一function,扫描每个类似的应用程序从iPhone和返回结果作为选项打开地图。
您可以使用sumesh的答案创build一个检查数组来映射已安装的应用程序[1] :
var installedNavigationApps : [String] = ["Apple Maps"] // Apple Maps is always installed
并与每个导航应用程序,你可以想到的:
if (UIApplication.sharedApplication().canOpenURL(url: NSURL)) { self.installedNavigationApps.append(url) } else { // do nothing }
常用导航应用程序是:
- Google地图 – NSURL(string:“comgooglemaps://”)
- Waze – NSURL(string:“waze://”)
- Navigon – NSURL(string:“navigon://”)
- TomTom – NSURL(string:“tomtomhome://”)
更多信息可以在http://wiki.akosma.com/IPhone_URL_Schemesfind
在创build安装的导航应用程序列表之后,您可以展示一个UIAlertController:
let alert = UIAlertController(title: "Selection", message: "Select Navigation App", preferredStyle: .ActionSheet) for app in self.installNavigationApps { let button = UIAlertAction(title: app, style: .Default, handler: nil) alert.addAction(button) } self.presentViewController(alert, animated: true, completion: nil)
当然,你需要在指定的urlscheme中添加一个button点击行为。 例如,如果谷歌地图被点击使用这样的事情:
UIApplication.sharedApplication().openURL(NSURL(string: "comgooglemaps://?saddr=&daddr=\(place.latitude),\(place.longitude)&directionsmode=driving")!) // Also from sumesh's answer
只有苹果地图和谷歌地图安装这将产生这样的事情: