SimpleStoreUI
这是一个基于表格视图的控制器,用于在应用程序中快速显示应用内购买项目和功能。我创建了这样一个基于 UI 的库,以便在我的所有应用程序中快速集成应用程序内购买。
SwiftStoreKit 基于StoreKit API执行重负载任务。我的基于 UI 的库只是利用了其中的一些函数和一个快速且简单的 UI 来加速集成。
功能
- 以表格样式在应用程序内显示应用内购买项目
- 每个项目都有图像、标题和价格
- 当前 UI 仅支持非消耗性项目。
- 基于 SwiftStoreKit 库
实现示例
步骤 1:在 AppDelegate.swift 中添加以下代码
let iAP_RemoveAd = "net.yourcompany.product"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
loadStore()
return true
}
func loadStore(){
StoreManager.shared.completeTransactionAtAppLaunch()
StoreManager.shared.shouldAddStorePaymentHandling(true)
StoreManager.shared.storeItems = [
Product(name: "Restore", productId: StoreManager.RestoreAll, price: "", purchased: false, image: "iap_restore"),
Product(name: "Remove Ads", productId: iAP_RemoveAd, price: "", purchased: false, image: "iap_remove_ads"),
]
StoreManager.shared.loadStoreProducts()
}
- 在此示例中,我们定义了一个名为iAP_RemoveAd的应用内购买项目。如果您有多个应用内购买项目,可以在这里或在其他地方定义。
- 将StoreManager.shared.storeItems填充您的应用内购买项目。如果您不想显示恢复功能,可以删除第一行。价格参数也可以选择填充或不填充,因为它稍后将被_store_manager自动填充。
- purchased: false是默认值。当商店加载时,StoreManager将检查并更新价格和已购买字段。
步骤 2:在您的main view controller中编写以下代码
import UIKit
import MBProgressHUD
import SimpleStoreUI
extension UIViewController {
//MARK: - Top View Controller
static func topViewController()-> UIViewController{
var topViewController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while ((topViewController.presentedViewController) != nil) {
topViewController = topViewController.presentedViewController!;
}
return topViewController
}
}
class ViewController: UIViewController {
@IBAction func showStore(_ sender: Any) {
//Main code to add the Store UI within app
let bundle = Bundle(for: StoreViewController.self)
let storeVC = StoreViewController(nibName: "StoreViewController", bundle: bundle)
StoreManager.shared.storeManagerDelegate = self
storeVC.title = "Store"
self.navigationController?.pushViewController(storeVC, animated: true)
}
}
extension ViewController : StoreManagerDelegate {
//MARK:- HudDelegate
public func showHud() {
print("Show HUD")
MBProgressHUD.showAdded(to: UIViewController.topViewController().view, animated: true)
}
public func hideHud() {
MBProgressHUD.hide(for: UIViewController.topViewController().view, animated: true)
}
public func purchaseSuccess(productId: String) {
if productId == iAP_RemoveAd {
// TODO: DO SOMETHING
print("Remove Ads Purchase Success. Do Something!")
}
}
}
- showStore() 函数演示了如何显示UI。
- StoreManagerDelegate有3个函数。您可以修改所有这些函数。
- 每次用户购买任何非消耗性项目或恢复任何这些项目时,都会调用带有productId的purchaseSuccess()。您需要根据productId定义如何向用户提供功能。
步骤 3:要了解用户是否购买了任何项目,请使用以下函数
StoreManager.shared.isProductPurchased(Constants.kIAPRemoveAds)
在iOS应用程序中的应用
要么
- 将SimpleStore/Sources文件夹拖到您的Xcode项目中。
或者
- 使用CocoaPods或Swift包管理器将SimpleStore作为项目依赖项包含在项目中。
pod 'SimpleStoreUI'
或者
pod 'SimpleStoreUI', :git => 'https://github.com/mahmudahsan/SimpleStoreUI'
- 在项目中添加您的应用内购买项目的图标。最好使用StoreIcons.assets添加图标。您将在这个演示文件夹中找到StoreIcons.assets
问题或反馈?
请随意提交问题,或者在我的@mahmudahsan on Twitter上找到我。