2015-12-24 55 views
0

我使用AFNetworking为收汇核销写下面的代码,这让我的状态= 210002 虽然它给了我状态= 0 NSMutableURLRequest在应用程序内购买收据验证使用AFNetworking Objective-C的

请帮助自动续约我通过获取解决方案

NSString *strurl = @"https://sandbox.itunes.apple.com/verifyReceipt"; 
NSData *receipt = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]; 

NSDictionary *[email protected]{ 
          @"receipt-data" : [receipt base64EncodedStringWithOptions:0], 
          @"password" : @"xxxxxxxxxxxxxxxxxxxx", 
         }; 


NSData *jsonParam = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil]; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"]; 
[manager POST:strurl parameters:jsonParam success:^(AFHTTPRequestOperation *oprtation, id responseObject){ 
    NSLog(@"JSON: %@", responseObject); 

}failure:^(AFHTTPRequestOperation *operation, NSError *error){ 
    NSLog(@"Error: %@", error); 

}]; 

谢谢

回答

0

下面是我在我的应用程序中使用收据验证代码,但是我已经在迅速的实现。

我也使用NSMutableURLRequest进行Web服务调用iTunes服务器。

func verifyPaymentReceipt(){ 

    let mainBundle = NSBundle.mainBundle() as NSBundle; 
    let receiptUrl = mainBundle.appStoreReceiptURL; 
    let isPresent = receiptUrl?.checkResourceIsReachableAndReturnError(NSErrorPointer()); 

    if(isPresent == true){ 

     let data = NSData(contentsOfURL: receiptUrl!); 

     // Create the JSON object that describes the request 

     let requestContents = NSMutableDictionary(); 
     //   let encodeddata = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions()); 
     let encodeddata = data!.base64EncodedString(); 

     print("encodeddata = \(encodeddata)"); 

     requestContents.setObject(encodeddata, forKey: "receipt-data"); 
     requestContents.setObject("xxxxxxxxxxxxxxxxxxxxxxx", forKey: "password"); 
     var requestData : NSData? 
     do{ 
      requestData = try NSJSONSerialization.dataWithJSONObject(requestContents, options: NSJSONWritingOptions()); 
     }catch{ 
      NSLog("Error in json data creation at verifyPaymentReceipt"); 
     } 

     let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString 
     let file = "\(documentsPath)/requestData" 

     if(NSFileManager.defaultManager().createFileAtPath(file, contents: data, attributes: nil)){ 
      NSLog("File %@ ",file); 
     } 
     else{ 
      NSLog("error File %@ ",file); 
     } 



     if(requestData != nil){ 

      let strRequestData = NSString(data: requestData!, encoding: NSUTF8StringEncoding); 
      print(" strRequestData = \(strRequestData)"); 
      // Create a POST request with the receipt data. 

      let storeURL = NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt"); 
      let storeRequest = NSMutableURLRequest(URL: storeURL!); 
      storeRequest.HTTPMethod = "POST"; 
      storeRequest.HTTPBody = requestData; 

      // Make a connection to the iTunes Store on a background queue. 

      let queue = NSOperationQueue(); 
      NSURLConnection.sendAsynchronousRequest(storeRequest, queue: queue, completionHandler: { (response : NSURLResponse?, data : NSData?, error : NSError?) -> Void in 

       if(error != nil){ 
        //Handle Error 
       } 
       else{ 
        let d = NSString(data: data!, encoding: NSUTF8StringEncoding); 
        NSLog("DATA:%@", d!); 

        var jsonResponse: NSMutableDictionary? 
        do{ 
         jsonResponse = try NSJSONSerialization.JSONObjectWithData(data!, 
          options: NSJSONReadingOptions.AllowFragments) as? NSMutableDictionary; 
         print(jsonResponse); 

        }catch{ 
         NSLog("Parsing issue : verifyPaymentReceipt"); 
        } 

        if(jsonResponse != nil){ 

         let expirationDate: NSDate? = self.expirationDateFromResponse(jsonResponse!); 
         NSLog("Expiration Date: %@", expirationDate!); 

        } 
       } 
      }); 

     } 

    } 

} 

As you mention that your code works fine with NSMutableURLRequest but it returns 21002 with AFNetworking. 

21002 - receipt-data属性中的数据格式不正确或丢失。

这意味着您的编码收据数据在使用AFNetworking时格式错误。所以我认为这是使用AFNetworking进行编码的问题。

在iOS 7中,Apple在NSData上引入了新的base64方法,使其不必使用第三方base 64解码库,但我仍建议您尝试使用Base64编码进行收件编码。我希望这将解决您的AFNetworkig问题。正如我在验证从服务器端收到的这个编码库在这种情况下工作时,我也面临21002相同的问题。不知道如何,但它解决了我的问题在服务器端接收验证呼叫。希望它也能为你工作。

+0

在这种情况下,它也适用于我 –

相关问题