接上一篇筆記
編程步驟之獲取并顯示產(chǎn)品信息
購物的第一步是讓用戶看到商品饿这,裝潢門面比什么都重要!顯示商品要盡量迅速(App Store的查詢速度普遍要2-3秒撞秋,要不要建立自己的服務(wù)器?)长捧,不能顯示缺貨的商品(看到心儀的產(chǎn)品,商家告訴你缺貨, 多么痛的領(lǐng)悟!)吻贿,Store UI要人性串结,價格顯示要到位...
Apple官網(wǎng)給出的流程圖如下:

第一步 在工程中引入storekit.framework并且在文件中
#import <StoreKit/StoreKit.h>
第二步 product identifier存在哪里?
可以保存在app bundle中
-
也可以保存在自己的服務(wù)器上
以下是Apple給出的存儲在本地的一個例子(product_ids.plist)。<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <string>com.example.level1</string> <string>com.example.level2</string> <string>com.example.rocket_car</string> </array> </plist>
如果使用自己的服務(wù)器, 可以傳輸JSON格式的文件, Apple同樣給出了相關(guān)的例子:
[
"com.example.level1",
"com.example.level2",
"com.example.rocket_car"
]
第三步 讀取本地product_ids.plist文件
NSURL *url = [[NSBundle mainBundle] URLForResource:@"product_ids" withExtension:@"plist"];
NSArray *productIdentifiers = [NSArray arrayWithContentsOfURL:url];
第四步 根據(jù)Product IDs從App Store獲取產(chǎn)品的信息
- (void)validateProductIdentifiers:(NSArray *)productIdentifiers{
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
productsRequest.delegate = self;
[productsRequest start];
}
// SKProductsRequestDelegate protocol method
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
self.products = response.products;
for (NSString *invalidIdentifier in response.invalidProductIdentifiers) {
// Handle any invalid product identifiers. 處理有效的ProductIdentifiers, 缺貨的,錯誤的不能有!
}
[self displayStoreUI]; // Custom method 顯示Store的UI
}
第五步 顯示購買界面UI
5.1 判斷用戶是否關(guān)閉了內(nèi)置付費肌割,如果關(guān)閉了卧蜓,就提示一下
if ([SKPaymentQueue canMakePayments]) {
[self displayStoreUI]; // Custom method
} else {
NSLog(@"用戶禁止應(yīng)用內(nèi)付費購買.");
}
5.2 購買界面UI上價格要顯示的清楚明了
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedPrice = [numberFormatter stringFromNumber:product.price];