php - 空间 NSURL 连接

标签 php iphone ios xcode nsurlconnection

我正在尝试创建一个名称字段,人们可以在其中输入名称并将其发送到 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 尽管日志显示它正在工作。

enter image description here

- (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/

相关文章:

php - 在 Laravel 中获取每个用户的最新消息(行)

php - Laravel - 按数字(整数)排序​​,即使列类型是字符串

php - 请求->get() 提供的 Psalm 可能为空值

iphone - 单View从左到右的iOS动画

ios - 隐藏的iCarousel元素反射效果

android - 有没有办法在 Android 或 iOS 库中的 worklight connect 方法调用中指定超时?

php - WooCommerce Hook woocommerce_cancelled_order

iphone - 将 iPhone 应用程序更新到新版本时更改应用程序设置

iphone - PDF 渲染中的文本下划线和突出显示

IOS7 - 自动布局屏幕宽度