Status code 0 on Google geocode API from iPhone/sim, but works fine on web (Non English characters)

荒凉一梦 提交于 2019-12-01 11:31:37

At some point in your code you're probably using the NSURL class, and:

The NSURL class will fail to create a new NSURL object if the path being passed is not well-formed—the path must comply with RFC 2396. Examples of cases that will not succeed are strings containing space characters and high-bit characters. Should creating an NSURL object fail, the creation methods return nil, which you must be prepared to handle.

You need to convert the high-bit characters in "Őrbottyán" into percent escapes, using [NSString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], before passing the string to NSURL. Modern browsers will silently make this conversion for you when you put a non-compliant string into the URL field, but in code you have to do it explicitly.


Edit to include quantumpotato's findings below: Google Maps will do the right thing if "Őrbottyán" is converted to "Orbottyan" (a "lossy" conversion to ASCII encoding), and that conversion can be performed with a round-trip through NSData:

NSData *data = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:dataString];
[dataString release];

I suspect "lossy conversion to ASCII" may not work with all websites, but it's been tested and verified with Google Maps, so there you have it. :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!