2012-10-10 15 views
5

मैं NSURLConnection'ssendSynchronousRequest:returningResponse:error: विधि का उपयोग करें (एक अलग NSOperation सूत्र में) बाहरी सर्वर से कनेक्ट करने के लिए डेटा पुनर्प्राप्त करने के लिए। मुझे कैसे पता चलेगा कि ऑपरेशन समाप्त हो गया है, या कुछ अन्य नेटवर्क त्रुटि?कैसे करता है, तो NSURLConnection के sendSynchronousRequest पता लगाने के लिए: returningResponse: त्रुटि: समाप्त हो गया का समय समाप्त हो जा रहा है या अन्य त्रुटि

उत्तर

14

यदि कोई त्रुटि हुई, तो त्रुटि पैरामीटर गैर-शून्य होगा जब sendSynchronousRequest:returningResponse:error: रिटर्न होगा।

आप [NSError code] द्वारा दिए गए मान की जांच करके त्रुटि कोड पुनर्प्राप्त कर सकते हैं। समय के लिए त्रुटि कोड NSURLErrorTimedOut है।

उदाहरण के लिए:

NSError *error = nil; 
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] 

if (error.code == NSURLErrorTimedOut) { 
// Handle time out here 
} 
0

आप उपयोगकर्ता को अलर्ट प्रस्तुत कर सकते हैं और sendSynchronousRequest:returningResponse:error: में त्रुटि पैरामीटर को अलर्ट के संदेश में भेज सकते हैं।

कोड कुछ इस तरह होगा:

[NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error]; 

if (error) 
{ 
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
[alert show]; 
} 

आशा है कि यह मदद करता है !!

संबंधित मुद्दे