App应用之间跳转

一个程序如果需要跳转到另一个程序,则需要在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;
}
同理:返回也是一样的操作,效果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值