user3595043
user3595043

Reputation: 21

Trouble implementing in app purchase

I have been following this tutorial on implementing an in app purchase - How do you add an in-app purchase to an iOS application?

However I get errors on lines:

productsRequest.delegate = self;

"Assigning to 'id <SKProductsRequestDelegate> from incompatible type "ViewController *const___strong'

and

[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 

Sending "ViewController *const___strong' to parameter of incompatible type 'id <SKPaymentTransactionObserver>

This is the .h:

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <StoreKit/StoreKit.h>

@interface ViewController : UIViewController
{
     IBOutlet UIButton *removeAdsButton;
}

- (IBAction)purchase;
- (IBAction)restore;
- (IBAction)tapsRemoveAdsButton;

@end

BOOL areAdsRemoved;

And this is my .m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

//Removal of ads
areAdsRemoved = [[NSUserDefaults standardUserDefaults] boolForKey:@"areAddsRemoved"];
[[NSUserDefaults standardUserDefaults] synchronize];
//this will load wether or not they bought the in-app purchase

if(areAdsRemoved){
    [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
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#define kRemoveAdsProductIdentifier @"com.companyname.appname.removeads"

- (IBAction)tapsRemoveAdsButton {
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: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{
ADBannerView *banner;
[banner setAlpha:0];
areAdsRemoved = YES;
removeAdsButton.hidden = YES;
removeAdsButton.enabled = NO;
[[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@"areAdsRemoved"];
//use NSUserDefaults so that you can load wether or not they bought it
[[NSUserDefaults standardUserDefaults] synchronize];
}

@end

Upvotes: 1

Views: 478

Answers (1)

Jerome Diaz
Jerome Diaz

Reputation: 1866

Those are not errors but warnings, you should add protocol conformance to your class declaration.

@interface ViewController : UIViewController < SKPaymentTransactionObserver, SKProductsRequestDelegate >

Be sure to implement every required method of both protocols.

Upvotes: 3

Related Questions