iOS-定义协议从一个app打开另一个app
第一步:对B操作(对B的info.plist中自定义url types)
注意, 这里的URL Schemes必填, URL identifier选填。
另外,URL Schemes建议都小写,由于之后接收到数据的时候,不区分大写和小写, 都是转为小写。
规定的格式是 URL Schemes://URL identifier
第二步:对A操作(在响应方法中添加响应代码,如图所示)
上图代码:
NSURL *url = [NSURL URLWithString:@"martinapp://com.martin.app"];
[[UIApplication sharedApplication] openURL:url];
第三步:
模拟器运行:先对B进行Build操作,而对A进行run即可
真机运行:首先保证B存在于真机中,而后运行A即可。
你须要在 Appdelegate.m中加入例如以下代码, 来处理接受到请求后做出的处理
//处理URL请求
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSLog(@"%@", url);
if ([[url scheme] isEqualToString:@"myurltest"])
{
//处理链接
NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"新消息" message:text delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
[myAlert show];
return YES;
}
return NO;
}