2014-03-30 31 views
0

我正在編寫一個iOS應用程序,它使用URL請求發送和接收數據的PHP文檔。這是我的代碼。鎖定時的Objective-C URL請求iPhone

NSString *myRequestString = [NSString stringWithFormat:@"userid=%@&recieverid=%@&messege=%@&type=%@&password=%@", ownID, _friendID, content_encoded, msg_type, password]; 
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"something.php"]]; 
    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    [request setHTTPBody: myRequestData]; 

    //My activiy Indicator starts here 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Now send a request and get Response 
     NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 
     NSString *result = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; 

     //Here some internal coding happens... 

     dispatch_async(dispatch_get_main_queue(), ^(void) { 

      //Stopping activity indicator 

     }); 
    }); 

但是,如果用戶鎖定了他的電話,而他(在其他應用程序,如Snapchat等,以及可能的)發送數據的應用程序凍結當用戶返回並已重新打開它。 我想知道如果應用程序連接到服務器,並且用戶關閉不會導致此錯誤發生的應用程序,這是否是更好的方式。

謝謝:) 安東 對不起我可憐的英語我不是母語的人。

+1

這有什麼好做的Xcode。 – mah

+0

爲什麼不能?在objective-c中沒有東西需要編碼嗎? – user3207681

+2

Xcode是一個IDE,它與程序中的錯誤無關,與您的程序與某些遠程服務器的交互無關。把它叫做Xcode就好像叫它與Mac有關(因爲你大概是用Mac開發的),或者稱之爲地球相關的,因爲在開發的時候,你在地球上。 – mah

回答

6

我建議:

  1. 指定後臺任務標識,由約翰·伍茲的建議,因此,如果用戶離開應用程序的請求的中間,它會嘗試在後臺繼續網絡請求。使用sendAsynchronousRequest而不是將sendSynchronousRequest調度到後臺。

  2. 確保你正確地檢測和處理錯誤(因爲它不是完全清楚問題是否問題在於你的問題的代碼或隨後處理的任何處理)。

  3. 無關,但我會避免使用bytes相關的NSData方法。

這樣:

// I'm guessing that you're percent encoding `messege` [sic], but I'd do it for all of those parameters (notably password) 

NSString *myRequestString = [NSString stringWithFormat:@"userid=%@&recieverid=%@&messege=%@&type=%@&password=%@", ownID, _friendID, content_encoded, msg_type, password]; 

// Use `dataUsingEncoding` rather than bytes rendition: 
// 
//  NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 

NSData *myRequestData = [myRequestString dataUsingEncoding:NSUTF8StringEncoding]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"something.php"]]; 
[request setHTTPMethod: @"POST"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
[request setHTTPBody: myRequestData]; 

// start background task 

UIBackgroundTaskIdentifier __block task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
    [[UIApplication sharedApplication] endBackgroundTask:task]; 
    task = UIBackgroundTaskInvalid; 
}]; 

// activity indicator starts here 

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    if (!data) { 
     NSLog(@"%s: sendAsynchronousRequest error: %@", __PRETTY_FUNCTION__, connectionError); 
    } else { 

     // use NSData rendition rather than bytes rendition: 
     // 
     //  NSString *result = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; 

     NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

     // here some internal coding happens... 
    } 

    // stop activity indicator here 

    // stop background task 

    if (task != UIBackgroundTaskInvalid) { 
     [[UIApplication sharedApplication] endBackgroundTask:task]; 
     task = UIBackgroundTaskInvalid; 
    } 
}]; 
+0

謝謝:)這很好! – user3207681

+0

謝謝你完美的作品! – user3207681