在app购买时遇到问题

时间:2014-05-05 07:10:52

标签: ios objective-c in-app-purchase

我一直在关注实施应用内购买的教程 - How do you add an in-app purchase to an iOS application?

然而我在线上出错:

productsRequest.delegate = self;

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

[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 

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

这是.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;

这是我的.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

1 个答案:

答案 0 :(得分:3)

这些不是错误而是警告,您应该在类声明中添加协议一致性。

@interface ViewController : UIViewController < SKPaymentTransactionObserver, SKProductsRequestDelegate >

确保实现两种协议的所有必需方法。

相关问题