我遵循了本教程,它对于ios6似乎运行良好,但是当我尝试使用ios7时,它永远不会调用:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)
(修复了不兼容的类型错误,这要归功于Macro206)(在ios7上仍无法使用应用内购买功能(但在ios6上似乎可以正常使用(可以购买并且广告横幅的Alpha设置为0(在BOOL上设置了Alpha设置))是真实的,来自我应用程序的其他位置))))
这是我所拥有的:(我删除了动画/图形代码以使其更短)
//
// MainMenu.m
// HungryFish
//
//
//
//#import "AppDelegate.h"
#import "MainMenu.h"
#import "cocos2d.h"
#import "HelloWorldLayer.h"
#import "SimpleAudioEngine.h"
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <StoreKit/StoreKit.h>
@implementation MainMenu
CCDirectorIOS *director_;
BOOL areAdsRemoved=nil;
+(id) scene
{
CCScene *scene = [CCScene node];
MainMenu *layer = [MainMenu node];
[scene addChild: layer];
return scene;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
int ADSIZE;
-(id) init
{
if( (self=[super init] )) {
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
areAdsRemoved = [[NSUserDefaults standardUserDefaults] boolForKey:@"areAddsRemoved"];
[[NSUserDefaults standardUserDefaults] synchronize];
//this will load wether or not they bought the in-app purchase
if(areAdsRemoved){
NSLog(@"Ads removed");
// [self.view setBackgroundColor:[UIColor blueColor]];
//if they did buy it, set the background to blue, if your using the code above to set the background to blue, if your removing ads, your going to have to make your own code here
}
}
return self;
}
// IN APP PURCHASES
#define kRemoveAdsProductIdentifier @"FishyFishinAPPid"
- (void)tapsRemoveAds{
NSLog(@"User requests to remove ads");
if([SKPaymentQueue canMakePayments]){
NSLog(@"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(@"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(@"Products Available!");
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(@"No products available");
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:(id)self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction) restore{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
if(SKPaymentTransactionStateRestored){
NSLog(@"Transaction state -> Restored");
//called when the user successfully restores a purchase
[self doRemoveAds];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(@"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(@"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(@"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)doRemoveAds{
areAdsRemoved = YES;
[[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@"areAdsRemoved"];
//use NSUserDefaults so that you can load wether or not they bought it
[[NSUserDefaults standardUserDefaults] synchronize];
}
// IN APP PURCHASES END
- (void) dealloc
{
[super dealloc];
}
@end
//
// MainMenu.h
// HungryFish
//
//
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import <StoreKit/StoreKit.h>
@interface MainMenu : CCLayer <SKProductsRequestDelegate>
{
}
extern BOOL areAdsRemoved;
- (IBAction)purchase;
- (IBAction)restore;
- (IBAction)tapsRemoveAdsButton;
+(id) scene;
@end
我得到的警告是:
(At line: @implementation MainMenu)
Method definition for 'tapsRemoveAdsButton' not found
Method definition for 'purchase' not found
我看过类似的问题,但从未真正了解如何解决,添加了({id)self“而不是仅仅” self“摆脱了错误,但并不能解决问题,代码停在了[[productsRequest start] ;“ 和“-(void)productsRequest:”永远不会被解雇。
我确定我在犯基本错误=(
(哦,以防万一,我已经在模拟器中对其进行了测试,在ios6上可以正常工作,但在ios7上不能正常工作)
就像提到的Macro206一样,您必须<SKProductsRequestDelegate>
在@interface AND之后添加#import <StoreKit/StoreKit.h>
。此外,应在具有特殊测试帐户的真实设备上对应用内购买进行测试。
您的代码格式非常糟糕,并且您已经过时了。如果您希望人们查看您的代码,则应该使他们更容易阅读并使其易于阅读。看看这个链接
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句