苹果开发——App内购以及验证store的收据(二)

原地址:http://zengwu3915.blog.163.com/blog/static/2783489720137605156966?suggestedreading

三、 客户端使用StoreKit完成内购

添加Storekit.Framework,编写自己的storeObsever,用于处理交易,代码如下,其中completeTransaction和failedTransaction两个函数是自定义的用来处理交易成功与失败其它的就都是SKPaymentTransactionObserver这个代理要求的。

//
// StoreObserver.h
// HelloStore
//
// Created by guoke on 13-8-4.
// Copyright (c) 2013年 guoke. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <StoreKit/StoreKit.h>
#import <StoreKit/SKPaymentTransaction.h>
@interface StoreObserver : NSObject <SKPaymentTransactionObserver, SKProductsRequestDelegate>
{
int buyType;
}

-(void)Create;
-(void)Destroy;

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
-(void)PurchasedTransaction: (SKPaymentTransaction *)transaction;
-(void)completeTransaction: (SKPaymentTransaction *)transaction;
-(void)failedTransaction: (SKPaymentTransaction *)transaction;
-(void)paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;
-(void)paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;

-(void)Buy:(int)type;
-(bool)CanMakePay;
-(void)RequestProductData;
-(void)restoreTransaction: (SKPaymentTransaction *)transaction;

@end

//
// StoreObserver.m
// HelloStore
//
// Created by guoke on 13-8-4.
// Copyright (c) 2013年 guoke. All rights reserved.
//
#import "StoreObserver.h"
#define ProductID @"com.HelloStorTest.TestItem" // 产品ID,对应iTunes Connect中设置的产品ID

@implementation StoreObserver

-(void)Create
{
//---------------------
//----监听购买结果
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}

-(void)Destroy
{
//解除监听
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}

-(void)Buy:(int)type
{
buyType = type;
if ([self CanMakePay])
{
NSLog(@"允许程序内付费购买");
[self RequestProductData];
}
else
{
NSLog(@"不允许程序内付费购买");
}
}

-(bool)CanMakePay
{
return [SKPaymentQueue canMakePayments];
}

-(void)RequestProductData
{
NSLog(@"---------请求对应的产品信息------------");
NSArray *product = nil;

// 这时可以根据buyType不同选择不同的商品,这个只是测试,只用了一种
product=[[NSArray alloc] initWithObjects:ProductID,nil];

NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
request.delegate=self;
[request start];
[product release];
}

//<SKProductsRequestDelegate> 请求协议
//收到的产品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{

NSLog(@"-----------收到产品反馈信息--------------");
NSArray *myProduct = response.products;
NSLog(@"产品Product ID:%@",response.invalidProductIdentifiers);
NSLog(@"产品付费数量: %d", [myProduct count]);
// populate UI
for(SKProduct *product in myProduct){
NSLog(@"product info");
NSLog(@"SKProduct 描述信息%@", [product description]);
NSLog(@"产品标题 %@" , product.localizedTitle);
NSLog(@"产品描述信息: %@" , product.localizedDescription);
NSLog(@"价格: %@" , product.price);
NSLog(@"Product id: %@" , product.productIdentifier);
}
SKPayment *payment = nil;

payment = [SKPayment paymentWithProductIdentifier:ProductID];
NSLog(@"---------发送购买请求------------");
[[SKPaymentQueue defaultQueue] addPayment:payment];
[request autorelease];
}
- (void)requestProUpgradeProductData
{
NSLog(@"------请求升级数据---------");
NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];
SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];

}
//弹出错误信息
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"-------弹出错误信息----------");
}

-(void) requestDidFinish:(SKRequest *)request
{
NSLog(@"----------反馈信息结束--------------");
}

-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----PurchasedTransaction----");
NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
[self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
[transactions release];
}

//<SKPaymentTransactionObserver> 千万不要忘记绑定,代码如下:
//----监听购买结果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray*)transactions//交易结果
{
NSLog(@"-----paymentQueue--------");
for(SKPaymentTransaction*transaction in transactions)
{
switch(transaction.transactionState)
{
caseSKPaymentTransactionStatePurchased://交易完成
[self completeTransaction:transaction];
NSLog(@"-----交易完成 --------");
break;
caseSKPaymentTransactionStateFailed://交易失败
[self failedTransaction:transaction];
NSLog(@"-----交易失败 --------");
break;
caseSKPaymentTransactionStateRestored://已经购买过该商品
[self restoreTransaction:transaction];
NSLog(@"-----已经购买过该商品 --------");
caseSKPaymentTransactionStatePurchasing://商品添加进列表
NSLog(@"-----商品添加进列表 --------");
break;
default:
break;
}
}
}
-(void) completeTransaction:(SKPaymentTransaction*)transaction
{
NSLog(@"-----completeTransaction--------");

// 接受到的App Store验证字符串,这里需要经过JSON编码
NSString* jsonObjectString =[self encode:(uint8_t*)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];

// 将jsonObjectString字符串发给服务器,由服务器POST到iTunes上验证

// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"失败");
if (transaction.error.code != SKErrorPaymentCancelled)
{
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction
{
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@" 交易恢复处理");
}

-(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSLog(@"-------paymentQueue----");
}

// base64编码
- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length
{
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t *output = (uint8_t *)data.mutableBytes;

for (NSInteger i = 0; i < length; i += 3) {
NSInteger value = 0;
for (NSInteger j = i; j < (i + 3); j++) {
value <<= 8;

if (j < length) {
value |= (0xFF & input[j]);
}
}

NSInteger index = (i / 3) * 4;
output[index + 0] = table[(value >> 18) & 0x3F];
output[index + 1] = table[(value >> 12) & 0x3F];
output[index + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : ‘=‘;
output[index + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : ‘=‘;
}

return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}
@end

在ViewController.m中创建和释放

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.observer = [[StoreObserver alloc] init];
[self.observer Create];

// test
[self.observer Buy:0];
}
- (void)viewDidUnload
{
[self.observer Destroy];
[self.observer release];
}

编译运行,将会看到以下Log信息

2013-08-04 19:15:39.776 HelloStoreTest[9839:c07] 允许程序内付费购买
2013-08-04 19:15:39.777 HelloStoreTest[9839:c07] ---------请求对应的产品信息------------
2013-08-04 19:15:39.780 HelloStoreTest[9839:c07] -----paymentQueue--------
2013-08-04 19:15:39.781 HelloStoreTest[9839:c07] -----商品添加进列表 --------
2013-08-04 19:15:39.989 HelloStoreTest[9839:c07] -----------收到产品反馈信息--------------
2013-08-04 19:15:39.990 HelloStoreTest[9839:c07] 产品Product ID:(
)
2013-08-04 19:15:39.990 HelloStoreTest[9839:c07] 产品付费数量: 1
2013-08-04 19:15:39.990 HelloStoreTest[9839:c07] product info
2013-08-04 19:15:39.991 HelloStoreTest[9839:c07] SKProduct 描述信息<SKProduct: 0x8b4e5a0>
2013-08-04 19:15:39.991 HelloStoreTest[9839:c07] 产品标题 测试商品
2013-08-04 19:15:39.991 HelloStoreTest[9839:c07] 产品描述信息: 这里是一个store测试商品
2013-08-04 19:15:39.992 HelloStoreTest[9839:c07] 价格: 6
2013-08-04 19:15:39.992 HelloStoreTest[9839:c07] Product id: com.HelloStorTest.TestItem
2013-08-04 19:15:39.992 HelloStoreTest[9839:c07] ---------发送购买请求------------
2013-08-04 19:15:39.993 HelloStoreTest[9839:c07] -----paymentQueue--------
2013-08-04 19:15:39.993 HelloStoreTest[9839:c07] -----商品添加进列表 --------
2013-08-04 19:15:39.994 HelloStoreTest[9839:c07] ----------反馈信息结束--------------

四、验证store收据

交易完成后,服务器需要向Apple验证交易是否成功,不然,自己买的产品给别人了就亏了。

在上面的客户端代码中,我们监听函数中在交易成功时调用了completeTransaction函数,购买成功之后,我们会收到反馈的收据,其中在transactionReceipt中存验证信息。将字打印出来格式为:

{
"signature" = "Arcf6xlj4B3XqosYqFrhSNltW+HZS1UOLTh30CLb2AXTW5GABZRK52j1B8eyOynXUqlqhpZCjCk51pWyiO59JCukrz9ktQeaVCha2xgqjzasqz6x8Ulsm+VeA4bDs3eakq5xcMgogRDUOBg5sJauPwMQA30PnLV+ruypva91JAS2AAADVzCCA1MwggI7oAMCAQICCGUUkU3ZWAS1MA0GCSqGSIb3DQEBBQUAMH8xCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSYwJAYDVQQLDB1BcHBsZSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEzMDEGA1UEAwwqQXBwbGUgaVR1bmVzIFN0b3JlIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTA5MDYxNTIyMDU1NloXDTE0MDYxNDIyMDU1NlowZDEjMCEGA1UEAwwaUHVyY2hhc2VSZWNlaXB0Q2VydGlmaWNhdGUxGzAZBgNVBAsMEkFwcGxlIGlUdW5lcyBTdG9yZTETMBEGA1UECgwKQXBwbGUgSW5jLjELMAkGA1UEBhMCVVMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMrRjF2ct4IrSdiTChaI0g8pwv/cmHs8p/RwV/rt/91XKVhNl4XIBimKjQ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vci1pZGVudGlmaWVyIiA9ICI2Q0Y4ODg1Ni1FRjZGLTRGN0UtOTJGOC1BNUEwMjJFNzdERUIiOwoJInByb2R1Y3QtaWQiID0gImNvbS5IZWxsb1N0b3JUZXN0LlRlc3RJdGVtIjsKCSJpdGVtLWlkIiA9ICI2ODM5MTkxNjIiOwoJImJpZCIgPSAiY29tLmd1b2tlLkhlbGxvU3RvcmVUZXN0IjsKCSJwdXJjaGFzZS1kYXRlLW1zIiA9ICIxMzc1NjE0OTQ1Mjc1IjsKCSJwdXJjaGFzZS1kYXRlIiA9ICIyMDEzLTA4LTA0IDExOjE1OjQ1IEV0Yy9HTVQiOwoJInB1cmNoYXNlLWRhdGUtcHN0IiA9ICIyMDEzLTA4LTA0IDA0OjE1OjQ1IEFtZXJpY2EvTG9zX0FuZ2VsZXMiOwoJIm9yaWdpbmFsLXB1cmNoYXNlLWRhdGUiID0gIjIwMTMtMDgtMDQgMTE6MTU6NDUgRXRjL0dNVCI7Cn0=";
"environment" = "Sandbox";
"pod" = "100";
"signing-status" = "0";
}

将这个字符串发给服务器,然后用服务器去POST到App上去验证。

验证过程:( 原本是在服务器上验证的,为了测试,我们不弄太复杂,直接在当前程序做一个验证测试 )

1. 先取出transactionReceipt属性中的收据数据。并以base64方式编码

NSString* ObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];

// 其中encode就是base64编码转换函数,编码后的数据为类似:

ewoJInNpZ25hdHVyZSIgPSAiQXJjZjZ4bGo0QjNYcW9zWXFGcmhTTmx0VytIWlMxVU9MVGgzMENMYjJBWFRXNUdBQlpSSzUyajFCOGV5T3luWFVxbHFocFpDakNrNTFwV3lpTzU5SkN1a3J6OWt0UWVhVkNoYTJ4Z3FqemFzcXo2eDhVbHNtK1ZlQTRiRHMzZWFrcTV4Y01nb2dSRFVPQmc1c0phdVB3TVFBMzBQbkxWK3J1eXB2YTkxSkFTMkFBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NHVVV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VdE9USkdPQzFCTlVFd01qSkZOemRFUlVJaU93b0pJbkJ5YjJSMVkzUXRhV1FpSUQwZ0ltTnZiUzVJWld4c2IxTjBiM0pVWlhOMExsUmxjM1JKZEdWdElqc0tDU0pwZEdWdExXbGtJaUE5SUNJMk9ETTVNVGt4TmpJaU93b0pJbUpwWkNJZ1BTQWlZMjl0TG1kMWIydGxMa2hsYkd4dlUzUnZjbVZVWlhOMElqc0tDU0p3ZFhKamFHRnpaUzFrWVhSbExXMXpJaUE5SUNJeE16YzFOakUwT1RRMU1qYzFJanNLQ1NKd2RYSmphR0Z6WlMxa1lYUmxJaUE5SUNJeU1ERXpMVEE0TFRBMElERXhPakUxT2pRMUlFVjBZeTlIVFZRaU93b0pJbkIxY21Ob1lYTmxMV1JoZEdVdGNITjBJaUE5SUNJeU1ERXpMVEE0TFRBMElEQTBPakUxT2pRMUlFRnRaWEpwWTJFdlRHOXpYMEZ1WjJWc1pYTWlPd29KSW05eWFXZHBibUZzTFhCMWNtTm9ZWE5sTFdSaGRHVWlJRDBnSWpJd01UTXRNRGd0TURRZ01URTZNVFU2TkRVZ1JYUmpMMGROVkNJN0NuMD0iOwoJImVudmlyb25tZW50IiA9ICJTYW5kYm94IjsKCSJwb2QiID0gIjEwMCI7Cgkic2lnbmluZy1zdGF0dXMiID0gIjAiOwp9

2. 创建JSON对象,字典格式,单键值对,键名为”receipt-data“,值为上一步编码的数据

{”recipt-data“:"编码后数据"}

NSString* sendString = [[NSString alloc] initWithFormat:@"{\"receipt-data\":\"%@\"}",ObjectString ];

3. 发送HTTP POS 的请求,将数据发送到App Store,其地址为 https://buy.itunes.apple.com/verifyReceipt

sandbox测试地址为:https://sandbox.itunes.apple.com/verifyReceipt

NSURL *sandboxStoreURL = [[NSURL alloc] initWithString: @"https://sandbox.itunes.apple.com/verifyReceipt"];
NSData *postData = [NSData dataWithBytes:[sendString UTF8String] length:[sendString length]];
NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:sandboxStoreURL];
[connectionRequest setHTTPMethod:@"POST"];
[connectionRequest setTimeoutInterval:120.0];
[connectionRequest setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[connectionRequest setHTTPBody:postData];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:connectionRequest
delegate:self];
[connection release];

4. App Store的返回值也是一个JSON格式的对象,包含两个键值对, status和receipt:

{

“status” : 0,

“receipt” : { … }

}

{"receipt":{"original_purchase_date_pst":"2013-08-04 04:15:45 America/Los_Angeles", "purchase_date_ms":"1375614945275", "unique_identifier":"0000b031c7b8", "original_transaction_id":"1000000082868664", "bvrs":"1.0", "transaction_id":"1000000082868664", "quantity":"1", "unique_vendor_identifier":"6CF88856-EF6F-4F7E-92F8-A5A022E77DEB", "item_id":"683919162", "product_id":"com.HelloStorTest.TestItem", "purchase_date":"2013-08-04 11:15:45 Etc/GMT", "original_purchase_date":"2013-08-04 11:15:45 Etc/GMT", "purchase_date_pst":"2013-08-04 04:15:45 America/Los_Angeles", "bid":"com.guoke.HelloStoreTest", "original_purchase_date_ms":"1375614945275"}, "status":0}

如果status的值为0, 就说明该receipt为有效的。 否则就是无效的。

App Store的收据

发送给App Store的收据数据是通过对transaction中对应的信息编码而创建的。 当App Store验证收据时, 将从其中解码出数据,并以”receipt”的键返回。 返回的响应信息是JSON格式,被包含在SKPaymentTransaction的对象中(transactionReceipt属性)。Server可通过这些值来了解交易的详细信息。 Apple建议只发送receipt数据到服务器并使用receipt数据验证和获得交易详情。 因为App Store可验证收据信息,返回信息,保证信息不被篡改,这种方式比同时提交receipt和transaction的数据要安全。

购买信息的键,很多的键都对应SKPaymentTransaction的属性。

键名  描述

quantity  购买商品的数量。对应SKPayment对象中的quantity属性

product_id  商品的标识,对应SKPayment对象的productIdentifier属性。

transaction_id  交易的标识,对应SKPaymentTransaction的transactionIdentifier属性

purchase_date  交易的日期,对应SKPaymentTransaction的transactionDate属性

original_-transaction_id  对于恢复的transaction对象,该键对应了原始的transaction标识

original_purchase_-date  对于恢复的transaction对象,该键对应了原始的交易日期

app_item_id  App Store用来标识程序的字符串。一个服务器可能需要支持多个server的支付功能,可以用这个标识来区分程序。链接sandbox用来测试的程序的不到这个值,因此该键不存在。

version_external_-identifier  用来标识程序修订数。该键在sandbox环境下不存在

bid iPhone 程序的bundle标识

bvrs iPhone 程序的版本号

参考文献:

http://www.himigame.com/iphone-cocos2d/550.html

http://www.cocoachina.com/bbs/read.php?tid=87662

http://blog.csdn.net/zu12jing/article/details/5880654

附件为本文测试程序,直接改名为zip解压即可(因网易不支持zip)

HelloStore.rar(39.45K)   
下载

苹果开发——App内购以及验证store的收据(二)

时间: 2024-10-15 13:50:20

苹果开发——App内购以及验证store的收据(二)的相关文章

苹果开发——App内购以及验证store的收据(一)

原地址:http://zengwu3915.blog.163.com/blog/static/27834897201375105236580?suggestedreading 发了几天时间在网上折腾了几天,终于把App内购给弄好了.分享一下个人经验 一. 在iTunesConnect中创建内购的App,然后为这个App设置一些产品(付费道具等) 打开iTunesConnect网站https://itunesconnect.apple.com/WebObjects/iTunesConnect.wo

app内购提示,您已购买此商品,但为下载

出现这样的问题,是支付没有finish造成的,一般在支付过程中断网了,下次再购买同一商品的时候就会出现这样的问题, 解决办法,在点击购买的时候判断支付队列中是否有为finish的商品,若有,则进行处理之后将其finish NSArray* transactions = [SKPaymentQueue defaultQueue].transactions; if (transactions.count > 0) { //检测是否有未完成的交易 SKPaymentTransaction* trans

苹果内购服务器验证之receipt返回多组in_app思考

最近有部分用户反映,苹果内购充值失败,经过测试总结有几个关键点出现问题 1.app购买成功苹果没有返回票据,属于票据遗漏(取决于苹果服务器的响应状况),只能客户端进行监听刷新等处理 2.app连续购买的过程中,前几次苹果没有返回票据,几次之后,苹果返回了一个有效的票据,app提交给服务器进行验证的过程中in_app出现多组数据的情况,这种情况还是能充值成功了,只是不能全部到账 3.app连续购买,有一次正常返回票据,在提交给服务器的过程中出现意外,但实际服务端已经接受到票据,为用户成功充值,但a

iOS开发系列——内购、GameCenter、iCloud、Passbook系统服务开发汇总

社交 Social 现在很多应用都内置“社交分享”功能,可以将看到的新闻.博客.广告等内容分享到微博.微信.QQ.空间等,其实从iOS6.0开始苹果官方就内置了Social.framework专门来实现社交分享功能,利用这个框架开发者只需要几句代码就可以实现内容分享.下面就以一个分享到新浪微博的功能为例来演示Social框架的应用,整个过程分为:创建内容编辑控制器,设置分享内容(文本内容.图片.超链接等),设置发送(或取消)后的回调事件,展示控制器. 程序代码: // // ViewContro

iOS App 内购 Demo

/*注意事项: 1.沙盒环境测试appStore内购流程的时候,请使用没越狱的设备. 2.请务必使用真机来测试,一切以真机为准. 3.项目的Bundle identifier需要与您申请AppID时填写的bundleID一致,不然会无法请求到商品信息. 4.如果是你自己的设备上已经绑定了自己的AppleID账号请先注销掉,否则你哭爹喊娘都不知道是怎么回事. 5.订单校验 苹果审核app时,仍然在沙盒环境下测试,所以需要先进行正式环境验证,如果发现是沙盒环境则转到沙盒验证. 识别沙盒环境订单方法:

php苹果内购订单验证

/** * 21000 App Store不能读取你提供的JSON对象 * 21002 receipt-data域的数据有问题 * 21003 receipt无法通过验证 * 21004 提供的shared secret不匹配你账号中的shared secret * 21005 receipt服务器当前不可用 * 21006 receipt合法,但是订阅已过期.服务器接收到这个状态码时,receipt数据仍然会解码并一起发送 * 21007 receipt是Sandbox receipt,但却发

App Store内购

一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程序将从App Store接收那些你想要提供的产品的信息,并将它们显示出来供用户购买.当用户需要购买某件产品时,程序调用StoreKit来收集购买信息.下图即为基本的store kit 模型: Store Kit的API只是为程序添加In App Purchase功能的一小部分.你需要决定如何去记录那些你想要提交的产品,如何在程序中将商店功能展现给用户,还要考虑如何将用户购买的产品提交.本章的剩

苹果内购流程详解

苹果内购主要分为两部分 itunnes 相关配置 一.在itunnes 上配置相关信息资料 首先需要配置一些前提条件,详细步骤如下 1.进入itunnes选择协议,税务银行业务 2.完善资料信息,主要有3项(联系人,银行信息,税务信息) 4.完善联系人信息,一般填申请开发者账号人员的信息,可以修改 5.完善银行信息 需要银行支付号(就是开户银行的一个标识),银行卡持有人名字 6.完善税务信息 7.以下默认选择no 8.确认界面,下图可能不太对,当时我是上下2个界面,第四项要上下一致才行 二.上面

iOS:苹果内购实践

iOS 苹果的内购 一.介绍 苹果规定,凡是虚拟的物品(例如:QQ音乐的乐币)进行交易时,都必须走苹果的内购通道,苹果要收取大约30%的抽成,所以不允许接入第三方的支付方式(微信.支付宝等),当然开发者可以设置后门,在审核时避开审核人员.这个是有风险的,一旦发现,app会被立即下架,还是老老实实接入内购吧. 二.注意 内购接入还是比较简单的,苹果提供了专门的框架<StoreKit/StoreKit.h>,只要按照它提供的api进行开发就行.然而,接入的过程还是有需要注意的地方,分别是:漏单处理