在我的iOS应用程序中,我有3个产品,用户可以使用应用内购买购买。从我的iOS应用程序中,我只想获得苹果服务器上所有3个项目的价格。如何在没有用户干预的情况下获得价格。 (可能是我在视图中处理加载或某些方法)
您无需任何用户干预即可获取任何产品信息。您所要做的就是发送产品请求信息并实现一个回调方法,该方法将处理响应,如下所示(示例来自http://troybrant.net/blog/2010/01/in-app-purchases-a-full-walkthrough/):
- (void)requestProUpgradeProductData
{
NSSet *productIdentifiers = [NSSet setWithObject:@"com.runmonster.runmonsterfree.upgradetopro" ];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
// we will release the request object in the delegate callback
}
#pragma mark -
#pragma mark SKProductsRequestDelegate methods
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;
proUpgradeProduct = [products count] == 1 ? [[products firstObject] retain] : nil;
if (proUpgradeProduct)
{
NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
NSLog(@"Product price: %@" , proUpgradeProduct.price);
NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
}
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
NSLog(@"Invalid product id: %@" , invalidProductId);
}
// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}
只需从viewDidLoad调用requestProUpgradeProductData即可。
您可以随时使用已知标识符列表创建SKProductsRequest实例。我在应用程序启动时完成了它。它是一个使用委托的异步调用,因此很容易做到没有阻塞。
myProductsRequest = [SKProductsRequest initWithProductIdentifiers:someSetOfIds];
myProductsRequest.delegate = myDelegageSomewhere;
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
for(SKProduct *product in response.products)
{
[self doSomethingWithPrice:product.price]
}
}
该信息与用户的iTunes帐户相关联。这就是它如何获得不同国家商店(不同货币)的价格。
-(NSString*)getPrice:(SKProduct*)product{
NSNumberFormatter * _priceFormatter;
_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
return [_priceFormatter stringFromNumber:product.price];
}
Swift 3.x,你将在下面使用
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
for product in response.products {
let price = product.price
let currencySymbol = productpriceLocale.currencySymbol
}
}
您可以将此功能添加到您的班级。
func priceOf(product: SKProduct) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.formatterBehavior = .behavior10_4
numberFormatter.numberStyle = .currency
numberFormatter.locale = product.priceLocale
return numberFormatter.string(from: product.price)!
}
然后,你可以传递你的产品!
priceLabel.text = "The price of the item is \(priceOf(product: product))"
将product
替换为SKProduct的名称。
要获得应用内商品的价格,您可以使用我的服务器。 仅适用于App Store中的可用应用程序。 发送邮件请求到:
http://195.138.193.194:5003/store/
身体:
{
"bundleId": "com.example.bundle",
"products": [
"com.example.productId1",
"com.example.productId2",
]
}