2015-12-24 56 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

在這種情況下,它也適用於我 –

相關問題