一个程序如果需要跳转到另一个程序,则需要在info.plist文件中添加URL Types,展开之后修改信息,在URL Schemes中添加key值,相当于跳转的URL,如下:
在BuyApp中
- (IBAction)gotoPay:(UIButton *)sender
{
//要跳转到某个APP,需要知道这个APP的URL,打开这个URL即可跳转(URL中不能有空格,而且空格用&)
NSURL *url = [NSURL URLWithString:@"Pay://?name=iphone8&price=5000"];
[[UIApplication sharedApplication]openURL:url];
}
则在接受的PayApp的Appdelete文件中
//当本应用APP被其他APP打开时调用
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%@",url] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
//把url转成string
NSString *urlString = url.absoluteString;
//先截取参数,获取?右边的参数
NSString *param = [[urlString componentsSeparatedByString:@"?"]lastObject];
//分割各个参数
NSArray *paramArray = [param componentsSeparatedByString:@"&"];
//获取=右边商品名字参数
NSString *name = [[[paramArray objectAtIndex:0]componentsSeparatedByString:@"="]lastObject];
//获取=右边价钱参数
NSString *price = [[[paramArray objectAtIndex:1]componentsSeparatedByString:@"="]lastObject];
//将商品名字、价格先存起来
[[NSUserDefaults standardUserDefaults]setObject:name forKey:@"name"];
[[NSUserDefaults standardUserDefaults]setObject:price forKey:@"price"];
[[NSUserDefaults standardUserDefaults]synchronize];
//或者发通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"getName" object:self userInfo:@{@"name":name,@"price":price}];
return YES;
}
同理:返回也是一样的操作,效果如下: