我正在尝试创建一个名称字段,人们可以在其中输入名称并将其发送到 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 页面拾取,evan 尽管日志显示它正在工作。
- (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
谢谢
最佳答案
在发出请求之前,您需要使用 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);
}
关于php - 空间 NSURL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253209/