Objective-C and Swift URL encoding
问题 I have a NSString like this: http://www. but I want to transform it to: http%3A%2F%2Fwww. How can I do this? 回答1: To escape the characters you want is a little more work. Example code iOS7 and above: NSString *unescaped = @"http://www"; NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSLog(@"escapedString: %@", escapedString); NSLog output: escapedString: http%3A%2F%2Fwww The following are useful URL