XCode iOS用+替换空格

时间:2012-10-18 13:03:44

标签: ios xcode nsstring nsurl

需要帮助从文本字段条目中替换我的空格以发布到GET

我目前的代码:

    NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text];
NSURL *add = [NSURL URLWithString:urlString];

[messageView loadRequest:[NSURLRequest requestWithURL:add]];

但是我试过了

NSString *finalAdd = [add stringByReplacingOccurrencesOfString:@" " withString:@"+"];

2 个答案:

答案 0 :(得分:1)

您应该转义参数

NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", [messageBox.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];

答案 1 :(得分:0)

结帐this postthis SO question。两者都为编码网址提供了很好的解决方案。