自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS應(yīng)用內(nèi)置付費(fèi):In-App Purchases完全攻略

移動開發(fā) iOS
實(shí)現(xiàn)IAP功能比想象的要復(fù)雜許多。我可是經(jīng)過無數(shù)痛苦的經(jīng)歷才完成我的程序。希望能夠幫助其他開發(fā)者減輕他們的痛苦。第一印象覺得In-App Purchase(簡稱IAP)非常簡單。Apple提供的大量文檔應(yīng)該讓開發(fā)者很快熟悉地熟悉。為什麼在你的應(yīng)用中集成IAP特性就如此令人生厭呢?

參考:iOS應(yīng)用內(nèi)置付費(fèi)In-App Purchase 個人使用總結(jié)

 

新:In-App Purchases入門    demo下載:http://down.51cto.com/data/941246

第一印象覺得In-App Purchase(簡稱IAP)非常簡單。Apple提供的大量文檔應(yīng)該讓開發(fā)者很快熟悉地熟悉。那麼,為什麼在你的應(yīng)用中集成IAP特性就如此令人生厭呢?

這是因?yàn)樵陂_發(fā)過程中不可避免會出現(xiàn)一些錯誤。而但這些錯誤發(fā)生的時候,你就抓瞎了。雖然Apple提供了有關(guān)IAP的大量文檔,但他們并未提及集成IAP 的詳細(xì)步驟。而且對StoreKit集成過程中出現(xiàn)的問題也沒有一個核對清單。另外對于為什麼諸如產(chǎn)品ID非法之類的問題也沒有提供NSError之類的 對象來告訴你原因。

在試用了各種可能的解決方桉后,你只能身心疲憊,彷徨無助。

為了提高你的效率和減少你的痛苦,我覺定利用此文來介紹一下實(shí)現(xiàn)IAP的詳細(xì)步驟。本文很詳細(xì),有點(diǎn)長。甚至可能太長了,但不像Apple的文檔,它提供了為實(shí)現(xiàn)IAP的每一個步驟。

廢話少說,我們直入主題吧。

概況

IAP能正常工作的秘訣:分成兩個步驟:

創(chuàng)建及提取產(chǎn)品描述

購買產(chǎn)品

第一個步驟是你可能遇到問題的部分。一旦你在代碼中成功地獲取了產(chǎn)品描述,編寫購買產(chǎn)品的代碼不過是小菜一碟。

我們先看看步驟1。

創(chuàng)建及提取產(chǎn)品描述

下面是有關(guān)創(chuàng)建產(chǎn)品及提取其描述的非常粗略的步驟:

創(chuàng)建唯一的App ID

生成及安裝新的provisioning profile文件

在Xcode中更新 bundle ID 及 code signing profile

如果還沒做的話,請?jiān)趇Tunes Connect中提交有關(guān)你程序的 metadata

如果還沒做的話,請?jiān)趇Tunes Connect中提交你程序的二進(jìn)制碼

為IAP添加新產(chǎn)品

編寫提取產(chǎn)品描述的代碼

等待幾小時

提取產(chǎn)品描述的代碼非常簡單,但其他步驟則很容易錯。

注意:為提取產(chǎn)品描述,你并不需要在iTunes Connect中創(chuàng)建IAP測試用戶。

1. 創(chuàng)建唯一的App ID

為支持IAP,你的App ID不能包括通配符(“*”)。為確定你的App Id是否包括通配符,請登錄http://developer.apple.com/iphone,在 iPhone Developer Program Portal中選擇左邊菜單中的 “App IDs”檢查你的 App ID。

下面是一個唯一的App ID:

7DW89RZKLY.com.runmonster.runmonsterfree

下面不是一個唯一的 App ID:

7DW89RZKLY.com.runmonster.*

如果你還沒有一個唯一的App ID,按如下步驟創(chuàng)建一個:

在developer portal中的 App IDs 部分,選擇“New App ID”

填寫下列信息:

Display name(顯示名): 選取一個不同的App ID的名稱。你不能編輯或刪除舊的App ID,所以你必須為你的App ID提供一個新名稱以避免溷淆。

Prefix(前綴): 生成一個新的前綴,或者如果你的程序是通過Keychain Services API分享數(shù)據(jù)的系列程序中之一的話,則選用已存在的前綴。

Suffix(后綴): com.companyname.appname (這是通用格式 – 注意沒有使用通配符)。

按 “Save”

按 App ID旁的“Configure” 鏈接

選取 “Enable In App Purchase”選擇框

按“Done”

2. 創(chuàng)建一個新的Provisioning Profile文件

在創(chuàng)建了新的App ID后,你需要生成一個指向這個App ID的新provisioning profile。

下面就是令人痛苦的生成和安裝新provisioning profile的詳細(xì)步驟:

在 iPhone Developer Portal中, 選擇左邊的Provisioning部分

確保你處于Development 標(biāo)籤下, 按下右上角的 “New Profile”

填入所需信息并指向你剛創(chuàng)建的唯一的App ID

如果你在Actions條目下看到 “Pending”,那麼請按下“Development”標(biāo)籤標(biāo)題進(jìn)行刷新

點(diǎn)擊 “Download” 下載新的profile文件

將profile文件拖入到Dock中Xcode圖標(biāo)上進(jìn)行安裝

如果你想在硬盤上保存provisioning profile,那麼你可以按如下步驟手工安裝profile:

在Xcode中, 選擇 Window > Organizer

選擇左邊 “Provisioning Profiles” 分類

Ctrl-按下profile > Reveal in Finder

將新profile拖入到 profile Finder 窗口

3. 更新Xcode 設(shè)置

在Xcode中安裝了 profile 文件后,你需要對使用此provisiong profile的項(xiàng)目進(jìn)行一些編輯工作:

編 輯項(xiàng)目 .plist 文件使其 Bundle ID 與 App ID 匹配。忽略ID開始部分的字母數(shù)字序列。例如,在Developer Portal中你的App ID為“7DW89RZKLY.com.runmonster.runmonsterfree”,那麼在Bundle ID中你只需輸入“com.runmonster.runmonsterfree” 。

編輯項(xiàng)目的 target 信息以使用新的provisioning profile:

選取 Project > Edit Active Target

選取頂部“Build” 標(biāo)籤

選取需要的 configuration (通常為 Debug)

在Code Signing Identity中選擇新的provisioning profile

在Code Signing Identity之下的行中(可能名為 Any iPhone OS Device)選擇新的provisioning profile

4. 添加你的應(yīng)用程序

如果你的程序已經(jīng)發(fā)表到App Store了,那麼可以略過此步驟。

在你將產(chǎn)品添加到 iTunes Connect之前,你必須添加此產(chǎn)品所需的程序。如果你的程序還沒有100%完成也無需擔(dān)心,你可以先提交具有部分?jǐn)?shù)據(jù)的程序,最后再提交真實(shí)的程序。

注意: 只有 SKU 和 version(版本)部分是以后不可修改的

登錄到 http://developer.apple.com/iphone

點(diǎn)擊右邊鏈接進(jìn)入 iTunes Connect

注意:你必須先登錄到developer.apple.com,否則會有不測發(fā)生(譯者注:具體是什麼不測我也不太清楚,膽大的請自己試一下)

在 iTunes Connect主頁點(diǎn)擊 “Manage Your Applications”

在右上角點(diǎn)擊“Create New Application”

填寫程序所需的一切信息。當(dāng)要求程序二進(jìn)制碼時,請選擇稍后上傳選項(xiàng)。

5. 提交程序二進(jìn)制碼

Apple的文檔中沒有任何地方提及詳情,但它卻是必須的步驟。要成功測IAP功能,你必須提交程序的二進(jìn)制碼。即使你的程序還沒有100%完成,你仍然需要提交二進(jìn)制碼。然而,你也可以立即擯棄你的二進(jìn)制碼,使其不會進(jìn)入審核階段。

下面這些步驟非常關(guān)鍵,我可是因?yàn)樯僮隽四承┎襟E而度過了一段非常痛苦的時間:

生成App Store發(fā)佈版程序

如果你不知怎麼做,請?jiān)?iPhone Developer Portal 中點(diǎn)擊左方的 Distribution標(biāo)籤,并選擇 “Prepare App” 標(biāo)籤。然后,根據(jù)藍(lán)色鏈接的指示:

獲取iPhone發(fā)行許可證

創(chuàng)建并下載在App Store發(fā)行所需的iPhone Distribution Provisioning Profile

在Xcode中生成程序的發(fā)行版

在iTunes Connect中進(jìn)入程序頁

選擇 “Upload Binary”

上傳.zip壓縮程序

如果你的程序還沒有100%完成以進(jìn)行審核,那麼請點(diǎn)擊iTunes Connect中你程序首頁中的 “Reject Binary”鏈接。程序的狀態(tài)應(yīng)該更新為 “Developer Rejected”.

不用擔(dān)心,由于程序的狀態(tài)是“Developer Rejected”,Apple是不會對其進(jìn)行審核的。你可以在任何時候提交程序的新版本并使其狀態(tài)為“Developer Rejected”,這不會對以后程序正式提交的等待時間有任何影響。

6. 添加產(chǎn)品

完成了以上所有步驟后,我們最終可以向iTunes Connect中添加產(chǎn)品了。

確保登錄到 http://developer.apple.com/iphone

進(jìn)入 iTunes Connect 主頁

點(diǎn)擊“Manage Your Applications”

點(diǎn)擊剛建好的程序 點(diǎn)擊view details

點(diǎn)擊 “Manage in-App Purchases” 鏈接

點(diǎn)擊 “Create New”

填寫下列產(chǎn)品信息:

Reference Name(參考名稱): 產(chǎn)品的通用名稱。比如,我使用的是 “Pro Upgrade”。此名稱是不允許進(jìn)行編輯的,它不會顯示于App Store中。

Product ID(產(chǎn)品ID): 你產(chǎn)品的唯一id。通常格式是 com.company.appname.product,但它可以說任何形式。它并不要求以程序的App ID作為前綴。

Type(類型): 有三種選擇

Non-consumable(非消耗品): 僅需付費(fèi)一次 (例如你希望將出現(xiàn)從免費(fèi)版升級為專業(yè)版)

Consumable(消耗品): 每次下載都需要付費(fèi)

Subscription(預(yù)訂): 循環(huán)反覆

Price Tier(價格等級): 產(chǎn)品價格。參見不同等級的價格列表。

Cleared for Sale(等待銷售): 一定要選取此項(xiàng),否則的話,測試時會發(fā)生非法產(chǎn)品ID的錯誤。

Language to Add(增加的語言): 選一項(xiàng)。下列兩項(xiàng)將出現(xiàn):

Displayed Name(顯示名稱): 用戶看到的產(chǎn)品名稱。比如我選擇 “Upgrade to Pro”。

Description(描述): 對產(chǎn)品進(jìn)行描述。此處輸入的文本將與Displayed Name 及 Price 一起在你代碼中提取 SKProduct時出現(xiàn)。

Screenshot(截 屏): 展示你產(chǎn)品的截屏。儘管屏幕上會顯示“提交截屏?xí)|發(fā)產(chǎn)品審核過程”之類的文字(個人拙見,這是非常糟糕的設(shè)計(jì)),你還是可以安全地提交截屏而不會使產(chǎn)品 進(jìn)入審核過程。存儲后,選擇“Submit with app binary” (隨程序二進(jìn)制碼一起提交)選項(xiàng)。是產(chǎn)品與程序二進(jìn)制綁定在一起,所以在你最后正式提交100%完成的程序二進(jìn)制碼時,產(chǎn)品也會隨之提交。

點(diǎn)擊 “Save”

最后一定不要忘了回到view details  編輯In-App Purchases

選擇剛剛添加的iap版本

別走開,下頁內(nèi)容更精彩

#p#

7. 編寫代碼

下面我們開始編寫代碼對剛加入到iTunes Connect中的產(chǎn)品信息進(jìn)行提取。我訪問產(chǎn)品數(shù)據(jù),我們需要使用 StoreKit framework。

注意: StoreKit 無法在模擬器上工作。你必須在真機(jī)上進(jìn)行測試。

1)添加 StoreKit framework 到你的項(xiàng)目中。

2)添加SKProduct引用到你的 .h 文件中:

  1. // InAppPurchaseManager.h 
  2. #import <StoreKit/StoreKit.h> 
  3. #define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification" 
  4. @interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate> 
  5.     SKProduct *proUpgradeProduct; 
  6.     SKProductsRequest *productsRequest; 

注意: InAppPurchaseManager 是一個單例類,它處理程序中所有IAP任務(wù)。它是本文中的示例程序。

3)產(chǎn)品請求,并在相應(yīng).m文件中實(shí)現(xiàn)代理協(xié)議:

  1. // InAppPurchaseManager.m 
  2. - (void)requestProUpgradeProductData 
  3.     NSSet *productIdentifiers = [NSSet setWithObject:@"com.runmonster.runmonsterfree.upgradetopro" ]; 
  4.     productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; 
  5.     productsRequest.delegate = self; 
  6.     [productsRequest start]; 
  7.      
  8.     // we will release the request object in the delegate callback 
  9. #pragma mark - 
  10. #pragma mark SKProductsRequestDelegate methods 
  11. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response 
  12.     NSArray *products = response.products; 
  13.     proUpgradeProduct = [products count] == 1 ? [[products firstObject] retain] : nil; 
  14.     if (proUpgradeProduct) 
  15.     { 
  16.         NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle); 
  17.         NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription); 
  18.         NSLog(@"Product price: %@" , proUpgradeProduct.price); 
  19.         NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier); 
  20.     } 
  21.     for (NSString *invalidProductId in response.invalidProductIdentifiers) 
  22.     { 
  23.         NSLog(@"Invalid product id: %@" , invalidProductId); 
  24.     } 
  25.     // finally release the reqest we alloc/init’ed in requestProUpgradeProductData 
  26.     [productsRequest release]; 
  27.     [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil]; 

上面代碼有幾點(diǎn)需要注意:

指定產(chǎn)品id時,你必須使用完整產(chǎn)品id。例如,上例中使用 “com.runmonster.runmonsterfree.upgradetopro”。僅使用 “upgradetopro” 將不會正常工作。

如果在productsRequest:didReceiveResponse:中response.products 為 nil,而你的產(chǎn)品id出現(xiàn)于 response.invalidProductIdentifers 數(shù)組中時,那麼請做好心理準(zhǔn)備開始一場徒勞的搜索戰(zhàn)吧。 StoreKit API沒有提供任何幫助,也沒有任何指示關(guān)于為什麼你的id是無效的。很可愛,不是嗎?

SKProduct類提供了有關(guān)程序標(biāo)題和描述的本地化版本,但是價格則沒有本地化版本。下面是針對此疏忽提供的代碼:

  1. // SKProduct+LocalizedPrice.h 
  2. #import <Foundation/Foundation.h> 
  3. #import <StoreKit/StoreKit.h> 
  4. @interface SKProduct (LocalizedPrice) 
  5. @property (nonatomic, readonly) NSString *localizedPrice; 
  6. @end
  1. // SKProduct+LocalizedPrice.m 
  2. #import "SKProduct+LocalizedPrice.h" 
  3. @implementation SKProduct (LocalizedPrice) 
  4. - (NSString *)localizedPrice 
  5.     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
  6.     [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
  7.     [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; 
  8.     [numberFormatter setLocale:self.priceLocale]; 
  9.     NSString *formattedString = [numberFormatter stringFromNumber:self.price]; 
  10.     [numberFormatter release]; 
  11.     return formattedString; 
  12. @end 

加入上述代碼,測試一下。你應(yīng)該在控制臺窗口中看見產(chǎn)品信息了。然而更大的可能是,你得到了一個無效的產(chǎn)品id。我下一篇文章將介紹怎樣對這個問題進(jìn)行調(diào)試。但是,下面的步驟8有可能是阻礙你前進(jìn)的障礙。

8. 等待幾小時

遵循了上述所有步驟,但是你的產(chǎn)品仍然是無效的?你是否兩次,三次,四次不懈努力地確認(rèn)你是否遵循了上面提到的每個步驟?你是否已經(jīng)對網(wǎng)上IAP信息少得可憐而感到絕望?那麼,你應(yīng)該等待。你的產(chǎn)品要進(jìn)入iTunes Connect使得Apple準(zhǔn)備好沙箱環(huán)境需要一些時間。對于我而言,我是經(jīng)過了無數(shù)次產(chǎn)品無效錯誤的絕望。而在24小時后,我沒有修改任何一行代碼, 但產(chǎn)品id變?yōu)橛行?。我認(rèn)為要使產(chǎn)品發(fā)佈到Apple的網(wǎng)絡(luò)系統(tǒng)需要幾個小時的時間,但如果你有時間的話,你可以像我一樣等上24個小時。

購買產(chǎn)品

至此你應(yīng)該已經(jīng)成功地獲取了 SKProduct 描述。比較而言,支持購買產(chǎn)品相對簡單些。僅需下面三個步驟:

編寫代碼支持事務(wù)(transaction)

在iTunes Connect中添加程序測試用戶

在設(shè)備中登錄你的 iTunes Store 帳號

購買測試

我們從編寫支持事務(wù)所需代碼開始。

1)編寫代碼支持事務(wù)

首先注意:你將負(fù)責(zé)開發(fā)產(chǎn)品購買的用戶界面。StoreKit 未提供任何與用戶界面相關(guān)的元素。如果你希望你的購買用戶界面與App Store一樣,那麼你要自己完成。

下面所有代碼都是有關(guān)事務(wù)處理的后臺部分。這是一個單獨(dú)的類只有一條簡單的API以供外部類(比如view controller)調(diào)用進(jìn)行購買。如果你找到將其集成到你程序的購買部分的方法,那麼我推薦你使用類似方桉。

首先,需要遵循 SKPaymentTransactionObserver 協(xié)議:

  1. // InAppPurchaseManager.h 
  2. // add a couple notifications sent out when the transaction completes 
  3. #define kInAppPurchaseManagerTransactionFailedNotification @"kInAppPurchaseManagerTransactionFailedNotification" 
  4. #define kInAppPurchaseManagerTransactionSucceededNotification @"kInAppPurchaseManagerTransactionSucceededNotification" 
  5. … 
  6. @interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver> 
  7.     … 
  8. // public methods 
  9. - (void)loadStore; 
  10. - (BOOL)canMakePurchases; 
  11. - (void)purchaseProUpgrade; 
  12. @end 

上面我們定義了兩個新的notification,它們將作為購買事務(wù)的結(jié)果被發(fā)送。在上例中我們?nèi)匀皇褂门c獲取產(chǎn)品描述同一個InAppPurchaseManager類。

  1. // InAppPurchaseManager.m 
  2. #define kInAppPurchaseProUpgradeProductId @"com.runmonster.runmonsterfree.upgradetopro" 
  3. … 
  4. #pragma - 
  5. #pragma Public methods 
  6. // 
  7. // call this method once on startup 
  8. // 
  9. - (void)loadStore 
  10.     // restarts any purchases if they were interrupted last time the app was open 
  11.     [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
  12.      
  13.     // get the product description (defined in early sections) 
  14.     [self requestProUpgradeProductData]; 
  15. // 
  16. // call this before making a purchase 
  17. // 
  18. - (BOOL)canMakePurchases 
  19.     return [SKPaymentQueue canMakePayments]; 
  20. // 
  21. // kick off the upgrade transaction 
  22. // 
  23. - (void)purchaseProUpgrade 
  24.     SKPayment *payment = [SKPayment paymentWithProductIdentifier:kInAppPurchaseProUpgradeProductId]; 
  25.     [[SKPaymentQueue defaultQueue] addPayment:payment]; 
  26. #pragma - 
  27. #pragma Purchase helpers 
  28. // 
  29. // saves a record of the transaction by storing the receipt to disk 
  30. // 
  31. - (void)recordTransaction:(SKPaymentTransaction *)transaction 
  32.     if ([transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId]) 
  33.     { 
  34.         // save the transaction receipt to disk 
  35.         [[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ]; 
  36.         [[NSUserDefaults standardUserDefaults] synchronize]; 
  37.     } 
  38. // 
  39. // enable pro features 
  40. // 
  41. - (void)provideContent:(NSString *)productId 
  42.     if ([productId isEqualToString:kInAppPurchaseProUpgradeProductId]) 
  43.     { 
  44.         // enable the pro features 
  45.         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isProUpgradePurchased" ]; 
  46.         [[NSUserDefaults standardUserDefaults] synchronize]; 
  47.     } 
  48. // 
  49. // removes the transaction from the queue and posts a notification with the transaction result 
  50. // 
  51. - (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful 
  52.     // remove the transaction from the payment queue. 
  53.     [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
  54.      
  55.     NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil]; 
  56.     if (wasSuccessful) 
  57.     { 
  58.         // send out a notification that we’ve finished the transaction 
  59.         [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo]; 
  60.     } 
  61.     else 
  62.     { 
  63.         // send out a notification for the failed transaction 
  64.         [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo]; 
  65.     } 
  66. // 
  67. // called when the transaction was successful 
  68. // 
  69. - (void)completeTransaction:(SKPaymentTransaction *)transaction 
  70.     [self recordTransaction:transaction]; 
  71.     [self provideContent:transaction.payment.productIdentifier]; 
  72.     [self finishTransaction:transaction wasSuccessful:YES]; 
  73. // 
  74. // called when a transaction has been restored and and successfully completed 
  75. // 
  76. - (void)restoreTransaction:(SKPaymentTransaction *)transaction 
  77.     [self recordTransaction:transaction.originalTransaction]; 
  78.     [self provideContent:transaction.originalTransaction.payment.productIdentifier]; 
  79.     [self finishTransaction:transaction wasSuccessful:YES]; 
  80. // 
  81. // called when a transaction has failed 
  82. // 
  83. - (void)failedTransaction:(SKPaymentTransaction *)transaction 
  84.     if (transaction.error.code != SKErrorPaymentCancelled) 
  85.     { 
  86.         // error! 
  87.         [self finishTransaction:transaction wasSuccessful:NO]; 
  88.     } 
  89.     else 
  90.     { 
  91.         // this is fine, the user just cancelled, so don’t notify 
  92.         [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
  93.     } 
  94. #pragma mark - 
  95. #pragma mark SKPaymentTransactionObserver methods 
  96. // 
  97. // called when the transaction status is updated 
  98. // 
  99. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
  100.     for (SKPaymentTransaction *transaction in transactions) 
  101.     { 
  102.         switch (transaction.transactionState) 
  103.         { 
  104.             case SKPaymentTransactionStatePurchased: 
  105.                 [self completeTransaction:transaction]; 
  106.                 break
  107.             case SKPaymentTransactionStateFailed: 
  108.                 [self failedTransaction:transaction]; 
  109.                 break
  110.             case SKPaymentTransactionStateRestored: 
  111.                 [self restoreTransaction:transaction]; 
  112.                 break
  113.             default
  114.                 break
  115.         } 
  116.     } 

要測試上面的新代碼,你還需要編寫調(diào)用 loadStore, canMakePurchases 以及 purchaseProUpgrade 方法的代碼。

有關(guān)上述代碼的詳細(xì)解釋,請參考官方 In App Purchase Programming Guide (IAP編程指南)

上述代碼有幾個部分是針對我的程序的。例如,在 provideContent:中,NSUserDefaults 中的@”isProUpgradePurchased” BOOL 字段被設(shè)定為 YES。程序的其他部分將檢查此BOOL值以確定是否需要啟動專業(yè)版功能。如果你正好也要實(shí)現(xiàn)免費(fèi)升級專業(yè)版的功能,那麼你可以使用同樣的方法。

2)添加測試用戶

為測試上述代碼,你需要在 iTunes Connect 中創(chuàng)建測試用戶以對IAP功能進(jìn)行測試。你可以使用測試帳號購買產(chǎn)品而不被Apple收取費(fèi)用。

按以下步驟創(chuàng)建測試用戶:

登錄到 http://developer.apple.com/iphone

進(jìn)入 iTunes Connect

選擇iTunes Connect首頁中的 “Manage Users”

選擇 “In App Purchase Test User”

選擇 “Add New User”

填入用戶信息. 所有信息都不必是合法的。建議使用虛假簡短的email地址及簡短的密碼。

選擇 “Save”

測試時你需要輸入這些email地址和密碼。

3)在你的設(shè)備中退出登錄

在進(jìn)行程序購買功能測試前,你必須在你的設(shè)備中退出iTunes Store。遵循以下步驟:

打開Settings App

點(diǎn)擊 “Store” 行

點(diǎn)擊 “Sign Out”

4)購買測試

現(xiàn)在,終于可以開始進(jìn)行IAP功能的測試了。測試很簡單:

運(yùn)行你設(shè)備中的程序

進(jìn)行購買

當(dāng)程序提示輸入用戶名和密碼時,輸入?yún)?shù)用戶的信息

如果你使用同一賬戶進(jìn)行購買時,系統(tǒng)將提示你已經(jīng)購買了此產(chǎn)品。按“Yes”就可以再次下載此產(chǎn)品。

總結(jié)

實(shí)現(xiàn)IAP功能比想象的要復(fù)雜許多。我可是經(jīng)過無數(shù)痛苦的經(jīng)歷才完成我的程序。希望能夠幫助其他開發(fā)者減輕他們的痛苦。

責(zé)任編輯:閆佳明 來源: cocoachina
相關(guān)推薦

2013-09-05 15:12:09

iOS應(yīng)用內(nèi)置付費(fèi)In-App Purc

2013-09-09 13:57:27

iOS應(yīng)用內(nèi)置付費(fèi)In-App Purc

2013-09-09 14:26:25

iOS應(yīng)用內(nèi)置付費(fèi)In-App Purc

2009-02-12 10:12:00

NAT配置

2009-02-10 09:47:00

應(yīng)用程序訪問權(quán)限

2014-12-19 12:57:57

APP推廣運(yùn)營

2013-05-22 10:00:30

iOSWeb Appicon

2013-09-09 16:11:16

iOS應(yīng)用內(nèi)置付費(fèi)IAP總結(jié)

2023-10-13 19:42:00

2010-07-12 09:42:06

FreeBSD 7.0nagios安裝

2011-03-28 10:12:44

FreeBSDnagios

2010-11-18 23:24:42

云遷移

2013-06-08 11:13:00

Android開發(fā)XML解析

2013-04-15 10:48:16

Xcode ARC詳解iOS ARC使用

2024-05-07 09:01:21

Queue 模塊Python線程安全隊(duì)列

2010-04-23 14:04:23

Oracle日期操作

2013-09-09 15:49:44

iOS應(yīng)用內(nèi)置付費(fèi)IAP設(shè)置總結(jié)

2023-11-06 08:14:51

Go語言Context

2011-05-17 10:19:40

EMC Celerra

2014-03-19 17:22:33

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號