OpenURL 使用方法

来源:互联网 发布:mysql 触发器 编辑:程序博客网 时间:2024/05/21 06:26

OpenURL 使用方法

已测试可行,部分接口会继续更新:

  • 调用系统电话、信息、浏览器、邮件
  • 调用百度地图
  • 调用高德地图
  • 调用谷歌地图-待更新
  • 调用腾讯地图-待更新
  • 调用系统地图
  • 调用其他 App - 待更新
  • 备注

调用系统电话、信息、浏览器、邮件

- (IBAction)btnClick:(UIButton *)sender {    UIApplication *app = [UIApplication sharedApplication];    switch (sender.tag) {    case 0:            // 调用系统拨号,拨打电话        [app openURL:[NSURL URLWithString:@"tel://10010"]];        break;    case 1:            // 调用系统信息,发送信息        [app openURL:[NSURL URLWithString:@"sms://10010"]];        break;    case 2:            // 调用系统邮箱,向指定邮箱发送邮件        [app openURL:[NSURL URLWithString:@"mailto://12345@qq.com"]];        break;    case 3:            // 调用Safari,访问指定网页        [app openURL:[NSURL URLWithString:@"https://www.baidu.com"]];        break;    default:        break;    }}

调用地图

- (void)openMapWithTag:(NSInteger)tag{    UIApplication *app = [UIApplication sharedApplication];    CGFloat lat = self.locationPoint.latitude;    CGFloat lng = self.locationPoint.longitude;    switch (tag) {        case 0:            if ([app canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {                [app openURL:[self urlFromString:[NSString stringWithFormat:@"baidumap://map/marker?location=%lf,%lf&title=我的位置&content=你猜我在哪&src=com.heima|OpenURLDemo",lat,lng]]];            }            break;        case 1:            if([app canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {                [app openURL:[self urlFromString:[NSString stringWithFormat:@"iosamap://viewMap?sourceApplication=OpenURLDemo&poiname=我的位置&lat=%lf&lon=%lf&dev=1",lat,lng]]];            }            break;        case 2:            if ([app canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {                [app openURL:[NSURL URLWithString:@"comgooglemaps://"]];            }            break;        case 3:            break;        case 4:            if(SYSTEM_VERSION_LESS_THAN(@"6.0")) {                [app openURL:[self urlFromString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",lat,lng,lat+1,lng+1]]];            } else {  // 直接调用 iOS 自带的 appleMap                // 当前坐标                MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];                // 目的坐标                MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat + 1, lng + 1) addressDictionary:nil]];                // 设置目的地名称                toLocation.name = @"目的地";                // 打开程序,并设置起始点,终止点,路径方式,显示路径时间                [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];            }            break;        default:            break;    }}

各地图URI API

百度地图
高德地图
腾讯地图

地址转格式

- (NSURL *)urlFromString:(NSString *)string{    NSString *address = string;    address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    NSURL *url = [NSURL URLWithString:address];    return url;}

获取当前坐标

// 遵守 CLLocationManagerDelegate// 位置管理器@property (nonatomic, strong) CLLocationManager *locationManager;// 调用 updateLocation时执行这个代理,获取用户当前位置- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    // 获取位置对象    CLLocation *location = [locations firstObject];    // 获取经纬度    CLLocationCoordinate2D coordinate = location.coordinate;    // 传值    self.locationPoint = coordinate;}// 懒加载- (CLLocationManager *)locationManager{    if (!_locationManager) {        // 创建位置管理器        _locationManager = [CLLocationManager new];        // 设置代理        self.locationManager.delegate = self;        // 设置位置筛选器,发生一定的位置改变后调用位置更新        self.locationManager.distanceFilter = 10;        // 设置精准度        self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;    }    return _locationManager;}

请求授权、获取地址

- (void)viewDidLoad {    [super viewDidLoad];    // 请求用户授权    [self.locationManager requestAlwaysAuthorization];    // 设置代理    self.locationManager.delegate = self;    // 开始更新用户位置    [self.locationManager startUpdatingLocation];}

增加提示页面

// 遵守 UIAlertViewDelegate- (IBAction)openMaps:(UIButton *)sender{    self.tag = sender.tag;    [self actionSheet:@"地图"]; }- (void)actionSheet:(NSString *)str{    NSString *title = [NSString stringWithFormat:@"是否显示%@",str];    UIAlertView *view = [[UIAlertView alloc] initWithTitle:title message:@"跳转到地图 APP" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];    [view show];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex) {        [self openMapWithTag:self.tag];    }}

GitHub地址
GitHub-OpenURLDemo

文中部分内容转自:
UIAlertView
OpenURL

0 0
原创粉丝点击