iOS5:UTF-8编码根本不起作用

时间:2011-11-02 23:34:30

标签: cocoa encoding utf-8

我的应用程序能够使用POST函数将多个文本字段的输入发送到我服务器上的PHP脚本,该函数将消息发送到我的电子邮件。问题是,某些角色不会被“翻译”。例如:ü - >那张。

我已经检查了我的HTTP标头,但它仍然是UTF-8。

我也使用此代码转换字符:

[textFieldName.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversation:YES];

我做错了什么?甚至PHP文件都有这段代码:

function mail_utf8($to,$subject = '(No subject)',$message = '',$header = ''){
    $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
    if(mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header)) return true;
    else return false;
  }

编辑:

NSString *post = nil;
            post = [[NSString alloc] initWithFormat:@"vorname=%@&name=%@&plz=%@&ort=%@&email=%@&geburtsdatum=%@&eventdatum=%@",
                    [textFieldVorname.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [textFieldName.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [textFieldPLZ.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [textFieldOrt.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [textFieldEmail.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [textFieldGeburtsdatum.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [textFieldEventdatum.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
            NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:[NSURL URLWithString:@"http://www.url.com/document-send.php"]];
            [request setHTTPMethod:@"POST"];
            [request setValue: postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];
            [NSURLConnection connectionWithRequest:request delegate:self];

3 个答案:

答案 0 :(得分:2)

我通过将charset=utf-8明确添加到我的Content-Type标头修复了类似的问题POST JSON数据。它也可能适用于您的情况。

尝试:

[request setValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];

答案 1 :(得分:1)

方法stringByAddingPercentEscapesUsingEncoding返回一个新的NSString对象。您需要捕获返回值并使用它来代替后期数据中字段的原始值。

NSString *encodedText = [ textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ] ;
NSData *postData = [ encodedText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES ] ;

答案 2 :(得分:0)

好的,我发现了错误:

就像Peter Hosey所说:错误是在php中将utf-8字符集解码为ISO 8859-1。现在这是工作代码:

mail($email_to,$email_subject,utf8_decode($email_data),$email_header);

谢谢你们帮助我:)。