【问题标题】:Accessing SKProductResponse from outside of object从对象外部访问 SKProductResponse
【发布时间】:2011-02-18 06:39:38
【问题描述】:

我正在将应用内购买集成到我的应用中,并创建了一个实现委托回调的对象:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *myProduct = response.products;
    // populate UI
    [request autorelease];
}

作为其方法之一。

在我的例子中,我有多个 SKProduct 对象,这些对象将由 response.products 返回。我想做的是能够在视图控制器中访问对象外部的 myProduct 数组,我在其中反映了一些 SKProduct 详细信息,例如价格和产品描述。

这是 In App Purchase 类的接口声明:

@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver> {

    NSArray *myProducts;
    SKProductsRequest *productsRequest;

}

// public methods
- (void)loadStore;
- (BOOL)canMakePurchases;
- (void)purchaseFeature:(NSString *)productId;

@property (nonatomic, retain) NSArray *myProducts;

@end

然后是我的视图控制器中的 viewDidLoad 方法:

- (void) viewDidLoad {
        /* Instantiate InAppPurchaseManager object then kick it off to collect Product info */
    iapManager = [InAppPurchaseManager new];
    [iapManager loadStore];

    SKProduct *myProduct;

    for (myProduct in iapManager.myProducts) {
            NSLog(@"Product title: %@" , myProduct.localizedTitle);
        NSLog(@"Product description: %@" , myProduct.localizedDescription);
        NSLog(@"Product price: %@" , myProduct.price);
        NSLog(@"Product id: %@" , myProduct.productIdentifier);
    }

} // end viewDidLoad

我得到一个例外:

[InAppPurchaseManager myProducts]:无法识别的选择器发送到实例 0x162d60

我在这里做错了什么,如何将我的 SKProduct 数据“导出”到我的视图控制器?任何想法都非常感谢!

另外,在 productsRequest: 方法中,我可以使用 NSLog 并通过相同的循环打印,并在控制台输出中漂亮地看到 SKProduct 数据;它只是在 viewDidLoad 方法中不起作用。

【问题讨论】:

  • 你合成了 myProducts 属性了吗?
  • 不,我没有那样做。但如果我这样做:@synthesize myProducts;我仍然无法在 viewDidLoad 方法或更广泛的视图控制器中看到 myProducts 变量中的任何数据。

标签: ios properties in-app-purchase


【解决方案1】:

好的。所以我错过了很多东西。感谢 Carl 让我指明了正确的方向:

1) 我在 InAppPurchaseManager.m 文件和 ViewController.m 文件中都缺少 myProducts 的综合语句。 2) 我需要 ViewController.h 文件中 myProducts 的 @property 语句。 3) 我需要在 InAppPurchaseManager.m 文件的 productsRequest 方法中使用 self 设置 myProducts 属性:

self.myProducts = response.products;

这就是为什么 myProducts 中保存的数据在 ViewController 中永远不可见的原因。

希望这对其他人有帮助!

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 2021-06-09
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多