Iphone NSURL总是返回零

Iphone NSURL总是返回零,iphone,ios,nsurl,Iphone,Ios,Nsurl,我有WCF服务,我想从我的web服务中获取数据。但URL总是返回零。为什么?我想将参数发送到WCF,它来自文本字段值。比如, txtfield.text = @"ATAŞEHİR"; 怎么了 NSString *request = [NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@",txtfield.text]; NSURL *URL = [NSURL URLWithString:[requ

我有WCF服务,我想从我的web服务中获取数据。但URL总是返回零。为什么?我想将参数发送到WCF,它来自文本字段值。比如,

txtfield.text = @"ATAŞEHİR";
怎么了

NSString *request = [NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@",txtfield.text];
NSURL *URL = [NSURL URLWithString:[request stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
我尝试了不同的方法,但结果并没有改变

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@",txtfield.text]];
或:


您正在使用
NSASCIIStringEncoding
对unicode字符串进行编码,因此会得到奇怪的字符

使用
NSUTF8StringEncoding
如:

NSString *queryParemeters = [textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *request = [NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@", queryParemeters];
NSLog(@"Encoded URL :%@", request);
NSURL *URL = [NSURL URLWithString:request];

另一方面,
[NSString stringByAddingPercentEscapesUsingEncoding:
对于URL编码可能有点问题。使用
核心基础
有一种更安全的方法。例如,请参见(找不到SO问题)。

我认为是unicode字符。 使用
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding将其转换为

NSString *text = @"ATAŞEHİR";
NSString *convertedText = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *request = [NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@", convertedText];

您是否尝试了
NSUTF8StringEncoding
(您的字符串似乎是unicode格式的)?@Rajneesh071请不要为此使用
replaceAccuranceofString…
。百分比转义例程的功能要强大得多这是真的,但他应该只编码最后一部分,而不是整个URL!
NSString* encodedText = [txtfield.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *request = [NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@",encodedText];
NSURL *URL = [NSURL URLWithString:request];
NSString *text = @"ATAŞEHİR";
NSString *convertedText = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *request = [NSString stringWithFormat:@"http://www.xxxxxxxx.com/CCWCF.svc/MethodName/%@", convertedText];