AFNetworking 在多语言应用程序中崩溃

AFNetworking crashing in multilingual app

Ride[source_name]=2152,%20Mohali%20Stadium%20Rd,%20Phase%2010,%20Sector%2064,%20Sahibzada%20Ajit%20Singh%20Nagar,%20Punjab%20160062,%20भारत

此参数在使用印地语 运行 时导致崩溃,而在使用西班牙语和英语时效果很好。请给我建议。崩溃描述如下:-

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: URLString'


添加以下代码行以避免 url.

中的无效参数
NSString *str = ...; // Your URL
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 
NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set];

ios 9.0 之前弃用的代码:

NSString *str = ...; // Your URL
NSString *urlAsString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

检查这个

Objective - C

NSString *string = @"भारत";
NSString *encoded = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

Swift 3.0

let string = "भारत"
let urlString = string.addingPercentEncoding( withAllowedCharacters: . urlUserAllowed)

Output :: %E0%A4%AD%E0%A4%BE%E0%A4%B0%E0%A4%A4