标签:
1.倒入:StoreKit.framework库
2.代码
1 #import <UIKit/UIKit.h> 2 3 //导入库 4 #import <StoreKit/StoreKit.h> 5 @interface ViewController : UIViewController<SKProductsRequestDelegate,SKPaymentTransactionObserver> 6 @property (strong, nonatomic) IBOutlet UITextField *productID; 7 @property (strong, nonatomic) IBOutlet UIButton *purchase; 8 //xib购买按钮 9 - (IBAction)purchaseFunc:(id)sender; 10 @end
#import "ViewController.h" /** 在测试购买时你输入的购买用户一定是 用户智能下面的 沙箱技术测试员 里面的账号 */ @interface ViewController () @property (nonatomic,copy,readonly) NSString *localizedPrice; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; self.productID.text = @"com.fadada.grB";//你的内购产品的id } //点击 - (IBAction)purchaseFunc:(id)sender { 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(@"产品付费数量:%ld",[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]; }
3.xib
标签:
原文地址:http://www.cnblogs.com/fusheng-it/p/5632036.html