How to access the price of a product in SKPayment?

前端 未结 5 1632
無奈伤痛
無奈伤痛 2020-12-23 02:41

I am having an In-App-Purchase for an iPhone app.

I want to display the price in the users local currency in a UILabel. For this I need the price & currency in a

相关标签:
5条回答
  • 2020-12-23 02:56

    Here is the solution for Swift 3.0

    extension SKProduct {
      func priceAsString() -> String {
        let formatter = NumberFormatter()
        formatter.formatterBehavior = .behavior10_4
        formatter.numberStyle = .currency
        formatter.locale = self.priceLocale
        return formatter.string(from: self.price)! as String
      }
    }
    
    0 讨论(0)
  • 2020-12-23 02:58

    Here's the Swift version of the above answer using an extension:

    extension SKProduct {
      func priceAsString() -> String {
        let formatter = NumberFormatter()
        formatter.formatterBehavior = .behavior10_4
        formatter.numberStyle = .currency
        formatter.locale = self.priceLocale
        return formatter.string(from: self.price)! as String
      }
    }
    
    0 讨论(0)
  • 2020-12-23 03:08

    There's a problem with just using NSLocaleCurrencySymbol + price.stringValue: it doesn't handle the peculiarities of different locales, eg. whether they put the currency symbol in front or not. Norway, Denmark, Sweden and Switzerland all put their currency after, eg. 17.00Kr. Also, most(?) European countries use ',' instead of '.' for decimals, eg. "2,99 €" rather than "€2.99".

    A better plan is to use NSNumberFormatter. The "priceLocale" that the SKProduct returned, as Ed demonstrated, is the key. It gives NSNumberFormatter the smarts to format a price correctly.

    You can also make this a lot easier by adding a new property to SKProduct using an Objective-C category. Add the following two files to your project:


    SKProduct+priceAsString.h:

    #import <Foundation/Foundation.h>
    #import <StoreKit/StoreKit.h>
    
    @interface SKProduct (priceAsString)
    @property (nonatomic, readonly) NSString *priceAsString;
    @end
    

    SKProduct+priceAsString.m:

    #import "SKProduct+priceAsString.h"
    
    @implementation SKProduct (priceAsString)
    
    - (NSString *) priceAsString
    {
      NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
      [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
      [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
      [formatter setLocale:[self priceLocale]];
    
      NSString *str = [formatter stringFromNumber:[self price]];
      [formatter release];
      return str;
    }
    
    @end
    

    Then, #import "SKProduct+priceAsString.h" in your code, and you should just be able to use product.priceAsString in code.

    0 讨论(0)
  • 2020-12-23 03:13

    The correct way to determine any of that information is to use an SKProduct object, retrieved from the SKProductResponse object returned to the delegate after a call to - (void) start on an initialized SKProductsRequest. Something like this:

    SKProductsRequest *req = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"Identifier"]];
    req.delegate = self;
    [req start];
    
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response {
        [request autorelease];
        if (response.products.count) {
            SKProduct *product = [response.products objectAtIndex:0];
            NSLocale *priceLocale = product.priceLocale;
            NSDecimalNumber *price = product.price;
            NSString *description = product.localizedDescription;
        }
    }
    
    0 讨论(0)
  • 2020-12-23 03:17
    • Works in Swift and Objective-C
    • Force unwrapping is never your friend.
    • Using computed properties creates cleaner code for Objective-C scenario because it doesn't need [] ;)

      @objc public extension SKProduct {

      var priceAsString: String? {
      
          let formatter = NumberFormatter()
          formatter.formatterBehavior = .behavior10_4
          formatter.numberStyle = .currency
          formatter.locale = self.priceLocale
          formatter.string(from: self.price)
      
          return formatter.string(from: self.price)
      }
      

      }

    NOTE : Don't forget import StoreKit

    0 讨论(0)
提交回复
热议问题