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以下の行に進む前に、を確認する必要があります。エラーが発生した場合は、JSONObjectWithDatanull データを使用して : メソッドを呼び出さないでください。

[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 に答える