iOS-应用间通信之自定义URL Schemes
来源:互联网 发布:mysql语句大全 编辑:程序博客网 时间:2024/05/16 12:08
一、URL Schemes知识的了解
URL Scheme是类似http://, ftp://这样的东西,同样你也可以为自己的应用自定URL Scheme,其他应用通过此标识就可以访问你的应用,如果自定的URL Scheme 和系统应用的相同,则会调用系统应用,而不会调用自定的应用程序。
例如:invoking://com.hello/yourpath/?username=WT&password=123456&callback=myapp
其中invoking是URL Scheme 即[url scheme],com.hello是host,即[url host], yourpath是path,即[url path],username=WT&password=123456&callback=myapp是query,即[url query]。
二、调用自己开发的应用
1)在plist文件中,注册自定对外接口
CFBundleURLName(URL Identifier) A string containing the abstract name of the URL scheme. To ensure uniqueness, it is recommended that you specify a reverse-DNS style of identifier, for example, com.acme.myscheme.The string you specify is also used as a key in your app’s InfoPlist.strings file. The value of the key is the human-readable scheme name.
CFBundleURLSchemes(URL Schemes) An array of strings containing the URL scheme names—for example, http, mailto, tel, and sms.
可以设置多个(URL Schemes),设置成功后如下:
调用方的部分代码如下:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- CGRect rectTextView= CGRectMake(10.0f, 30.0f, 300.0f, 100.0f);
- self.textView = [[UITextView alloc] initWithFrame:rectTextView];
- [self.textView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
- [self.textView.layer setBorderWidth:0.5f];
- [self.textView setText:@"username=WT&password=123456&callback=invoking"];
- [self.view addSubview:self.textView];
- CGRect rect = CGRectMake(10.0f, 150.0f, 300.0f, 40.0f);
- UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button.frame = rect;
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [button setTitle:@"Login" forState:UIControlStateNormal];
- [button setBackgroundColor:[UIColor blueColor]];
- [button addTarget:self action:@selector(handle:) forControlEvents:UIControlEventTouchUpInside];
- [button.layer setMasksToBounds:YES];
- [button.layer setCornerRadius:5.0f];
- [self.view addSubview:button];
- }
- - (void)handle:(id)sender
- {
- NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
- @"invoked://com.hello/path?%@", self.textView.text]];
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url];
- } else {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"message"
- message:[NSString stringWithFormat:@"%@", url]
- delegate:self cancelButtonTitle:@"确定"
- otherButtonTitles:nil, nil nil];
- [alertView show];
- }
- }
被调用方的接收代码:
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- NSLog(@"%@", url);
- if ([[url scheme] isEqualToString:@"invoked"]) {
- if ([[url host] isEqualToString:@"com.hello"]) {
- NSString *query = [url query];
- NSArray *array = [query componentsSeparatedByString:@"&"];
- NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];
- for (NSString *item in array) {
- NSArray *valueArray = [item componentsSeparatedByString:@"="];
- [dic setValue:[valueArray objectAtIndex:1] forKey:[valueArray objectAtIndex:0]];
- }
- [self application:application didFinishLaunchingWithOptions:dic];
- }
- return YES;
- }
- return NO;
- }
同样,可以在传入参数中设置一些预留字段,以便以后扩展,要实现再能够回调回去,就可以加一个callback字段,如:callback=myapp 后面跟上自己应用的URL Scheme,再将执行完成的结果返回回去。代码处理和被调用方大致相同。
0 0
- iOS-应用间通信之自定义URL Schemes
- iOS-应用间通信之自定义URL Schemes
- iOS-应用间通信之自定义URL Schemes
- iOS上自定义URL Schemes
- 如何获取ios应用的URL Schemes
- 获取iOS应用的URL Schemes
- iOS 应用之间相互跳转URL Schemes
- 自定义URL Schemes
- [iOS URL Schemes相关] URL Schemes 转载
- 应用间相互跳转(URL Schemes)
- iOS URL schemes 查看
- iOS URL schemes
- ios 更多 Url Schemes
- iOS URL Schemes 使用详解 什么是 URL Schemes
- iOS SDK: Working with URL Schemes
- iOS SDK: Working with URL Schemes
- iOS 内置URL schemes简介(1)
- iOS 内置URL schemes简介(2)
- Android 手势--滑动
- 入职后的感悟
- archlinux如何解决txt文档乱码
- windows如何启动telnet功能
- leveldb之log文件
- iOS-应用间通信之自定义URL Schemes
- 十天
- 一起学Maven(Maven中的生命周期与插件机制)<七>
- 人生若旅,磨难常伴
- C# 注册并使用sqlite 自定义函数
- 单元测试中内部输入的六种情形
- tnsnames.ora中的一个连接串不能从LISTENER连接时通过另一个监听连接(failover)
- 2004有感
- [C++_13]异常