Code Monkey home page Code Monkey logo

xyiapkit's Introduction

XYIAPKit

CI Status Version License Platform

iOS内购编程指南

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

XYIAPKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'XYIAPKit'

功能

1、内购的基本功能,产品列表查询、添加购买、恢复购买

2、票据校验、自动续费订阅过期检测

3、交易记录保存

使用方法

1、查询在线商品

/**
 请求在线商品,并存储于内存中

 @param identifiers 产品id
 */
- (void)requestProducts:(NSSet*)identifiers
                success:(void (^)(NSArray *products, NSArray *invalidProductIdentifiers))successBlock
                failure:(void (^)(NSError *error))failureBlock;

#########################################################################################################

	NSSet *set = [[NSSet alloc] initWithArray:@[@"1", @"2", @"3", @"4", @"5", @"6"]];
    [[XYStore defaultStore] requestProducts:set
                                    success:^(NSArray *products, NSArray *invalidProductIdentifiers)
     {

     } failure:^(NSError *error) {

     }];

2、添加购买

    [[XYStore defaultStore] addPayment:productId
                               success:^(SKPaymentTransaction *transaction)
    {

        
    } failure:^(SKPaymentTransaction *transaction, NSError *error) {

    }];

3、恢复内购

[[XYStore defaultStore] restoreTransactionsOnSuccess:^(NSArray *transactions) {
        
        NSLog(@"restore === %@", transactions);
    } failure:^(NSError *error) {
        NSLog(@"restore error === %@", error);
    }];

4、添加票据校验

1)、外部票据校验(可以通过信任的服务器进行校验)
创建校验对象(建议创建单例对象,以便监听),遵守XYStoreReceiptVerifier协议
- (void)verifyTransaction:(SKPaymentTransaction *)transaction
                  success:(void (^)(void))successBlock
                  failure:(void (^)(NSError *))failureBlock
{
    __weak typeof(self) weakSelf = self;
    [[XYStore defaultStore] base64Receipt:^(NSString *base64Data) {
        
        [[XYStore defaultStore] fetchProduct:transaction.payment.productIdentifier
                                     success:^(SKProduct *product)
         {
         	//外部校验请求
             [weakSelf callBackApple:product
                         receiptData:base64Data
                             success:successBlock
                             failure:failureBlock];
             
         } failure:failureBlock];
        
    } failure:failureBlock];
}
在`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions `中注册票据校验
	// 在此设置内购的票据校验,防止掉单问题的发生
    [[XYStore defaultStore] registerReceiptVerifier:[XYAppReceiptVerifier shareInstance]];
2)、APP内部校验

podfile中添加`pod 'XYIAPKit/iTunesReceiptVerify', '~> 0.8.0'`
引入票据校验的sdk

在`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions `中注册票据校验
	// 在此设置内购的票据校验,防止掉单问题的发生
    [[XYStore defaultStore] registerReceiptVerifier:[XYStoreiTunesReceiptVerifier shareInstance]];
XYStoreiTunesReceiptVerifier中提供对自动续期产品的订阅过期的判断
	/**
 @ 适用自动续期订阅
 
 判断是否已订阅
 若果是非自动续费型商品,直接返回NO

 @param productId 自动续期订阅产品id
 @return YES:  NO:未订阅或者订阅过期
 */
- (BOOL)isSubscribedWithAutoRenewProduct:(NSString *)productId;
也提供 针对持续有效的产品,Apple返回的票据记录中会一直保留其票据信息,可以通过票据记录判断是否有效
/**
 针对持续有效的产品,Apple返回的票据记录中会一直保留其票据信息,可以通过票据记录判断是否有效
 支持:1、非续期订阅 2、非消耗型项目
 
 注:消耗型项目一旦完成,不会长期保留在票据信息中
 
 @param productId 产品id
 @return YES:消费有效 NO:无效
 */
- (BOOL)isValidWithPersistentProductId:(NSString *)productId;

5、添加交易记录存储 1)、存储在NSUserDefaults podfile中添加pod 'XYIAPKit/UserDefaultPersistence', :'~> 0.8.0'

在`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions `中注册票据校验
	// 在此设置内购的票据校验,防止掉单问题的发生
    [[XYStore defaultStore] registerTransactionPersistor:[XYStoreUserDefaultsPersistence shareInstance]];

2)、存储于Keychain中
	podfile中添加`pod 'XYIAPKit/KeychainPersistence', '~> 0.8.0'`

在`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions `中注册票据校验
	// 在此设置内购的票据校验,防止掉单问题的发生
    [[XYStore defaultStore] registerTransactionPersistor:[XYStoreKeychainPersistence shareInstance]];

Author

1073605877, [email protected]

License

XYIAPKit is available under the MIT license. See the LICENSE file for more info.

xyiapkit's People

Contributors

mqccoder avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.