从Google Play商店和Apple App Store获取购买日期

时间:2017-06-04 20:12:01

标签: android ios iphone xamarin

我需要您的帮助以下主题:我们开发了一款适用于Android和iOS的Xamarin表单应用。用户必须为此付费。取决于购买日期,我们必须在应用程序中执行某些操作。

安装应用程序后,我们可以在应用程序中保存第一个安装日期。但是,如果用户在新设备上重新安装应用程序,则输出从头开始。所以我认为我们需要从应用商店(谷歌和Apple)请求购买日期。

我用谷歌搜索但没有明确答案如何做到这一点。 有人可以帮忙解决这个问题吗? 如何申请该应用的购买日期?

提前多多感谢!

1 个答案:

答案 0 :(得分:0)

对于Apple来说,这很简单。只需使用Receipt Validation。

文档:https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html

示例代码:

-(void)getReceipt //check if local recipt exists.  if not, get one
{
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
    NSError* err = nil;
    if (![receiptURL checkResourceIsReachableAndReturnError:&err]){ //if no local receipt
        SKReceiptRefreshRequest* request = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil];
        request.delegate = self;
        [request start];
   }
    else
        [self getReceipt2:receipt];

}


-(void)getReceipt2:(NSData *)receipt //check recipt with apple and parse it.
{
    NSError *error;
    NSDictionary *requestContents = @{
                                      @"receipt-data": [receipt base64EncodedStringWithOptions:0]
                                      };
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                          options:0
                                                            error:&error];

    if (!requestData) { NSLog(@"RequestData Error");}

    NSURL *storeURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"]; //https://sandbox.itunes.apple.com/verifyReceipt for sandbox
    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) {
                                   NSLog(@"Connection Error");
                               } else {
                                   NSError *error;
                                   NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                   if (!jsonResponse) { NSLog(@"Json Error"); return;}
                                   NSArray *results = [jsonResponse valueForKey:@"receipt"];
                                   NSString *strPurchaseDate = [results valueForKey:@"original_purchase_date"];
                                   }

}

对于Android,我还没找到方法...... :(