आप AFHTTPSessionManager
उपवर्गीकरण या सीधे कोई AFURLSessionManager
आप निम्न विधि का इस्तेमाल कर सकते हैं एक ब्लॉक में किसी कार्य के पूरा होने के बाद मार डाला सेट करने के लिए उपयोग कर रहे हैं:
/**
Sets a block to be executed as the last message related to a specific task, as handled by the `NSURLSessionTaskDelegate` method `URLSession:task:didCompleteWithError:`.
@param block A block object to be executed when a session task is completed. The block has no return value, and takes three arguments: the session, the task, and any error that occurred in the process of executing the task.
*/
- (void)setTaskDidCompleteBlock:(void (^)(NSURLSession *session, NSURLSessionTask *task, NSError *error))block;
बस जो कुछ भी आप के लिए क्या करना चाहते हैं प्रदर्शन उस में सत्र के प्रत्येक कार्य:
[self setTaskDidCompleteBlock:^(NSURLSession *session, NSURLSessionTask *task, NSError *error) {
if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 500) {
}
}
}];
संपादित करें: वास्तव में यदि आपको प्रतिक्रिया ऑब्जेक्ट में लौटाई गई त्रुटि को संभालने की आवश्यकता है तो उपरोक्त विधि नौकरी नहीं करेगी। अपने AFHTTPSessionManager
उपवर्ग में
@interface MyJSONResponseSerializer : AFJSONResponseSerializer
@end
@implementation MyJSONResponseSerializer
#pragma mark - AFURLResponseSerialization
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error
{
id responseObject = [super responseObjectForResponse:response data:data error:error];
if ([responseObject isKindOfClass:[NSDictionary class]]
&& /* .. check for status or error fields .. */)
{
// Handle error globally here
}
return responseObject;
}
@end
और सेट यह: एक तरह से अगर आप AFHTTPSessionManager
उपवर्गीकरण रहे हैं उपवर्ग के लिए और एक कस्टम प्रतिक्रिया serializer सेट के साथ यह responseObjectForResponse:data:error:
ऐसे ही अतिभारित है हो सकता है
@interface MyAPIClient : AFHTTPSessionManager
+ (instancetype)sharedClient;
@end
@implementation MyAPIClient
+ (instancetype)sharedClient {
static MyAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[MyAPIClient alloc] initWithBaseURL:[NSURL URLWithString:MyAPIBaseURLString]];
_sharedClient.responseSerializer = [MyJSONResponseSerializer serializer];
});
return _sharedClient;
}
@end
मैंने अधिसूचनाओं के बारे में सोचा नहीं है - अनुरोधों को लागू करने के अनुकूलन से कहीं बेहतर और कम घुसपैठ। आपका बहुत बहुत धन्यवाद! प्रमाणीकरण चुनौती ब्लॉक के रूप में: मैं वास्तव में बुनियादी लेख के बजाय टोकन प्रमाणीकरण का उपयोग कर रहा हूं, इसलिए मुझे लगता है कि काम नहीं करेगा, है ना? 401 का उल्लेख करके आपको गुमराह करने के लिए खेद है। बोनस प्रश्न: "अवैध टोकन" के लिए सही प्रतिक्रिया कोड क्या होगा? 400? –
मुझे यकीन नहीं है कि "अवैध टोकन" के लिए सही प्रतिक्रिया कोड क्या है। शायद 403 अधिक उपयुक्त है। – Felix
AFAIK 403 प्रमाणीकरण के बजाय असफल * प्रमाणीकरण * के लिए अधिक है ("प्रमाणीकरण सफल हुआ (यदि कोई है), लेकिन आपको ऐसा करने की अनुमति नहीं है")। लेकिन कभी नहीं, यह एक और सवाल है। आपकी सहायता के लिए एक बार फिर से धन्यवाद। –