1

启动我的应用程序时,它会检查用户是否已订阅。如果它没有检测到互联网连接,它会因错误而崩溃:

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);
      }];
    }
4

2 回答 2

2

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");
              }
          }];
于 2012-10-24T22:56:49.637 回答
1

使用可达性框架检查互联网连接。

于 2012-10-24T22:56:48.910 回答