2015-12-08 122 views
1

我已經在沙盒環境下對IAP的消耗品類型進行了測試。我想確保我購買的物品有效或沒有。但結果總是以狀態「21004」返回。我對分享祕密不做任何事情。所以你可以看看我成功購買物品後跟着蘋果的示例代碼:收據驗證不正確

- (void)verifyStatus:(SKPaymentTransaction *)transaction { 

    NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]; 
    NSError *receiptError; 
    BOOL isPresent = [[[NSBundle mainBundle] appStoreReceiptURL] checkResourceIsReachableAndReturnError:&receiptError]; 
    if (!isPresent) { 
     NSLog(@"Validation failed"); 
    } 
    NSString *receiptStr = [receiptData base64EncodedStringWithOptions:0]; 
    NSDictionary *requestContents = @{@"receipt-data":receiptStr}; 
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents 
                 options:0 
                 error:nil]; 

    if (!requestData) { /* ... Handle error ... */ } 

    NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]; 
    NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL]; 
    [storeRequest setHTTPMethod:@"POST"]; 
    [storeRequest setHTTPBody:requestData]; 

    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
          if (connectionError) { 
           /* ... Handle error ... */ 
          } else { 
           NSError *error; 
           NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
           NSLog(@"Respond : %@",jsonResponse); 
           if (!jsonResponse) { /* ... Handle error ...*/ } 
           /* ... Send a response back to the device ... */ 
          } 
}]; 

} 

我得到了什麼問題?請分享你的經驗。謝謝

+0

有問題解決了嗎? –

+0

它仍然是錯誤 – Sovannarith

回答

1

請嘗試以下代碼和步驟SECRECT KEY

#define SHARED_SECRET @"SECRECT KEY" 

-(void)checkReceipt { 
    // verifies receipt with Apple 
    NSError *jsonError = nil; 

    NSString *receiptBase64 = [receiptData base64EncodedStringWithOptions:0];//receiptData NSData for validate Receipt 
    NSLog(@"Receipt Base64: %@",receiptBase64); 

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObjectsAndKeys: 
                   receiptBase64,@"receipt-data", 
                   SHARED_SECRET,@"password", 
                   nil] 
                 options:NSJSONWritingPrettyPrinted 
                 error:&jsonError 
         ]; 
    NSLog(@"%@",jsonData); 
    NSError * error=nil; 
    NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error]; 
    NSLog(@"%@",parsedData); 
    NSLog(@"JSON: %@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]); 
    // URL for sandbox receipt validation; replace "sandbox" with "buy" in production or you will receive 
    // error codes 21006 or 21007 
    NSURL *requestURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]; 

    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:requestURL]; 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody:jsonData]; 


    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:req queue:queue 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
           if (connectionError) { 
            /* ... Handle error ... */ 
           } else { 
            NSError *error; 
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
            NSLog(@"Respond : %@",jsonResponse); 
            if (!jsonResponse) { /* ... Handle error ...*/ } 
            /* ... Send a response back to the device ... */ 
           } 
          }]; 

} 

你需要改變「朱鴻KEY」與祕密密鑰你從iTunes Connect.sample樣子39fkjc38jd02mg72k9cn29dfkm39fk00

以下是創建/查看Secreate Key的步驟。

登錄iTunes Connect - >我的應用程序>選擇您的應用程序>應用內購買 購買>查看或生成共享密鑰。

+0

它的工作原理。謝謝Nimit Parekh – Sovannarith

+0

歡迎男人。 –