iOS OpenURL用法简介

IOS中,实现一个应用启动另外一个应用,使用UIApplication的openURL:方法就可实现,这里以test跳到test02为例。(需要先创建这两个工程)

注册自定义URL协议(在test中)

首先被启动的应用需要向iPhone注册一个自定义URL协议。这是在info.plist文件进行的。

1. 右键,选择“Add Row”

2. Key值选择“URL types”

3. 打开“Item 0″,然后为该key增加一个URL identifier。可以是任何值,但建议用“反域名”(例如 “com.fcplayer.test”)。

4. 在“Item 0”下再加一行。

5. 选择“URL Schemes” 作为Key。

6. 输入你的URL协议名 (例如“test://” 应写做“test”)。如果有必要,你可以在这里加入多个协议。

操作截图如下:

7. 两个项目的URL identifier 不能一样,而且在URL Schemes的Item 0不能相同,这一点需要注意。 

访问自定义URL(在test02中)

在主应用程序中通过访问自定义URL启动另外一个应用:(test已经安装,这段代码要写在另一个应用里面,比如test02)

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(10020010050);

    btn.backgroundColor = [UIColor redColor];

    [btn addTarget:self action:@selector(onBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

 

- (void)onBtnClick{

    NSURL * urlStr = [NSURL URLWithString:@"test://x=100"];//后面为参数

    if ([[UIApplication sharedApplicationcanOpenURL:urlStr]) {

        NSLog(@"can go to test");

        [[UIApplication sharedApplicationopenURL:urlStr];

    }else{

        NSLog(@"can not go to test!!!!!");

    }

}

 

 

自定义处理URL(在test中)

有些时候我们除了启动还需向另外一个应用发送参数,这是也可以通过自定义的URL来实现,如:

test://config=1&abar=2

通常,我们会从参数中解析出URL以便在视图中显示或者存储到UserPreference。下面的例子把URL存储为User Preference的url变量中或者打印出来:

这时我们在被启动应用中就必须进行自定义处理,在delegate中实现该消息(Cocos2d加在AppDelegate中),例如:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{

    if(!url) {

        return NO;

    }

    NSString *URLString = [url absoluteString];

    NSLog(@"%@",URLString);

    return YES;

}

 

 

调用地图电话等方法

openURL的使用方法:
view plaincopy toclipboardprint?
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appString]];  
其中系统的appString有:

1.Map    http://maps.google.com/maps?q=Shanghai  
2.Email  mailto://myname@google.com  
3.Tel    tel://10086  
4.Msg    sms://10086  
openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序。这是Iphone开发中我经常需要用到的一段代码,它仅仅只有一行而已。
- (IBAction)openMaps {
    //打开地图 
   NSString*addressText = @"beijing";
    //@"1Infinite Loop, Cupertino, CA 95014"; 
   addressText =[addressTextstringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
   NSString*urlText = [NSStringstringWithFormat:@"http://maps.google.com/maps?q=%@",addressText]; 
   NSLog(@"urlText=============== %@", urlText);
   [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:urlText]];
}

- (IBAction)openEmail {
     //打开mail // Fire off an email to apple support
      [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
 } 
 
- (IBAction)openPhone {
  
    //拨打电话
    // CallGoogle 411
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
 } 
 
- (IBAction)openSms {
    //打开短信
     // Text toGoogle SMS
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];
}

-(IBAction)openBrowser {
    //打开浏览器
    // Lanuch any iPhone developers fav site
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunesconnect.apple.com"]];
 }
iphone程序内调用谷歌地图

使用CLLocationManager类,MKMapView。并且实现<MKMapViewDelegate,CLLocationManagerDelegate>
//初始化CLLocationManager,CLLocationManager获得当前地理坐标
locmanager=[[CLLocationManager alloc]init];

[locmanager setDelegate:self];
 //设置精确度
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];

[locmanager startUpdatingLocation];
执行完以后,会自动调用代理方法:

原文地址:https://www.cnblogs.com/Yishu/p/4920220.html