2013-03-06 69 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