iOS OpenURL用法简介

摘要:
要注册自定义URL协议,首先启动的应用程序需要在iPhone上注册自定义的URL协议。这是在信息中完成的。plist文件。

参考:http://www.2cto.com/kf/201401/274753.html

     http://blog.sina.com.cn/s/blog_a170e5c80101gsdj.html

         http://blog.csdn.net/perfect_promise/article/details/7793735

         

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”)。如果有必要,你可以在这里加入多个协议。

操作截图如下:

iOS OpenURL用法简介第1张

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

访问自定义URL(在test02中)

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

- (void)viewDidLoad {

    [superviewDidLoad];

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(100, 200, 100, 50);

    btn.backgroundColor = [UIColorredColor];

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

    [self.viewaddSubview:btn];

}

 

- (void)onBtnClick{

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

    if ([[UIApplicationsharedApplication] canOpenURL:urlStr]) {

        NSLog(@"can go to test");

        [[UIApplicationsharedApplication] openURL: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) {

        returnNO;

    }

    NSString *URLString = [url absoluteString];

    NSLog(@"%@",URLString);

    returnYES;

}

 

 

调用地图电话等方法

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];
执行完以后,会自动调用代理方法:

 

免责声明:文章转载自《iOS OpenURL用法简介》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇出现这个错误说明是程序在调用'ZipArchive' 这个类的时候没有成功ElementUI中的el-table怎样实现每一列显示的是控件并能动态实现双向数据绑定下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MySQL入门笔记(二)

MySQL的数据类型、数据库操作、针对单表的操作以及简单的记录操作可参考:MySQL入门笔记(一) 五、子查询   子查询可简单地理解为查询中的查询,即子查询外部必然还有一层查询,并且这里的查询并非仅仅指SELECT的查询操作,而是包括INSERT、DELETE、SET等操作在内的所有操作。 1. 使用比较运算符的子查询 operand comparis...

R语言做正态分布检验

摘自:吴喜之:《非参数统计》(第二版),中国统计出版社,2006年10月:P164-165 1、ks.test() 例如零假设为N(15,0.2),则ks.test(x,"pnorm",15,0.2)。如果不是正态分布,还可以选"pexp", "pgamma"等。 2、shapiro.test() 可以进行关于正态分布的Shapiro-Wilk检验。...

python爬虫数据解析之xpath

xpath是一门在xml文档中查找信息的语言。xpath可以用来在xml文档中对元素和属性进行遍历。 在xpath中,有7中类型的节点,元素,属性,文本,命名空间,处理指令,注释及根节点。 节点 首先看下面例子: <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> &l...

AS400一些有用的命令

1. '1' -> *ON      '0' -> *OFF 2. Indicator position    CHAIN : HI    LOOKUP : EQ    READ : EQ 3.DSPFD显示PF的member个数,Format Identifier,trigger等等,DSPFFD显示PF的列名之类 DSPFD FILE(T...

CSS3 基础(1)——选择器详解

 CSS3选择器详解 一、 属性选择器   在CSS3中,追加了三个属性选择器分别为:[att*=val]、[att^=val]和[att$=val],使得属性选择器有了通配符的概念。 选择器 示例 描述 [attribute^=value] [src^="https"] 选择每一个src属性的值以"https"开头的元素 [attribute...

设置User Agent

  公司的前端要给项目的webview加一个区分,用来区别是iOS端访问、android访问还是在浏览器访问的,说是要加一个User Agent ,前端根据不同信息做适配,和我说来一头雾水,后来经过开发同事的指导和在网上查阅资料,才有了点头绪,在这里和大家分享一下。 一、获取UserAgent UIWebView方式: UIWebView* tempWeb...