NSURL总是返回Nil [英] NSURL Always Returns Nil

查看:54
本文介绍了NSURL总是返回Nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WCF服务,我想从我的Web服务中获取数据.但是URL总是返回nil.为什么?我想向来自文本字段值的WCF发送参数.例如;

I have WCF service and I want to get data from my web service. But URL always returns nil. Why? I want to send parameter to WCF which comes from text field value. For example;

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]];

我尝试了不同的方法,但结果没有改变.

I tried different things but result didn't change.

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

或:

NSURL *URL = [[NSURL alloc] initWithString:[request stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

推荐答案

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

另外, [NSString stringByAddingPercentEscapesUsingEncoding:] 对于URL编码可能会有些问题.使用 Core Foundation 有一种更安全的方法.参见 http://madebymany.com/blog/url-encoding-an-例如nsstring-on-ios (找不到SO问题).

On a separate note, [NSString stringByAddingPercentEscapesUsingEncoding:] can be a bit problematic for URL encoding. There is a safer way using Core Foundation. See http://madebymany.com/blog/url-encoding-an-nsstring-on-ios, for example (can't find the SO question).

这篇关于NSURL总是返回Nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆