ApplePay集成和测试账号申请:
第一步:
//1.首先在项目工程中加入“storekit.framework”,加入头文件#import <StoreKit/StoreKit.h> //2.在.h文件中加入“SKPaymentTransactionObserver,SKProductsRequestDelegate”监听机制 #import "HKBaseViewController.h" #import <StoreKit/StoreKit.h> @interface HKApplePayViewController : HKBaseViewController<SKPaymentTransactionObserver,SKProductsRequestDelegate> @property (nonatomic, strong) UITextField *productID; @end
第二步:
#import "HKApplePayViewController.h" #import "HKPayTools.h" @interface HKApplePayViewController () @end @implementation HKApplePayViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setupNav]; [self setupUI]; [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; self.productID.text = @"com.games.ztyxs.product_point.1"; } - (void)applePay { NSString *product = self.productID.text; if([SKPaymentQueue canMakePayments]){ [self requestProductData:product]; }else{ NSLog(@"不允许程序内付费"); } } //请求商品 - (void)requestProductData:(NSString *)type{ NSLog(@"-------------请求对应的产品信息----------------"); NSArray *product = [[NSArray alloc] initWithObjects:type, nil]; NSSet *nsset = [NSSet setWithArray:product]; SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset]; request.delegate = self; [request start]; } //收到产品返回信息 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ NSLog(@"--------------收到产品反馈消息---------------------"); NSArray *product = response.products; if([product count] == 0){ NSLog(@"--------------没有商品------------------"); return; } NSLog(@"productID:%@", response.invalidProductIdentifiers); NSLog(@"产品付费数量:%lu",(unsigned long)[product count]); SKProduct *p = nil; for (SKProduct *pro in product) { NSLog(@"%@", [pro description]); NSLog(@"%@", [pro localizedTitle]); NSLog(@"%@", [pro localizedDescription]); NSLog(@"%@", [pro price]); NSLog(@"%@", [pro productIdentifier]); if([pro.productIdentifier isEqualToString:self.productID.text]){ p = pro; } } SKPayment *payment = [SKPayment paymentWithProduct:p]; NSLog(@"发送购买请求"); [[SKPaymentQueue defaultQueue] addPayment:payment]; } //请求失败 - (void)request:(SKRequest *)request didFailWithError:(NSError *)error{ NSLog(@"------------------错误-----------------:%@", error); } - (void)requestDidFinish:(SKRequest *)request{ NSLog(@"------------反馈信息结束-----------------"); } //监听购买结果 - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{ for(SKPaymentTransaction *tran in transaction){ switch (tran.transactionState) { case SKPaymentTransactionStatePurchased: NSLog(@"交易完成"); break; case SKPaymentTransactionStatePurchasing: NSLog(@"商品添加进列表"); break; case SKPaymentTransactionStateRestored: NSLog(@"已经购买过商品"); break; case SKPaymentTransactionStateFailed: NSLog(@"交易失败"); break; default: break; } } } //交易结束 - (void)completeTransaction:(SKPaymentTransaction *)transaction{ NSLog(@"交易结束"); [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } - (void)dealloc{ [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self applePay]; } - (void)setupUI { self.view.backgroundColor = kWhiteColor; } -(void)setupNav { self.navTitle = @"ApplePay苹果内支付"; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
第三步:注意事项
/*
注意:
1.沙盒环境测试appStore内购流程的时候,请使用没越狱的设备。
2.请务必使用真机来测试,一切以真机为准。
3.项目的Bundle identifier需要与您申请AppID时填写的bundleID一致,不然会无法请求到商品信息。
*/
相关截图:
手机截屏:
//要求输入AppStore帐密,使用测试生成的即可
确认购买
测试账号申请:
在ItunesConnect中选择“用户和职能”选项~
随后在左上角的选项中选择沙盒测试者,点击左上角的加号图标增加一位测试者,如图
编辑好相应的内容,点击保存,就创建了一个测试账号,是不是很简单啊!当然这个账号如果你忘记了密码可以重新生成一个,无关紧要。
顺带多句嘴,千万不要在正式的appstore上面用沙盒测试的账号来登录,不然后果很严重,千万要牢记在心,此账号只用于测试环境下~