从对象外部访问 SKProductResponse
我正在将应用程序内购买集成到我的应用程序中,并创建了一个实现委托回调的对象:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
// populate UI
[request autorelease];
}
作为其方法之一。
就我而言,我有多个 SKProduct 对象,这些对象将由 response.products 返回。我想要做的是能够访问视图控制器中对象外部的 myProduct 数组,其中我反映了一些 SKProduct 详细信息,例如价格和产品描述。
这是应用程序内购买类的接口声明:
@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 方法中不起作用。
I'm integrating In App Purchase into my app and have created an object that implements the delegate callback:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
// populate UI
[request autorelease];
}
as one of its methods.
In my case I have multiple SKProduct objects that will be returned by response.products. What I'd like to do is be able to access the myProduct array outside of the object in a view controller where I reflect some of the SKProduct details like price and product description.
Here's the interface declaration for the In App Purchase class:
@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
Then the viewDidLoad method in my view controller:
- (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
I get an exception:
[InAppPurchaseManager myProducts]: unrecognized selector sent to instance 0x162d60
What am I doing wrong here, and how can I "export" my SKProduct data to my view controller? Any ideas are much appreciated!
Also, in the productsRequest: method, I'm able to use NSLog and print through the same loop and see the SKProduct data beautifully reflected in the console output; it just doesn't work in the viewDidLoad method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。所以我遗漏了很多东西。感谢 Carl 为我指明了正确的方向:
1) 我在 InAppPurchaseManager.m 文件和 ViewController.m 文件中都缺少 myProducts 的综合语句。
2)我需要 ViewController.h 文件中 myProducts 的 @property 语句。
3) 我需要在 InAppPurchaseManager.m 文件的 productsRequest 方法中使用 self 设置 myProducts 属性:
这就是为什么 myProducts 中保存的数据在 ViewController 中永远不可见的原因。
希望这对其他人有帮助!
OK. So there were a number of things I was missing. Thanks to Carl for getting me pointed in the right direction:
1) I was missing synthesize statements for myProducts in both the InAppPurchaseManager.m file and the ViewController.m file.
2) I needed @property statements for myProducts in the ViewController.h file.
3) I needed to set the myProducts property using self in the productsRequest method of the InAppPurchaseManager.m file:
This is why the data held in myProducts was never visible in the ViewController.
Hope this helps somebody else out there!