iOS开发swift -- APP内购的实现

SwiftyStoreKit简介

实现内购之前要在iTunes Connect上创建项目,本文是用SwiftyStoreKit一个轻量级的应用程序购买框架去实现内购功能。

一、 iTunes Connect上创建购买项目

iOS开发swift -- APP内购的实现_第1张图片
步骤一

这里选择消耗型项目
iOS开发swift -- APP内购的实现_第2张图片
步骤二

其余内容自己填写就可以了
iOS开发swift -- APP内购的实现_第3张图片
步骤三

二、CocoaPods框架导入

use_frameworks!
pod 'SwiftyStoreKit'

三、代码示例

//程序启动时在AppDelegate添加观察者,监测购买结果状态,并根据需求作出相应处理。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // see notes below for the meaning of Atomic / Non-Atomic
    SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
        for purchase in purchases {
            switch purchase.transaction.transactionState {
            case .purchased, .restored:
                if purchase.needsFinishTransaction {
                    // Deliver content from server, then:
                    SwiftyStoreKit.finishTransaction(purchase.transaction)
                }
                // Unlock content
            case .failed, .purchasing, .deferred:
                break // do nothing
            }
        }
    }
    return true
}


//获取商品信息
SwiftyStoreKit.retrieveProductsInfo(["productId"]) { result in
    if let product = result.retrievedProducts.first {
        //返回的retrievedProducts数组Set
        let priceString = product.localizedPrice!
        print("Product: \(product.localizedDescription), price: \(priceString)")
    }
    else if let invalidProductId = result.invalidProductIDs.first {
        print("Invalid product identifier: \(invalidProductId)")
    }
    else {
        print("Error: \(result.error)")
    }
}

//通过product id 购买商品 
SwiftyStoreKit.purchaseProduct("productId", quantity: 1, atomically: false) { result in
    switch result {
    case .success(let product):
        //atomically true 表示走服务器获取最后支付结果
        // fetch content from your server, then:
        if product.needsFinishTransaction {
            SwiftyStoreKit.finishTransaction(product.transaction)
        }
        print("Purchase Success: \(product.productId)")
    case .error(let error):
        switch error.code {
        case .unknown: print("Unknown error. Please contact support")
        case .clientInvalid: print("Not allowed to make the payment")
        case .paymentCancelled: break
        case .paymentInvalid: print("The purchase identifier was invalid")
        case .paymentNotAllowed: print("The device is not allowed to make the payment")
        case .storeProductNotAvailable: print("The product is not available in the current storefront")
        case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
        case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
        case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
        }
    }
}


//通过product  购买商品 
SwiftyStoreKit.retrieveProductsInfo(["com.musevisions.SwiftyStoreKit.Purchase1"]) { result in
    if let product = result.retrievedProducts.first {
        SwiftyStoreKit.purchaseProduct(product, quantity: 1, atomically: true) { result in
            // handle result (same as above)
        }
    }
}

四、沙箱账号测试购买

没成功上架之前,在iTunes Connect上用户与职能中创建沙箱账号,便于测试。


iOS开发swift -- APP内购的实现_第4张图片
创建沙箱账号

你可能感兴趣的:(iOS开发swift -- APP内购的实现)