一早敬、iOS 10.0之后系統(tǒng)提供的方法告材,直接調(diào)用即可枷踏!
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
NSLog(@"success = %d",success);
}];
二凿可、傳遞參數(shù)
2.1傳遞簡單參數(shù)繁仁,直接使用?方式
和url get傳參一樣,假如要跳轉(zhuǎn)微信:
weixin://weixin.com/client?sourceType=10&hello=@"11"
2.2傳遞帶json參數(shù):需要對json做utf-8轉(zhuǎn)碼涉馅,再做拼接.
2.2.1字典轉(zhuǎn)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轉(zhuǎn)碼
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];
總結(jié):后面的URL是舉的例子,并不能實(shí)際跳轉(zhuǎn)黄虱,關(guān)鍵步驟是json數(shù)據(jù)還需要做utf-8轉(zhuǎn)碼控漠。