Monkey original, reprinted. Reprinted Please note: Reprinted from Cocos2D Development Network-cocos2dev.com, thank you!
Original address: http://www.cocos2dev.com /? P = 514
When I used the ios7SDK build yesterday, I found several warnings. Some of the previously written internal purchase methods were discarded under ios7. Today I changed it and sent the code.
PGStoreKitManager. h
//// PGStoreKitManager. h // OCPhysicGame /// Created by LiuYanghui on 14-1-26. // Copyright (c) 2014 LiuYanghui. all rights reserved. // # import <Foundation/Foundation. h> # import <GameKit/GameKit. h> # import <StoreKit/StoreKit. h> @ class ViewController; @ interface PGStoreKitManager: NSObject <program, SKPaymentTransactionObserver, role> {UIAlertView * _ loadingAlert; BOOL _ enableGameCenter;} @ property (nonatomic, readwrite, strong) viewController * viewController; + (PGStoreKitManager *) getInstance; // game center ------------------------------------------/** log on to gamecenter. Set setViewController */-(void) authenticateLocalPlayer; /** upload points */-(void) reportScore: (NSString *) identifier hiScore :( int64_t) score;/** upload achievement */-(void) reportAchievementIdentifier: (NSString *) identifier percentComplete :( float) percent;/** display ultimate */-(void) showLeaderboard: (NSString *) leaderboard; /** display achievement */-(void) showAchievements; // iap --------------------------------------------/** initialize the initStoreKit */-(void; /** Purchase Product */-(void) purchaseItem: (NSString *) identifier; @ end
PGStoreKitManager. m
//// PGStoreKitManager. m // OCPhysicGame /// Created by LiuYanghui on 14-1-26. // Copyright (c) 2014 LiuYanghui. all rights reserved. // # import "PGStoreKitManager. h "# import" ViewController. h "@ implementation PGStoreKitManager + (PGStoreKitManager *) getInstance {static PGStoreKitManager * mgr = nil; static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {mgr = [[self alloc] init] ;}); return mgr ;} -(Id) init {self = [super init]; if (self) {[self initData]; return self;} return nil;}-(void) initData {_ enableGameCenter = NO; _ viewController = nil;}-(void) showMessage :( NSString *) title Message :( NSString *) msg {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message: msg delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alert show];} -(void) showLoadingView :( NSS Tring *) title {_ loadingAlert = [[UIAlertView alloc] initWithTitle: title message: @ "" delegate: self cancelButtonTitle: nil otherButtonTitles: nil, nil]; [_ loadingAlert show];}-(void) removeLoadingView {[_ loadingAlert dismissWithClickedButtonIndex: 0 animated: YES];} # pragma mark-GameCenter-(void) authenticateLocalPlayer {GKLocalPlayer * localPlayer = [GKLocalPlayer localPlayer]; if ([localPlayer isAuthen Ticated] = NO) {localPlayer. authenticateHandler = ^ (UIViewController * viewController, NSError * error) {if (error) {_ enableGameCenter = NO;} else {_ enableGameCenter = YES; if (viewController) {[_ viewController presentViewController: viewController animated: YES completion: nil] ;}};} else {_ enableGameCenter = YES ;}}/** upload points */-(void) reportScore: (NSString *) identifier hiScore :( int64_t) score ;{ If (score <0 |! _ EnableGameCenter) return; GKScore * scoreBoard = [[GKScore alloc] initWithLeaderboardIdentifier: identifier]; scoreBoard. value = score; [GKScore reportScores: @ [scoreBoard] withCompletionHandler: ^ (NSError * error) {if (error) {// handle error}];} /** upload achievement */-(void) reportAchievementIdentifier: (NSString *) identifier percentComplete :( float) percent {if (percent <0 |! _ EnableGameCenter) return; GKAchievement * achievement = [[GKAchievement alloc] initWithIdentifier: identifier]; if (achievement) {achievement. percentComplete = percent; [GKAchievement reportAchievements: @ [achievement] withCompletionHandler: ^ (NSError * error) {if (error) {// handle error}];} /** display ultimate version */-(void) showLeaderboard: (NSString *) leaderboard {if (! _ EnableGameCenter) return; GKGameCenterViewController * gameCenterViewController = [[GKGameCenterViewController alloc] init]; gameCenterViewController. viewState = GKGameCenterViewControllerStateLeaderboards; gameCenterViewController. gameCenterDelegate = self; [_ viewController presentViewController: gameCenterViewController animated: YES completion: nil];}/** display achievement */-(void) showAchievements {if (! _ EnableGameCenter) return; GKGameCenterViewController * gameCenterViewController = [[GKGameCenterViewController alloc] init]; gameCenterViewController. viewState = GKGameCenterViewControllerStateAchievements; gameCenterViewController. gameCenterDelegate = self; [_ viewController presentViewController: gameCenterViewController animated: YES completion: nil] ;}# pragma mark gameCenterViewController Close callback-(v Oid) gameCenterViewControllerDidFinish :( GKGameCenterViewController *) gameCenterViewController {[_ viewController dismissViewControllerAnimated: YES completion: nil];} // region # pragma mark-IAP-(BOOL) canProcessPayments {if ([SKPaymentQueue canMakePayments]) {return YES;} else {return NO ;}/ ** initialize in-class consumption */-(void) initStoreKit {[[SKPaymentQueue default Queue] addTransactionObserver: self];}/** Purchase Product */-(void) purchaseItem: (NSString *) identifier {[self showLoadingView: @ "Access Store... "]; if (! [Self canProcessPayments]) {NSLog (@ "1. failed --> SKPaymentQueue canMakePayments NO "); [self removeLoadingView]; return;} NSLog (@" 1. success --> Request Product information... % @ ", identifier); // use the requested product information to purchase SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: identifier]; request. delegate = self; [request start];} // callback of SKProductsRequest-(void) productsRequest :( SKProductsRequest *) Request didReceiveResponse :( SKProductsResponse *) response {NSArray * myProduct = response. products; if (myProduct. count = 0) {NSLog (@ "2. failed --> unable to obtain product information. Failed to purchase. InvalidProductIdentifiers = % @ ", response. invalidProductIdentifiers); [self removeLoadingView]; return;} NSLog (@ "2. successful --> the product information is obtained successfully. you are purchasing the product... "); SKPayment * payment = [SKPayment paymentWithProduct: myProduct [0]; [[SKPaymentQueue defaultQueue] addPayment: payment];} // callback of SKPayment-(void) paymentQueue :( SKPaymentQueue *) queue updatedTransactions :( NSArray *) transactions {NSLog (@ "3. success --> receive Apple purchase data, processing... "); for (SKPaymentTransaction * transaction in transactions) {switch (transaction. transactionState) {case SKPaymentTransactionStatePurchased: [self completeTransaction: transaction]; break; case when: [self failedTransaction: transaction]; break; case when: [self restoreTransaction: transaction; default: break ;}}// end transaction-(void) completeTransaction: (SKPaymentTransaction *) transaction {NSLog (@ "4. success --> end transaction SKPaymentTransactionStatePurchased "); [self removeLoadingView]; // record the transaction and provide the product. The two methods must be processed: [self recordTransaction: transaction]; [self provideContent: transaction. payment. productIdentifier]; // remove transaction from the payment queue. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];} // reset transaction-(void) restoreTransaction: (SKPaymentTransaction *) transaction {NSLog (@ "4. success --> Reset transaction SKPaymentTransactionStateRestored "); [self recordTransaction: transaction]; [self provideContent: transaction. originalTransaction. payment. productIdentifier]; [[SKPaymentQueue defaultQueue] finishTransaction: transaction];} // transaction failed-(void) failedTransaction: (SKPaymentTransaction *) transaction {[self removeLoadingView]; NSLog (@ "4. success --> Transaction Failed SKPaymentTransactionStateRestored error. code: % d ", (int) transaction. error. code); [[SKPaymentQueue defaultQueue] finishTransaction: transaction];} // transaction record-(void) recordTransaction: (SKPaymentTransaction *) transacation {NSLog (@ "4. success --> transaction records, where records can be stored ");} // provides the product-(void) provideContent: (NSString *) identifier {NSLog (@" 4. success --> transaction successful. Please provide product identifier =%@ ", identifier); [self removeLoadingView]; [self showMessage: @" Success "Message: @" You have successfully purchased. "];} @ end
The comment has been clearly written, and you can leave a message if you are not clear.