initWithContentsOfURL:error:当内容为特殊字符时返回(null)

时间:2012-03-28 17:28:26

标签: objective-c cocoa-touch encoding nsstring

initWithContentsOfURL:error:返回(null)时应返回"São Tomé and Príncipe"。我使用没有特殊字符的文本测试了它,然后它工作正常。

NSString *myURL = [NSString stringWithString:@"http://mywebsite.com/api-call.php"];
NSURL *url = [[NSURL alloc] initWithString:myURL];
NSString *strResult = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",strResult);

我猜问题与编码有关,但我不知道如何修复它。

1 个答案:

答案 0 :(得分:0)

感谢Iulius Caesar我发现我的文档没有以UTF-8编码方式返回数据。

我通过在代码顶部添加header('Content-Type: text/html; charset=UTF-8');并将其保存为UTF-8格式文件来修改php文件。现在它有效!