gpt4 book ai didi

ios - 如何保存或存储 SKPayment 交易?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:27 27 4
gpt4 key购买 nike

我想将 SKPayment 交易存储在用户默认设置或设备上,我还想在以后(尚未完成)将交易数据存储在我的服务器上,以便用户可以恢复订阅如果需要/必要/可能的话。

我遇到的问题是我尝试保存到用户默认值,但它一直告诉我:

* -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '{ transaction = ""; }' of class '__NSCFDictionary'.

我使用的代码是:

 // Save
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:transaction forKey:@"transaction"];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"TransactionReceipt"];
[[NSUserDefaults standardUserDefaults] synchronize];

// Read
NSMutableDictionary *savedDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyTransactionReceipt"];
[[NSUserDefaults standardUserDefaults] synchronize];

NSLog(@"Saved stored transaction = %@", savedDict);

我还尝试保存事务对象本身而不是将其存储在字典中,但这总是给出错误,它会提示

"Attempt to insert non-insert property value ..."

我也尝试过使用 NSKeyedArchiver,但我不确定如何编写代码来处理 SKPaymentTransaction 的读写。

如有任何帮助,我们将不胜感激。

谢谢。

最佳答案

我通过将交易数据存储在文档目录内的 plist 中解决了这个问题。

这对我的要求来说更容易也更可靠。

对于那些希望这样做的人来说,基本代码如下。

// WRITING        
// Save to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *receiptStorageFile = [documentsDirectory stringByAppendingPathComponent:@"receipts.plist"];

NSLog(@"Reciept Storage File = %@", receiptStorageFile);

NSMutableArray *receiptStorage = [[NSMutableArray alloc] initWithContentsOfFile:receiptStorageFile];
if(!receiptStorage) {
receiptStorage = [[NSMutableArray alloc] init];
}

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:transaction.transactionIdentifier forKey:@"transactionIdentifier"];
[dict setObject:transaction.transactionDate forKey:@"transactionDate"];
[dict setObject:transaction.transactionReceipt forKey:@"transactionReceipt"];

// Write
[receiptStorage addObject:dict];
[receiptStorage writeToFile:receiptStorageFile atomically:YES];
[receiptStorage release];


// READING

// Get app documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"DocumentsDirectory = %@", documentsDirectory);

// Load from documents directory
NSArray *receipts = [[[NSArray alloc] initWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"receipts.plist"]] autorelease];
// Found a receipt on disk
if ([receipts count]>0) {
// Do something, read the dictionary, etc.
} // end if

关于ios - 如何保存或存储 SKPayment 交易?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com