0

我有两个视图控制器。其中之一显示了可用的应用内购买。当用户选择其中一个时,它会显示一个新的视图控制器,其中包含购买的详细信息和“购买”按钮。

我将代码从第一个视图传递到第二个视图的方式如下:

-(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 上向用户展示价格。有人可以帮我吗?谢谢你。

4

2 回答 2

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;
}
于 2014-01-11T15:08:45.130 回答
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);
    }

我希望这对你有帮助。

重要提示:代码未经测试,我现在才写...

祝你好运!

于 2014-01-11T15:11:09.903 回答