通过自定义的URL Scheme启动你的App

来源:互联网 发布:淘宝上传宝贝描述模版 编辑:程序博客网 时间:2024/05/17 03:47

iPhone SDK可以把你的App和一个自定义的URL Scheme绑定。该URL Scheme可用来从浏览器或别的App启动你的App。

如何响应从别的App里发给你的URL Scheme申请,由你决定:可以唤醒你的App;也可以传一些信息给你。

给自己的App注册一个URL Scheme非常简单,就是在info.plist文件里定义两个键值就OK。如下图所示:

url scheme

  1. 添加一个叫URL types的键值。
  2. 给其下的Item 1添加一个URL identifier,格式为Reverse Domain Name:com.mycompany.myapp。
  3. 再加一个URL Scheme,然后给它定义一个值,任意字符串。比如:myapp。

定义结束,就可以使用下面的模式来发送一个URL:

myapp://
myapp://some/path/here
myapp://?foo=1&bar=2
myapp://some/path/here?foo=1&bar=2

然后,你的App的UIApplicationDelegate会收到一个消息。若你想自己处理该URL,可以重载下面这个方法:

[plain] view plaincopy
  1. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url   
  2. {  
  3.     // Do something with the url here  
  4. }  

例如,把传过来的URL保存在本地:

[plain] view plaincopy
  1. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  
  2. {  
  3.     if (!url)  
  4.     {  
  5.         return NO;  
  6.     }  
  7.   
  8.     NSString *URLString = [url absoluteString];  
  9.     [[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];  
  10.     [[NSUserDefaults standardUserDefaults] synchronize];  
  11.     return YES;  










Add Row  URL types,然后按照上面的设置,URL identifier   自定义,item(todolist部分) 自定义;

上面显示item1实际为item0;

下面的为接收到外部调用的时候程序启动,响应方法,在safari输入:todolist://com.acme.ToDoList ,

[cpp] view plaincopyprint?
  1. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {  
  2.       
  3.     if ([[url scheme] isEqualToString:@"todolist"]) {  
  4.         NSLog(@"外部调用成功");  
  5.     }  
  6.     return YES;  
  7. }  

下面的为自身调用safari浏览器,运行谷歌地图:

[cpp] view plaincopyprint?
  1. NSString* searchQuery = @"1 Infinite Loop, Cupertino, CA 95014";  
  2. searchQuery = [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
  3. NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", searchQuery];  
  4. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];  


[cpp] view plaincopyprint?
  1. 调用谷歌地图(Google Maps)  
  2.   
  3. URL模式:http://maps.google.com/maps?q=<strong>${QUERY_STRING}</strong>  
  4. 代码示例:  
  5.   
  6. NSString* searchQuery = @"1 Infinite Loop, Cupertino, CA 95014";  
  7. searchQuery = [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
  8. NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", searchQuery];  
  9. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];  
  10.   
  11.    
  12.   
  13. 调用邮件客户端(Apple Mail)  
  14.   
  15. URL模式:mailto://<strong>${EMAIL_ADDRESS}</strong>  
  16. 代码示例:  
  17.   
  18. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@eyecm.com"]];  
  19.   
  20.    
  21.   
  22. 拨号(Phone Number)  
  23.   
  24. URL模式:tel://<strong>${PHONE_NUMBER}</strong>  
  25. 代码示例:  
  26.   
  27. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];  
  28.   
  29.    
  30.   
  31. 调用短信(SMS)  
  32.   
  33. URL模式:sms:<strong>${PHONENUMBER_OR_SHORTCODE}</strong>  
  34. 代码示例:  
  35.   
  36. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:10086"]];  
  37.   
  38.    
  39.   
  40. 调用浏览器(Safari Browser)  
  41.   
  42. 代码示例:  
  43.   
  44. NSURL *url = [NSURL URLWithString:@"http://eyecm.com"];  
  45. [[UIApplication sharedApplication] openURL:url];  
  46.   
  47.    
  48.   
  49. 调用应用商店(AppStore)  
  50.   
  51. URL模式:http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8  
  52. 代码示例:  
  53.   
  54. NSURL *appStoreUrl = [NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&amp;mt=8"];  
  55. [[UIApplication sharedApplication] openURL:appStoreUrl]; 

0 0
原创粉丝点击