应用内打开其他应用

通过url scheme打开微信,支付宝以及地图

(一)添加url schemes

在info.plist中添加LSApplicationQueriesSchemes 设置为Array,并添加四个item:weixin,alipay,iosamap,baidumap对应微信,支付宝以及高德地图。

(二)头文件

#import <UIKit/UIKit.h>

@import CoreLocation;
@import MapKit;


@interface AppsViewController : UIViewController

@end

(三)打开地图

从苹果自带地图开始,如果可以打开,直接打开苹果地图,否则依次检查高德地图百度地图。

-(void) openMap
{
    NSLog(@"map");
    
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
    {
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

        MKMapItem *toLocation = [[MKMapItem alloc] init];
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        
        return;
    }
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"iosamap://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"baidumap://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
}

(四)打开微信和支付宝

-(void) openWx
{
    NSLog(@"wx");
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"weixin://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
}

-(void) openAlipay
{
    NSLog(@"ali");
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipay://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"alipay://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
}

//----------------------------------------代码---------------------------------------------//

#import "AppsViewController.h"

@interface AppsViewController ()

@end

@implementation AppsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor orangeColor];
    
    UIButton *wxBtn=[[UIButton alloc] init];
    wxBtn.frame=CGRectMake(100, 150, 100, 30);
    wxBtn.backgroundColor=[UIColor redColor];
    [wxBtn setTitle:@"wechat" forState:UIControlStateNormal];
    [wxBtn addTarget:self action:@selector(openWx) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:wxBtn];
    
    UIButton *aliBtn=[[UIButton alloc] init];
    aliBtn.frame=CGRectMake(100, 250, 100, 30);
    aliBtn.backgroundColor=[UIColor redColor];
    [aliBtn setTitle:@"alipay" forState:UIControlStateNormal];
    [aliBtn addTarget:self action:@selector(openAlipay) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:aliBtn];
    
    UIButton *mapBtn=[[UIButton alloc] init];
    mapBtn.frame=CGRectMake(100, 350, 100, 30);
    mapBtn.backgroundColor=[UIColor redColor];
    [mapBtn setTitle:@"map" forState:UIControlStateNormal];
    [mapBtn addTarget:self action:@selector(openMap) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:mapBtn];
    // Do any additional setup after loading the view.
}

-(void) openWx
{
    NSLog(@"wx");
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"weixin://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
}

-(void) openMap
{
    NSLog(@"map");
    
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
    {
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        //MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:nil addressDictionary:nil]];
        MKMapItem *toLocation = [[MKMapItem alloc] init];
        //目的地名称,可以传值进来,当然我们实际需要的并不是它,它只作为给用户的展示,我们实际需要的是self.coordinate,通过它来进行定位
        //toLocation.name = @"银泰·海威国际";
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        
        return;
    }
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"baidumap://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
}

-(void) openAlipay
{
    NSLog(@"ali");
    
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipay://"]])
    {
        NSString *urlString = [NSString stringWithFormat:@"alipay://"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
        return;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
原文地址:https://www.cnblogs.com/llstart-new0201/p/9722337.html