感谢 Jasarien 就此事与 Apple 联系。下面是一些示例代码,供任何发现此主题并试图解决如何更新应用内内容的人使用。
首先,当您收到来自 SKProductsRequest 的响应时,请根据您之前下载的内容的版本检查 downloadContentVersion。您可以使用如下代码获取现有内容的内容版本:
NSString *pathToYourContent = @""; // Put your path here
NSString *contentInfoPath = [pathToYourContent stringByAppendingPathComponent:@"ContentInfo.plist"];
NSDictionary *contentInfo = [NSDictionary dictionaryWithContentsOfFile:contentInfoPath];
NSString *contentVersion = [contentInfo objectForKey:@"ContentVersion"];
NSString *iapProductIdentifier = [contentInfo objectForKey:@"IAPProductIdentifier"];
然后,如果您发现该版本已更改,您可以提示用户更新。如果他们同意,则开始恢复。
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
然后,当交易开始进入时,您需要决定接受哪些交易,忽略哪些交易。在这个下载歌曲内容的应用程序示例中,我有一个 YHSongVersion 类,它存储我们下载的所有歌曲的 ID 和版本,这些存储在数组 self.ownedSongVersions 中。此示例将接受版本号不同或我们没有获得内容的任何恢复。
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStateFailed:
break;
case SKPaymentTransactionStatePurchased:
[self provideContentForTransaction:transaction];
break;
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStateRestored:
[self restorePurchaseIfRequired:transaction];
break;
}
}
}
/// On a restore download the content if either we don't have the content or the version number has changed.
- (void)restorePurchaseIfRequired:(SKPaymentTransaction *)transaction {
BOOL haveSong = NO;
SKDownload *download = [transaction.downloads objectAtIndex:0];
for (YHSongVersion *ownedSongVersion in self.ownedSongVersions) {
BOOL isSongForThisDownload = [ownedSongVersion.iapProductIdentifier isEqualToString:download.contentIdentifier];
if (isSongForThisDownload) {
haveSong = YES;
BOOL hasDifferentVersionNumber = ![ownedSongVersion.contentVersion isEqualToString:download.contentVersion];
if (hasDifferentVersionNumber) {
[self provideContentForTransaction:transaction];
}
else {
// Do nothing
[self completeTransaction:transaction];
NSLog(@"Ignoring restore for %@", ownedSongVersion.iapProductIdentifier);
}
}
}
if (!haveSong) {
[self provideContentForTransaction:transaction];
}
}