iOS 带参数跳转其他app

一、iOS 10.0之后系统提供的方法,直接调用即可!

[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
NSLog(@"success = %d",success);
}];

二、传递参数

2.1传递简单参数,直接使用?方式

和url get传参一样,假如要跳转微信:
weixin://weixin.com/client?sourceType=10&hello=@"11"

2.2传递带json参数:需要对json做utf-8转码,再做拼接.

2.2.1字典转json方法

- (NSString*)dictToJson:(NSDictionary *)dic
{
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

2.2.2json字符串utf-8转码

jsonStr = [jsonStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

2.2.3完整NSURL拼装

NSString * urlStr = [NSString stringWithFormat:@"weixin://weixin.com/client?sourceType=10&json=%@",jsonStr];
NSURL *url = [NSURL URLWithString:urlStr];

总结:后面的URL是举的例子,并不能实际跳转,关键步骤是json数据还需要做utf-8转码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。