【问题标题】:Get purchase date from Google Play Store and Apple App Store从 Google Play Store 和 Apple App Store 获取购买日期
【发布时间】:2017-11-05 14:26:02
【问题描述】:

我在以下主题方面需要您的帮助:我们已经开发了一个适用于 Android 和 iOS 的带有 Xamarin 表单的应用程序。用户必须为此付费。取决于购买日期,我们必须在应用中做一些事情。

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

我用谷歌搜索了它,但没有明确的答案。 有人可以帮忙吗? 如何请求应用的购买日期?

提前非常感谢!

【问题讨论】:

  • 用户为整个应用付费?例如,您从相应的商店下载您的应用程序会收费,或者您是否引用了应用程序内购买?

标签: android ios iphone xamarin


【解决方案1】:

对于 Apple,这很容易。只需使用收据验证。

文档: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,我还没有找到方法... :(

【讨论】:

  • 我认为 OP 正在寻找 Xamarin.forms 解决方案。
猜你喜欢
  • 2015-06-30
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多