在IOS的应用中经常会看到一些应用程序通过某个点击动作而直接跳到App Store页面,开始很奇怪这种第三方应用于IOS的系统应用交互时怎么实现的。后来发现苹果的开发者文档中关于这种通信的实现是默认支持的,只要第三方的应用符合规范即可,这里的规范就是指接下来我们要简单介绍的东西——URL Schemes



       根据规定,IOS中支持几种特殊的URL schemes,第三方的本地应用程序和运行在Safari的Web应用程序,通过这些URL schemes可以与IOS的系统应用进行整合,为的是为用户提供一种无缝的用户体验过程。某些手机IOS应用中都会展示一些电话号码信息,在用户点击这些号码时,通过使用规定内合适的URL,应用程序可以打开手机的Phone Application进行拨号呼叫操作。这是一种很快捷的方式,省去了用户要记忆号码,然后再逐次拨号的流程。



URL scheme打开方式

       根据第三方应用程序的类型,打开IOS系统应用的方式划分为两种



             1.

Native App或我们常称呼的本地应用,可以通过UIApplication的openURL:方法来系统应用

             2.Web App或我们常称呼的Web应用,指的是那些运行在Safari浏览器中或通过UIWebView

内嵌入本地应用的的Web应用程序,可以通过点击或长按操作打开系统应用。


       当然,顺利打开系统应用的前提是二者都要保证提供指定的附和规范的URL scheme。



URL Scheme分类

       IOS支持的URL Schemes分为以下几类



        

Mail

Links(邮件链接)



          通过mailto协议来加载Mail应用程序,只要提供目标邮箱地址即可,代码如


                HTML页面中        


<a href="mailto:frank@wwdcdemo.example.com">John Frank</a>
<a href="mailto:frank@wwdcdemo.example.com">John Frank</a>

               本地应用中                


if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto:frank@wwdcdemo.example.com"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}
if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto:frank@wwdcdemo.example.com"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}

       另外也可以通过to,cc,bcc,subject,body字段来指定邮件的抄送,密送,主题,消息内容。参数值都要经过URL编码处理。


mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!
mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!



        

Phone links(电话链接)



         tel协议用来启动Phone 应用,以及呼叫指定的号码。在网页上点击一个号码链接会弹出一个对话框来提示用户是否需要拨号,并在用户允许后开始拨号。在本地的应用中,打开 一个tel协议地址会直接拨号而对用户提示。如


         HTMl页面中


<a href="tel:1-408-555-5555">1-408-555-5555</a>
<a href="tel:1-408-555-5555">1-408-555-5555</a>

         本地应用中


if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel:1-408-555-5555"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;        
}
if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel:1-408-555-5555"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;        
}



       为了防止恶意的号码重定向或改变通话行为以及帐号,Phone应用支持tel协议的大部分的特殊字符。当URL包含*或#时,Phone应用是不会进行拨号的。如果你的应用程序是tonguo用户输入或其他未知源获取的,那么URL中的任何特殊字符都需要经过编码。对于本地应用,可以使用NSString的stringByAddingPercentEscapesUsingEncoding:方法来编码。


       此外,IOS中的safari默认是执行号码检测的。然而,如果你得页面包含了那些可以被检测为号码,但实际意义却不是号码的数字时,你可以选择关闭当前页面的号码检测行为。只要在页面代码加入以下元数据即可


<meta name = "format-detection" content = "telephone=no">
<meta name = "format-detection" content = "telephone=no">

      

Text links(文本链接)


       使用sms协议来加载Messages应用。该URL的正式格式为"sms:<phone>",其中<phone>是可选的,用来指定sms消息接收者的号码。参数值包含了数字,"+" , "-" , "." 。


       HTML页面中


<a href="sms:">Launch Messages App</a>
<a href="sms:1-408-555-1212">New SMS Message</a>
<a href="sms:">Launch Messages App</a>
<a href="sms:1-408-555-1212">New SMS Message</a>

      本地应用中


if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:1-408-555-1212"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}
if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:1-408-555-1212"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}

     

 

iTunes links(iTunes链接)



       iTunes链接用来链接到iTunes Store中的内容。通过Apple的 iTunes Link Maker我们可以方便的查询并获取应用程序的链接地址。


       HTML页面中


<a href="https://itunes.apple.com/cn/app/numbers/id361304891?mt=8">Numbers</a>
<a href="https://itunes.apple.com/cn/app/numbers/id361304891?mt=8">Numbers</a>

       本地应用中


if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/numbers/id361304891?mt=8"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}
if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/numbers/id361304891?mt=8"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}

       

Map links(地图链接)



       地图链接用来显示地理位置或指引驾驶方向。在Web App中的链接会直接跳到地图页面,本地应用会直接打开IOS的地图应用。如


       HTML页面中


<a href="http://maps.apple.com/?q=cupertino">Cupertino</a>
<a href="http://maps.apple.com/?q=cupertino">Cupertino</a>

       本地应用


[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://maps.apple.com/?q=cupertino"]] ;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://maps.apple.com/?q=cupertino"]] ;

       正确的地图链接格式规则如下


  • 域名必须为maps.apple.com
  • 路径不能为/maps/*
  • 参数不能为q=*
  • 参数不能包含view=text或dirflag=r

       

Youtube links(Youtube链接)



       Youtube链接用来加载YouTube应用程序或者链接值YouTube的web站点来播放指定的视频。链接到Youtube的应用可以播放其视频。链接是以http为开头的,而非youtube。


       HTML页面中


<a href="http://www.youtube.com/watch?v=xNsGNlDb6xY">iPhone5</a>
<a href="http://www.youtube.com/v/xNsGNlDb6xY">iPhone5</a>
<a href="http://www.youtube.com/watch?v=xNsGNlDb6xY">iPhone5</a>
<a href="http://www.youtube.com/v/xNsGNlDb6xY">iPhone5</a>

       本地应用程序中    


//或 http://www.youtube.com/v/xNsGNlDb6xY
if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=xNsGNlDb6xY"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}
//或 http://www.youtube.com/v/xNsGNlDb6xY
if(![[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=xNsGNlDb6xY"]] ){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"无法打开程序" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] ;
    [alert show] ;
}



注:描述于IOS6.0下,关于地图协议,在IOS5.X版本使用的地图为Google Map,详情参见IOS5.X对应的开发者文档。