我正在尝试对字符串进行编码(通过HTTP post请求发送)以接受可以在iPhone上键入的所有字符。
以下适用于我在典型英语桌面键盘上尝试的任何角色
let userPassword = "password1"
let encodedPassword = userPassword.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet())
但不识别重音字符,如á,é,í,ó,ú,ü,ñ等等(可以通过按住a,e,i ...在标准iPhone键盘上访问)。是否有NSCharacterSet
或简单的扩展名,其中包含标准iPhone键盘上的所有/所有字符?
编辑:这是我正在制作的请求的代码
let username = "joe"
let password = "pássword"
let encodedUsername = username.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet())
let encodedPassword = password.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet())
let request = NSMutableURLRequest(URL: NSURL(string: "https://www.url.com")!)
request.HTTPMethod = "POST"
let postString = "id="+encodedUsername+"&pw="+encodedPassword
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
...
答案 0 :(得分:0)
只需在接受用户输入时添加此项,这样用户就无法添加任何特殊符号或重音字符,也无需使用允许的字符进行编码: -
#define ACCEPTABLE_CHARACTERS @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *acceptedInput = [NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS];
if (![[string componentsSeparatedByCharactersInSet:acceptedInput] count] > 1){
NSLog(@"not allowed");
return NO;
}
else{
return YES;
}
}