[iOS]URL编码和解码

  1. 首先来看下什么样的是URL编码(字符串中带有%22 类似这样的)
    NSString *str = @"http://m.tuniu.com/api/home/data/index/c/%7B%22v%22%3A%227.1.0%22%2C%22ct%22%3A20%2C%22dt%22%3A1%2C%22p%22%3A11210%2C%22cc%22%3A2500%7D/d/%7B%22clientModel%22%3A%22HONOR+H30-L01%22%2C%22width%22%3A720%7D�"
    把URL解码
    // 解码

(iOS9.0(包括9.0)以上使用):

NSString *str2 = [str stringByRemovingPercentEncoding];

(iOS9.0以下使用)

  NSString *str2 = 

[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
把URL编码
// 编码

(iOS9.0(包括9.0)以上使用)

NSString *str3 =
[str2 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];

(iOS9.0以下使用)

NSString *str3 =
[str2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

你可能感兴趣的:([iOS]URL编码和解码)