ios 跳轉(zhuǎn)第三方App實現(xiàn)導(dǎo)航

要跳轉(zhuǎn)第三方App實現(xiàn)導(dǎo)航就首先需要了解兩個問題
1.怎么判斷手機上是否安裝了第三方導(dǎo)航App
2.怎么實現(xiàn)跳轉(zhuǎn)到第三方App

問題1
比如你如果要檢測是否有安裝百度地圖APP,那么就是:

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

常用的4個地圖的 URL Scheme:
1.蘋果自帶地圖(不需要檢測箭窜,所以不需要URL Scheme)
2.百度地圖 :baidumap://
3.高德地圖 :iosamap://
4.谷歌地圖 :comgooglemaps://

問題2
在 iOS 9 之后我們做跳轉(zhuǎn)是需要有個白名單的


image

具體實現(xiàn)代碼

#import <MapKit/MapKit.h>

@implementation ThirdAppNavigationManager

+ (void)thirdAppNavigation:(NSString *)lat lng:(NSString *)lng
{
    NSMutableArray *maps = [NSMutableArray array];
    
    //蘋果原生地圖-蘋果原生地圖方法和其他不一樣
    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
    iosMapDic[@"title"] = @"蘋果地圖";
    [maps addObject:iosMapDic];
    
    //百度地圖
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
        baiduMapDic[@"title"] = @"百度地圖";
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name=北京&mode=driving&coord_type=gcj02",lat,lng] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        baiduMapDic[@"url"] = urlString;
        [maps addObject:baiduMapDic];
    }
    
    //高德地圖
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
        gaodeMapDic[@"title"] = @"高德地圖";
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%@&lon=%@&dev=0&style=2",@"導(dǎo)航功能",@"nav123456",lat,lng] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        gaodeMapDic[@"url"] = urlString;
        [maps addObject:gaodeMapDic];
    }
    
    //谷歌地圖
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
        googleMapDic[@"title"] = @"谷歌地圖";
        NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%@,%@&directionsmode=driving",@"導(dǎo)航測試",@"nav123456",lat,lng] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        googleMapDic[@"url"] = urlString;
        [maps addObject:googleMapDic];
    }
    
    //騰訊地圖
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
        qqMapDic[@"title"] = @"騰訊地圖";
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%@,%@&to=終點&coord_type=1&policy=0",lat,lng] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        qqMapDic[@"url"] = urlString;
        [maps addObject:qqMapDic];
    }
    
    
    //選擇
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"選擇地圖" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    NSInteger index = maps.count;
    
    for (int i = 0; i < index; i++) {
        
        NSString * title = maps[i][@"title"];
        
        //蘋果原生地圖方法
        if (i == 0) {
            
            UIAlertAction * action = [UIAlertAction actionWithTitle:title style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                [self navAppleMap:lat lng:lng];
            }];
            [alert addAction:action];
            
            continue;
        }
        
        UIAlertAction * action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSString *urlString = maps[i][@"url"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
                
            }];
        }];

        [alert addAction:action];
    }
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"點擊了取消");
    }];
     [alert addAction:cancelAction];
    
    [[UIViewController getCurrentViewController] presentViewController:alert animated:YES completion:nil];
}

//蘋果地圖
+ (void)navAppleMap:(NSString *)lat lng:(NSString *)lng
{
    //終點坐標(biāo)
    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat.doubleValue, lng.doubleValue);
    
    //用戶位置
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    //終點位置
    MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ];
    
    
    NSArray *items = @[currentLoc,toLocation];
    
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)
                          };
    
    [MKMapItem openMapsWithItems:items launchOptions:dic];
}
@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末猖凛,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子媳握,更是在濱河造成了極大的恐慌碱屁,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛾找,死亡現(xiàn)場離奇詭異娩脾,居然都是意外死亡,警方通過查閱死者的電腦和手機打毛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門柿赊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人幻枉,你說我怎么就攤上這事碰声。” “怎么了熬甫?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵胰挑,是天一觀的道長。 經(jīng)常有香客問我椿肩,道長瞻颂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任郑象,我火速辦了婚禮贡这,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘厂榛。我一直安慰自己盖矫,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布击奶。 她就那樣靜靜地躺著辈双,像睡著了一般。 火紅的嫁衣襯著肌膚如雪柜砾。 梳的紋絲不亂的頭發(fā)上辐马,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音,去河邊找鬼喜爷。 笑死冗疮,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的檩帐。 我是一名探鬼主播术幔,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼湃密!你這毒婦竟也來了诅挑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤泛源,失蹤者是張志新(化名)和其女友劉穎拔妥,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體达箍,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡没龙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了缎玫。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片硬纤。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖赃磨,靈堂內(nèi)的尸體忽然破棺而出筝家,到底是詐尸還是另有隱情,我是刑警寧澤邻辉,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布溪王,位于F島的核電站,受9級特大地震影響值骇,放射性物質(zhì)發(fā)生泄漏莹菱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一雷客、第九天 我趴在偏房一處隱蔽的房頂上張望芒珠。 院中可真熱鬧桥狡,春花似錦搅裙、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至嫂易,卻和暖如春兄朋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背怜械。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工颅和, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留傅事,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓峡扩,卻偏偏與公主長得像蹭越,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子教届,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容