NSURL总是返回Nil

编程入门 行业动态 更新时间:2024-10-26 01:28:34
本文介绍了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:@"www.xxxxxxxx/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:@"www.xxxxxxxx/CCWCF.svc/MethodName/%@",txtfield.text]];

或:

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

推荐答案

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

另外, [NSString stringByAddingPercentEscapesUsingEncoding:] 对于URL编码可能会有些问题.使用 Core Foundation 有一种更安全的方法.参见 madebymany/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 madebymany/blog/url-encoding-an-nsstring-on-ios, for example (can't find the SO question).

更多推荐

NSURL总是返回Nil

本文发布于:2023-10-19 19:18:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSURL   Nil

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!