一、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转码。