mainBundle上的appStoreReceiptURL总是返回nil

时间:2013-11-17 04:47:13

标签: ios objective-c in-app-purchase

此方法appStoreReceiptURL是SKPaymentTransaction上已弃用的transactionReceipt方法的替代品。每个人都说要改用这个电话:

NSURL *theURL = [[NSBundle mainBundle] appStoreReceiptURL];

如果有收据,则应该将收据返回给收据。但对我来说没有一个,因为这个值是零,而且据我所知,它不应该是。我在iOS 7上运行并且已经完成了一些应用内购买(设备上的沙箱)。现在我正在尝试添加另一个应用内购买,自动续订订阅,我需要深入了解收据以获得订阅到期日期。但我无法通过这个简单的步骤,因为价值总是为零。

有谁知道为什么?

2 个答案:

答案 0 :(得分:22)

有点晚了,但它可能对某人有用:

-(void) someMethod {
    NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
    if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]])
    {

        NSData *ios7ReceiptData = [NSData dataWithContentsOfURL:receiptUrl];
        //Do stuff

    } else {
        NSLog(@"iOS 7 AppReceipt not found %@, refreshing...",iapID);
        SKReceiptRefreshRequest *refreshReceiptRequest = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:@{}];
        refreshReceiptRequest.delegate = self;
        [refreshReceiptRequest start];
    }
}

- (void)requestDidFinish:(SKRequest *)request {
    if([request isKindOfClass:[SKReceiptRefreshRequest class]])
    {
        //SKReceiptRefreshRequest
        NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
        if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
            NSLog(@"App Receipt exists");
            //Do stuff
        } else {
            NSLog(@"Receipt request done but there is no receipt");

            // This can happen if the user cancels the login screen for the store.
            // If we get here it means there is no receipt and an attempt to get it failed because the user cancelled the login.
            //[self trackFailedAttempt];
        }
    }
}

`

答案 1 :(得分:1)

它现在是iOS 8.4和Xcode 6.4所以也许历史不同,但我发现这个方法调用总是在模拟器中运行时返回nil。在真实设备上,它按照Apple记录的方式工作:返回应用收据的存储路径 - 不保证在那里有收据,也不保证收据是有效收据。