我无法理解下面的代码:
for (SKProduct * skProduct in skProducts){
IAPProduct * product = _products[skProduct.productIdentifier];
。H
@property (nonatomic, strong) NSMutableDictionary * products; //not synthesized in .m
.m
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
_productsRequest = nil;
NSArray * skProducts = response.products;
for (SKProduct * skProduct in skProducts)
{
IAPProduct * product = _products[skProduct.productIdentifier];
product.skProduct = skProduct;
product.availableForPurchase = YES;
}
Q1。SKProduct 是一个 Apple StoreKit 类。我们如何能够将 skProducts 数组的元素分配给 SKProduct 的实例?我以为我们只能为属性和 IVAR 赋值?
Q2。我们如何能够创建 IAPProduct 的实例并将其设置为等于 _products[skProduct.productIdentifier],我认为这只是一个 NSString?
如果您想查看 IAAProduct 的 .h 和 .m,请告诉我。谢谢
更新:添加 IAPProduct .h
@class SKProduct;
@interface IAPProduct : NSObject
- (id)initWithProductIdentifier:(NSString *)productIdentifier;
- (BOOL)allowedToPurchase;
@property (nonatomic, assign) BOOL availableForPurchase;
@property (nonatomic, strong) NSString * productIdentifier;
@property (nonatomic, strong) SKProduct * skProduct;
@property (nonatomic, assign) BOOL purchaseInProgress;
@property (nonatomic, assign) BOOL purchase;
@end
.m
@implementation IAPProduct
- (id)initWithProductIdentifier:(NSString *)productIdentifier {
if ((self = [super init])) {
self.availableForPurchase = NO;
self.productIdentifier = productIdentifier;
self.skProduct = nil;
}
return self;
}
- (BOOL)allowedToPurchase {
if (!self.availableForPurchase) return NO;
if (self.purchaseInProgress) return NO;
if (self.purchase) return NO;
return YES;
}
@end
更新 2:
#import <Foundation/Foundation.h>
typedef void (^RequestProductsCompletionHandler) (BOOL success, NSArray *products);
@interface IAPHelper : NSObject
@property (nonatomic, strong) NSMutableDictionary *products;
-(id)initWithProducts:(NSMutableDictionary *)products;
-(void)requestProductsWithProductIdentifiers:(RequestProductsCompletionHandler)completionHander;
@end
.m
#import "IAPHelper.h"
#import "IAPProduct.h"
#import <StoreKit/StoreKit.h>
@interface IAPHelper () <SKProductsRequestDelegate>
@end
@implementation IAPHelper
{
SKProductsRequest * _productsRequest;
RequestProductsCompletionHandler _completionHandler;
}
-(id)initWithProducts:(NSMutableDictionary *)products
{
if (self = [super init])
{
_products = products;
}
}
- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHander
{
// 4
_completionHandler = [completionHander copy];
NSMutableSet *productsIdentifiers = [NSMutableSet setWithCapacity:_products.count];
for(IAPProduct *product in _products.allValues)
{
product.availableForPurchase = NO;
[productsIdentifiers addObject:product.productIdentifier];
}
_productsRequest = [[SKProductsRequest alloc]
initWithProductIdentifiers:productsIdentifiers];
_productsRequest.delegate = self;
[_productsRequest start];
}
#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"Loaded list of products...");
_productsRequest = nil;
// 1
NSArray * skProducts = response.products;
for (SKProduct * skProduct in skProducts)
{
IAPProduct * product = _products[skProduct.productIdentifier];
product.skProduct = skProduct;
product.availableForPurchase = YES;
}
// 2
for (NSString * invalidProductIdentifier in response.invalidProductIdentifiers)
{
IAPProduct * product = _products[invalidProductIdentifier];
product.availableForPurchase = NO;
NSLog(@"Invalid product identifier, removing: %@", invalidProductIdentifier);
}
// 3
NSMutableArray * availableProducts = [NSMutableArray array];
for (IAPProduct * product in _products.allValues) {
if (product.availableForPurchase) {
[availableProducts addObject:product];
}
}
_completionHandler(YES, availableProducts);
_completionHandler = nil;
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to load list of products.");
_productsRequest = nil;
}
@end