首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >空格NSURL连接

空格NSURL连接
EN

Stack Overflow用户
提问于 2013-03-07 00:48:08
回答 1查看 252关注 0票数 0

我正在尝试有一个名字字段,人们可以把名字放在那里,它将发送到一个php页面。

我已经在没有空格的情况下用这段代码工作了。

代码语言:javascript
复制
- (IBAction)Submit:(id)sender {

    NSString *strURL=[NSString stringWithFormat:@"http://www.bigwavemedia.co.uk/ios/contact.php?name=%@", nameField.text];

    NSURL *url=[NSURL URLWithString:strURL];
    self.request=[NSURLRequest requestWithURL:url];

    self.nsCon=[[NSURLConnection alloc] initWithRequest:self.request delegate:self];


    NSLog(@"out put = %@", self.request);
}

但一旦我使用空格加法器修复,它就不会被我的php页面拾取,埃文,尽管日志说它是工作的。

代码语言:javascript
复制
- (IBAction)Submit:(id)sender {

    NSString *strURL=[NSString stringWithFormat:@"http://www.bigwavemedia.co.uk/ios/contact.php?name=", nameField.text];

    strURL = [strURL stringByAppendingString:nameField.text];
    strURL = [strURL stringByAppendingString:@"'"];
    strURL = [strURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    NSURL *url=[NSURL URLWithString:strURL];
    self.request=[NSURLRequest requestWithURL:url];

    self.nsCon=[[NSURLConnection alloc] initWithRequest:self.request delegate:self];


    NSLog(@"out put = %@", self.request);
}

我是不是遇到了语法错误,或者以错误的方式处理此方法?

我的.h文件

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface ContactViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate, NSURLConnectionDataDelegate>{
    IBOutlet UITextField *nameField;
}
- (IBAction)Submit:(id)sender;
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (strong) NSURLConnection *nsCon;
@property (strong) NSURLConnection *request;
@property (strong) NSURLConnection *receivedData;

@end

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-07 00:51:41

在发出请求之前,您需要使用stringByAddingPercentEscapesUsingEncoding

代码语言:javascript
复制
NSURL *url = [NSURL URLWithString:
                 [[str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]        
                 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

对于您的代码,请尝试以下代码。

代码语言:javascript
复制
- (IBAction)Submit:(id)sender {

    NSString *strURL=[NSString stringWithFormat:@"http://www.bigwavemedia.co.uk/ios/contact.php?name=%@", nameField.text];

    NSURL *url = [NSURL URLWithString:
                     [[strURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]        
                     stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

    self.request=[NSURLRequest requestWithURL:url];

    self.nsCon=[[NSURLConnection alloc] initWithRequest:self.request delegate:self];

    NSLog(@"out put = %@", self.request);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15253209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档