ios - 关于 appStoreReceiptURL 的问题

标签 ios objective-c iphone xcode ipad

我正在尝试使用 IAP 将我的付费应用程序转换为免费版本,所以基本上我需要检查用户是否购买了以前的版本然后解锁 IAP 项目,我不确定我是否在这里做对了!甚至可以在开发过程中检查和跟踪“appStoreReceiptURL”吗?这是我的代码:

  NSURL* url = [[NSBundle mainBundle] appStoreReceiptURL];
    NSLog(@"receiptUrl %@",[url path]);
    NSError* err = nil;
    if (![url checkResourceIsReachableAndReturnError:&err]){
        SKReceiptRefreshRequest* request = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil];
        request.delegate = self;
        [request start];
    }


-(void)requestDidFinish:(SKRequest*)request{
    if([request isKindOfClass:[SKReceiptRefreshRequest class]]){

        NSLog(@"YES, You purchased this app");
    }
}



-(void)request:(SKRequest*)request didFailWithError:(NSError *)error{

    NSLog(@"NO, you need to buy it ");

}

现在我可以使用我的 Apple ID 登录,登录后它告诉我是的,您购买了这个应用程序“,是的,我真的购买了我的应用程序!,我要确保一切正常。 这个过程是否应该在每次更新中发生?

最佳答案

这是一个简单的解决方案

#import <StoreKit/StoreKit.h>

不要忘记添加它的委托(delegate)

<SKPaymentTransactionObserver, SKProductsRequestDelegate>

支付验证

- (IBAction)boughtIt:(id)sender {

    NSURL* url = [[NSBundle mainBundle] appStoreReceiptURL];
    NSLog(@"receiptUrl %@",[url path]);
    NSError* err = nil;
    if (![url checkResourceIsReachableAndReturnError:&err]){
        SKReceiptRefreshRequest* request = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil];
        request.delegate = self;
        [request start];
         _activity.hidden = NO;
    }

}



-(void)requestDidFinish:(SKRequest*)request{
    if([request isKindOfClass:[SKReceiptRefreshRequest class]]){

        NSLog(@"YES, You purchased this app");

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Congrats !" message:@"Welcome To The World Of Dinosaurs" delegate:self
                                             cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isPurchased"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        _activity.hidden = YES;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}



- (void)request:(SKRequest*)request didFailWithError:(NSError *)error{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry !" message:@"It seems you did not purchase this app before, if you like to unlock all content and features please purchase Paleontologist Pack" delegate:self
                                         cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];

    [alert show];

    _activity.hidden = YES;

//    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"NO, you need to buy it ");

}

这仅在用户PURCHASED应用程序时有效,它不适用于兑换代码

关于ios - 关于 appStoreReceiptURL 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815668/

相关文章:

ios - linkingObjectsProperties 从 swift 2.0 覆盖

ios - 如何在没有导航 Controller 的情况下导航到另一个 Controller ?

objective-c - self.navigationController == nil 从 LocalNotification 唤醒后

ios - 用于为所有 URLRequest 添加自定义 header 字段的 URLRequest 和 URLSession 的拦截器?

ios - 使用 `UINavigationController` 切换到任意 View

ios - 如何删除 UIWebView 中的 HTML5 持久性数据库?

ios - 如何等待 NSURLConnection 委托(delegate)完成后再继续?

objective-c - OS X 的 UIGraphicsGetCurrentContext() 等价物是什么?

iphone - iPhone 5 与旧款 iPhone (3GS/4/4S) 上的 UITextField 位置不同

iphone - 使用 Controller 控制其他 Controller 时的 View 大小问题