如何制作包含 | 的 NSURL (管道字符)?

发布于 2024-09-05 19:47:57 字数 356 浏览 2 评论 0原文

我正在尝试从我的 iPhone 应用程序访问谷歌地图的正向地理编码服务。 当我尝试从带有管道的字符串创建 NSURL 时,我只得到一个 nil 指针。

NSURL *searchURL = [NSURL URLWithString:@"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false"];

我在 google api 中没有看到任何其他方式可以不用管道发送边界坐标。 关于我如何做到这一点有什么想法吗?

I am trying to access google maps' forward geocoding service from my iphone app.
When i try to make an NSURL from a string with a pipe in it I just get a nil pointer.

NSURL *searchURL = [NSURL URLWithString:@"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false"];

I dont see any other way in the google api to send bounds coordinates with out a pipe.
Any ideas about how I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

城歌 2024-09-12 19:47:57

您是否尝试过用 %7C (字符 | 的 URL 编码值)替换管道?

Have you tried replacing the pipe with %7C (the URL encoded value for the char |)?

愁以何悠 2024-09-12 19:47:57

由于 stringByAddingPercentEscapesUsingEncoding 已弃用,您应该使用 stringByAddingPercentEncodingWithAllowedCharacters

Swift 答案:

let rawUrlStr = "http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
let urlEncoded = rawUrlStr.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let url = NSURL(string: urlEncoded)

编辑:Swift 3 答案:

let rawUrlStr = "http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
if let urlEncoded = rawUrlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    let url = NSURL(string: urlEncoded)
}

As stringByAddingPercentEscapesUsingEncoding is deprecated, you should use stringByAddingPercentEncodingWithAllowedCharacters.

Swift answer:

let rawUrlStr = "http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
let urlEncoded = rawUrlStr.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let url = NSURL(string: urlEncoded)

Edit: Swift 3 answer:

let rawUrlStr = "http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
if let urlEncoded = rawUrlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    let url = NSURL(string: urlEncoded)
}
这个俗人 2024-09-12 19:47:57

如果您想保证将来输入的任何奇怪字符的安全,请使用 stringByAddingPercentEscapesUsingEncoding 方法使字符串“URL 友好”...

NSString *rawUrlStr = @"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
NSString *urlStr = [rawUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *searchURL = [NSURL URLWithString:urlStr];

If you want to be safe for whatever weird characters you will put in the future, use stringByAddingPercentEscapesUsingEncoding method to make the string "URL-Friendly"...

NSString *rawUrlStr = @"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
NSString *urlStr = [rawUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *searchURL = [NSURL URLWithString:urlStr];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文