我有两个视图控制器。其中一个显示可用的In App Purchase。当用户选择其中一个时,它会显示一个新的View Controllers,其中包含Purchase的详细信息和“Buy”按钮。
我将代码从第一个视图传递到第二个视图的方式如下:
-(IBAction)purchase1:(id)sender{
_purchasedController = [[iPadPurchasedViewController alloc] initWithNibName:nil bundle:nil];
_purchasedController.productID = @"xxxxx.xxxxx.xxxxx";
[self presentViewController:_purchasedController animated:YES completion:NULL];
[self.purchasedController getProductID:self];
}
因此,当用户选择一个按钮时,我将productID字符串传递给第二个ViewController。但是,我想在第一个View Controller上向用户显示价格。有人可以帮我吗?谢谢。
答案 0 :(得分:1)
编辑答案更完整:
在ViewController.h文件中应该有
@interface ViewController : UIViewController <SKProductsRequestDelegate> {
NSArray *allProducts;
}
然后在ViewController.m例如。在viewDidload
:
-(void) viewDidload {
[super viewDidLoad];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:@"xxxx1",@"xxxx2",nil]];
request.delegate=self;
[request start];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
allProducts = response.products;
NSLog(@"Fetched %d products", allProducts.count);
}
然后它可以这样使用:
-(NSString*) getPriceForProductWithID:(NSString*)productID{
if(allProducts.count < 1) {
NSLog(@"Did not fetch products yet");
}
else {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
for(SKProduct * product in allProducts) {
if([product.productIdentifier isEqualToString:productID])
{
[numberFormatter setLocale:product.priceLocale];
NSString *priceString= [numberFormatter stringFromNumber:product.price];
NSLog(@"Product with ID: %@ has price: %@",product.productIdentifier, priceString);
return priceString;
}
}
}
return nil;
}
答案 1 :(得分:1)
获取产品:
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"xxxxx"]];
如果它会成功,将会调用以下代表:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
if (response.products == nil) {
NSLog(@"No product belonging to the app!");
}
if ([response.invalidProductIdentifiers count] != 0) {
NSLog(@"The following product indentifiers are invalid: %@",response.invalidProductIdentifiers);
}
for (SKProduct* prod in response.products) {
NSLog(@"Proce: %@",prod.price);
}
我希望这对你有用。
重要提示:代码未经测试,我现在就写了......
祝你好运!