UIApplication openUrl不使用格式化的NSString [英] UIApplication openUrl not working with formatted NSString

查看:120
本文介绍了UIApplication openUrl不使用格式化的NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码打开谷歌地图:

I have the following code to open google maps:

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

但它不起作用且没有错误。它只是没有打开。

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

推荐答案

URLWithString 需要一个百分比转义字符串。您的示例网址包含空格,这会导致创建无NSURL。此外,addressString还可能包含需要转义的字符。首先尝试以百分比形式转义url字符串:

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]];  

这篇关于UIApplication openUrl不使用格式化的NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆