开发者

UIApplication openUrl not working with formatted NSString

开发者 https://www.devze.com 2022-12-24 18:19 出处:网络
I have the following code to open google maps: NSString *urlString = [NSString stringWithFormat:@\"http://maps.google.com/maps?q=%@, Anchorage, AK\",addressStri开发者_高级运维ng];

I have the following code to open google maps:

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@, Anchorage, AK",addressStri开发者_高级运维ng];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

But it doesn't work and there is no error. It just doesn't open.


URLWithString requires a percent-escaped string. Your sample url contains spaces which results in a nil NSURL being created. Additionally, the addressString may also contain characters that need to be escaped. Try percent-escaping the url string first:

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@, Anchorage, AK",addressString];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];  


Need to escape the urlString , else [NSURL URLWithString:urlString] will return nill.

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@, Anchorage, AK",addressString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号