Domanda

I followed this tutorial and it seems to be working fine for ios6 but when I try with ios7 it never calls :

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)

(Fixed the incompatible type error, thanks to Macro206) (In-App purchases still aren't working on ios7 (but seems to work fine on ios6(Able to buy and the ad banner's alpha set to 0 (alpha is set when the BOOL is true, from elsewhere in my app))))

Here's what I have: (I removed the animation/graphics codes to make it shorter)

//
//  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

The warnings I get are :

(At line: @implementation MainMenu)
Method definition for 'tapsRemoveAdsButton' not found
Method definition for 'purchase' not found

I looked at similar questions but never really understood how to fix it, adding "(id)self" instead of just "self" got rid of the errors but it doesn't fix the problem, the code stops at "[productsRequest start];" and "- (void)productsRequest:" never gets fired.

I'm sure i'm doing basic mistakes =(

(Oh and in case it matters, I've been testing it in the simulator, works fine on ios6 but not on ios7)

È stato utile?

Soluzione

Like Macro206 mentioned you have to add <SKProductsRequestDelegate>after your @interface AND #import <StoreKit/StoreKit.h>. Furthermore In-App-Purchase should be tested on a real device with a special test account.

Your code is terribly formatted and you are keeping obsolete lines. If you want people to review your code, you should make it easier for them and make it readable. Have a look at this link

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top