启动我的应用时会检查用户是否已订阅。如果它检测到互联网连接,则会因错误而崩溃:
Failed to retrieve subscription with error 'The Internet connection appears to be offline.' and responseString: (null)*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
的.m
[self getPath:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (![responseObject isKindOfClass:[NSDictionary class]])
{
failureBlock(@"Invalid response received");
return;
}
NSDictionary *responseDict = (NSDictionary *)responseObject;
if (responseDict[@"error"] == nil)
{
[self saveUserDict:responseDict];
successBlock(responseDict);
}
else
{
failureBlock(responseDict[@"error"]);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"Failed to log in with error '%@' and response: %@", error.localizedDescription, operation.responseString);
failureBlock(@"An unknown error occurred");
}];
}
- (void)getSubscriptionWithSuccessBlock:(void (^)(NSDictionary *subscriptionDict))successBlock
failureBlock:(void (^)(id responseObject))failureBlock
{
static NSString *path = @"/api/subscription";
NSDictionary *parameters = @{
@"userId" : userDict[@"userId"],
@"token" : userDict[@"token"]
};
DebugLog(@"Getting subscription with parameters: %@", parameters);
[self getPath:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (![responseObject isKindOfClass:[NSDictionary class]])
{
failureBlock(@"Invalid response received");
return;
}
NSDictionary *subscriptionDict = (NSDictionary *)responseObject;
if (subscriptionDict[@"error"] == nil)
{
DebugLog(@"Successfully retrieved subscription");
successBlock(subscriptionDict);
}
else
{
failureBlock(responseObject);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"Failed to retrieve subscription with error '%@' and responseString: %@", error.localizedDescription, operation.responseString);
id responseObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
options:0
error:nil];
failureBlock(responseObject);
}];
}
答案 0 :(得分:1)
检查与Reachability框架的互联网连接。
答案 1 :(得分:1)
在继续执行以下行之前,您需要检查error
。如果出现错误,请不要使用空数据调用JSONObjectWithData
:方法。
[self getPath:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (![responseObject isKindOfClass:[NSDictionary class]])
{
failureBlock(@"Invalid response received");
return;
}
NSDictionary *responseDict = (NSDictionary *)responseObject;
if (responseDict[@"error"] == nil)
{
[self saveUserDict:responseDict];
successBlock(responseDict);
}
else
{
failureBlock(responseDict[@"error"]);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (!error) {
DebugLog(@"Failed to retrieve subscription with error '%@' and responseString: %@", error.localizedDescription, operation.responseString);
id responseObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
options:0
error:nil];
failureBlock(responseObject);
} else {
//handle the error scenario
failureBlock(@"error occured");
}
}];