2010-09-02 50 views
0

我試圖發送一些數據到PHP腳本在線提交在線分數爲我的iOS應用程序。NSMutableURLRequest setHTTPBody:crash

該腳本將要求帳戶憑證使用正確的轉義字符格式化爲HTTP請求。這是setHTTPBody方法,兌現應用:

 [text_field removeFromSuperview]; 
     text_field.text = @""; //Remove text so it doesn't show because the text field is still on top of the open gl view 
     text_field.enabled = NO; 
     NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"secret"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]; 
     [request setHTTPMethod: @"POST"]; 
     [request setHTTPBody: [[NSString stringWithFormat:@"username=%@&password=%@&score=%i",[ogl_view->username stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding],[text_field.text stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding],ogl_view->score[0]] dataUsingEncoding:NSUnicodeStringEncoding]]; 
     NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate:self]; 
     if (connection){ 
      received_data = [[NSMutableData data] retain]; 
      ogl_view->section = CURLING_PROCESS_CREDENTIALS; 
     }else{ 
      ogl_view->section = CURLING_LOGIN_OR_REGISTER; 
      UIAlertView *connection_alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Can't connect to server" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil]; 
      [connection_alert show]; 
      [connection_alert release]; 
     } 

我發現它在崩潰的setHTTPBody方法被稱爲點。有誰知道爲什麼?

謝謝你的回答。

編輯:這裏的崩潰日誌:

2010-09-02 19:02:30.291 iPhone Monkey Curling[431:207] -[DOMText stringByAddingPercentEscapesUsingEncoding:]: unrecognized selector sent to instance 0x5c97ce0 
    2010-09-02 19:02:30.293 iPhone Monkey Curling[431:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DOMText stringByAddingPercentEscapesUsingEncoding:]: unrecognized selector sent to instance 0x5c97ce0' 
    *** Call stack at first throw: 
    (
     0 CoreFoundation      0x02531919 __exceptionPreprocess + 185 
     1 libobjc.A.dylib      0x0267f5de objc_exception_throw + 47 
     2 CoreFoundation      0x0253342b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
     3 CoreFoundation      0x024a3116 ___forwarding___ + 966 
     4 CoreFoundation      0x024a2cd2 _CF_forwarding_prep_0 + 50 
     5 iPhone Monkey Curling    0x00004338 -[PasswordFieldDelegate textFieldShouldReturn:] + 450 
     6 UIKit        0x00699358 -[UIKeyboardImpl callShouldInsertText:] + 148 
     7 UIKit        0x05f29c86 -[UIKeyboardImplAccessibility(SafeCategory) callShouldInsertText:] + 70 
     8 UIKit        0x006a0693 -[UIKeyboardImpl addInputString:fromVariantKey:] + 107 
     9 UIKit        0x006a2957 -[UIKeyboardImpl handleKeyEvent:] + 1723 
     10 UIKit        0x007c31c0 -[UIKeyboardLayoutStar sendStringAction:forKey:] + 684 
     11 UIKit        0x007c79ba -[UIKeyboardLayoutStar touchUp:] + 2556 
     12 UIKit        0x006b99a3 -[UIKeyboardLayout touchesEnded:withEvent:] + 550 
     13 UIKit        0x0058a2ff -[UIWindow _sendTouchesForEvent:] + 567 
     14 UIKit        0x0056c1ec -[UIApplication sendEvent:] + 447 
     15 UIKit        0x00570ac4 _UIApplicationHandleEvent + 7495 
     16 GraphicsServices     0x02d97afa PurpleEventCallback + 1578 
     17 CoreFoundation      0x02512dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
     18 CoreFoundation      0x02473737 __CFRunLoopDoSource1 + 215 
     19 CoreFoundation      0x024709c3 __CFRunLoopRun + 979 
     20 CoreFoundation      0x02470280 CFRunLoopRunSpecific + 208 
     21 CoreFoundation      0x024701a1 CFRunLoopRunInMode + 97 
     22 GraphicsServices     0x02d962c8 GSEventRunModal + 217 
     23 GraphicsServices     0x02d9638d GSEventRun + 115 
     24 UIKit        0x00574b58 UIApplicationMain + 1160 
     25 iPhone Monkey Curling    0x00002388 main + 102 
     26 iPhone Monkey Curling    0x00002319 start + 53 
    ) 
+0

如果它崩潰,那麼會有調試器的崩潰日誌或回溯。請發佈它。 – bbum 2010-09-02 15:13:33

+0

以前沒有,但現在有。奇怪。我會發布日誌。我不知道爲什麼它說stringByAddingPercentEscapesUsingEncoding方法是無效的。 Bissare。 – 2010-09-02 18:04:55

回答

3

-setHTTPBody接受NSData,不是NSString。在設置之前,您需要將NSString轉換爲NSData

幸運的是,NSString有一個dataUsingEncoding:方法,使它非常容易。

+0

謝謝。我已經實現了這一點。不幸的是,它仍然崩潰。 – 2010-09-02 18:14:40

+0

您的崩潰日誌表明您正嘗試將stringByAdding ...發送到不是NSString的東西。我會研究這一點。 – 2010-09-02 18:35:55

+0

ogl_view-> username絕對是一個NSString,所以如果文本字段的文本屬性...我將不得不深入研究它。 由於您提出了另一個問題,我會接受您的答案作爲解決方案,我感謝您。 這裏的問題必須從其他地方開始...... – 2010-09-02 20:29:23