You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用内通过openURL调起百度地图APP失败该如何解决?

问题根因及修复方案

1. 缺少URL Scheme白名单配置

iOS 9及以上系统要求,所有需要跳转的第三方APP的URL Scheme必须提前在项目Info.plist的LSApplicationQueriesSchemes数组中声明,未声明的Scheme会被系统拦截,导致调用直接返回success = 0。

修复操作:打开Info.plist,新增LSApplicationQueriesSchemes数组类型键,在数组下添加字符串类型的子项,值为baidumap。

2. URL字符串格式错误

当前使用的URL模板里origin参数的坐标和name字段之间多了多余空格:latlng:%f,%f |name,空格会导致URL解析失败。

修复操作:删除坐标值和竖线之间的空格,改为latlng:%f,%f|name。

3. 中文参数未做URL编码

URL中直接拼接未编码的中文字符会导致整个URL不合法,[NSURL URLWithString:]会直接返回nil,自然无法调起应用。

修复操作:对中文的起点、终点名称先做URL编码再拼接。


正确代码示例

// 替换为实际的起点、终点坐标
CLLocationDegrees originLat = 39.915;
CLLocationDegrees originLng = 116.404;
CLLocationDegrees destLat = 39.915;
CLLocationDegrees destLng = 116.504;

// 中文参数URL编码
NSString *originName = [@"我的位置" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *destName = [@"终点" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

// 生成合法URL
NSString *urlTemplate = @"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving";
NSString *urlStr = [NSString stringWithFormat:urlTemplate, originLat, originLng, originName, destLat, destLng, destName];
NSURL *targetUrl = [NSURL URLWithString:urlStr];

if (targetUrl) {
    [[UIApplication sharedApplication] openURL:targetUrl options:@{} completionHandler:^(BOOL success) {
        NSLog(@"Invoke success = %d", success);
    }];
} else {
    NSLog(@"URL生成非法,请检查参数");
}

额外排查项

  • 确认测试设备已安装百度地图APP,未安装时调用必然返回失败
  • 传入的经纬度需要使用百度BD09坐标系,非BD09坐标需要先做坐标系转换,否则会出现路线偏移,但不会导致调起失败

内容的提问来源于stack exchange,提问作者Stanley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 07:45:03