0

我试图有一个名称字段,人们可以在那里输入名称,它将发送到一个 php 页面。

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

- (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 页面拾取,尽管日志说它正在工作。

在此处输入图像描述

- (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 文件

#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

谢谢

4

1 回答 1

2

您需要stringByAddingPercentEscapesUsingEncoding在提出请求之前使用。

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

对于您的代码,请尝试此操作。

- (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);
}
于 2013-03-06T16:51:40.523 回答