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

SwiftyStoreKit简介

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

一、 iTunes Connect上创建购买项目

步骤一

这里选择消耗型项目
步骤二

其余内容自己填写就可以了
步骤三

二、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<SKProduct>
        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上用户与职能中创建沙箱账号,便于测试。


创建沙箱账号
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前述:最近刚刚和后台的同事完成了IOS的内购项目功能开发,用以替换之前的支付宝、微信支付功能。这里,梳理出大体的步...
    silencerZiBo阅读 17,776评论 25 25
  • 自己开发的视频直播项目,牵涉到充值金币,用到了苹果公司的内购,趴坑了两天,这里总结下实现苹果内购。 一. 创建测试...
    Leo丶Dicaprio阅读 3,493评论 8 7
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,980评论 1 180
  • 天气: 情 心情指数:早晨是四晚上是8现在10 小确幸: 每日金句☞【觉察生活的点滴】 Objective 你对今...
    学霸教练李斌阅读 260评论 0 0
  • 最近业余有点忙,也有点乱,而且忙得还不知道到交作业时候应该焦点在哪里,我把它称为瞎忙。 三件事,努力也是焦虑...
    爱笑粥粥阅读 327评论 8 3

友情链接更多精彩内容