2013-03-06 66 views
0

我想要一个名称字段,人们可以放在那里名称,它会发送到一个PHP页面。空间NSURL连接

我有这个工作没有与此代码的空间...

- (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页面被拾起,埃文尽管日志说它的工作。

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 

感谢

+0

作为一个侧面说明,可能不会解决您的问题,而不是stringByReplacingOccurrencesOfString方法,你应该使用[unescapedString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]。 – 2013-03-06 16:54:14

回答

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); 
} 
+0

感谢您的快速回复!只要我们修复,我会在代码中出现一处错误,我会标记为正确的。继承人链接到图像。 http://s10.postimage.org/eb0pw9m3t/Screen_Shot_2013_03_06_at_16_58_27.png – Brent 2013-03-06 16:59:30

+0

我更新它使用NSUTF8StringEncoding,但现在我没有得到任何输出与空间。 – Brent 2013-03-06 17:01:51

+0

你得到什么错误? – 2013-03-06 17:09:45