在App购买(删除广告)精灵套件

时间:2014-11-07 23:10:30

标签: ios objective-c storekit

我在这里有完整的功能代码,用于"在App App Purchase(删除iAd)中可以正常运行单视图应用程序,但我需要在MyScene(SpriteKit)游戏中实现它。这是我的ViewController.h

 #import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>

#import <iAd/iAd.h>

@interface ViewController : UIViewController<ADBannerViewDelegate>

@property ( nonatomic,strong) NSArray*products;

@property(nonatomic,strong)NSUserDefaults*defaults;

- (IBAction)removeAdvertisment:(id)sender;
@end

这是我的ViewController.m

#import "ViewController.h"

#import "MyScene.h"

#import <StoreKit/StoreKit.h>

@implementation ViewController

@synthesize  /*idBanner*/ products,defaults;

- (void)viewDidLoad
{
    [super viewDidLoad];

     //Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];


    [[SKPaymentQueue defaultQueue]addTransactionObserver:(id)self];

    defaults =[NSUserDefaults standardUserDefaults];

    BOOL isPro=[defaults boolForKey:@"isPro"];



    if (!isPro) {
        //user code here
        [self fetchProducts];
    }

    else
    {
        //iadBanner.alpha=0.0;

    }

}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:1];
        [UIView commitAnimations];

}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:0];
    [UIView commitAnimations];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

-(void)fetchProducts{
    SKProductsRequest*request = [[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithArray:@[@"ProductIDPurchaseID"]]];
    request.delegate = (id)self;
    [request start];
}

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    products = response.products;
    NSLog(@"The app wass purchased");
}
-(void)request:(SKRequest*)request didFailWithError:(NSError *)error{
    NSLog(@"%@",error);
}

-(void)buy:(SKProduct*)product{

    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue]addPayment:payment];
}

-(void)paymentQueue:(SKPaymentQueue*)queue updatedTransactions:(NSArray *)transactions{

    for (SKPaymentTransaction *tx in transactions)   {
        switch (tx.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [[SKPaymentQueue defaultQueue]finishTransaction:tx];
                //_____________________
                //iadBanner.alpha=0.0;
                //______________________

                [defaults setBool:YES forKey:@"isPro"];
                break;

            case SKPaymentTransactionStateFailed:
                [[SKPaymentQueue defaultQueue]finishTransaction:tx];
                NSLog(@"Error:%@",tx.error);
                break;


            case SKPaymentTransactionStateRestored:
                [[SKPaymentQueue defaultQueue]finishTransaction:tx];
                break;


            default:
                break;
        }
    }


}

- (IBAction)removeAdvertisment:(id)sender {

    SKProduct *prod = [products objectAtIndex:0];
    [self buy:prod];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}


@end

当您按下UIButton时,它会使整个代码生效,并且玩家可以通过购买&#34;删除广告来删除添加内容#34;

此方法使孔代码工作

  • (IBAction)removeAdvertisment:(id)sender {

    SKProduct * prod = [products objectAtIndex:0]; [自我购买:prod]; }

但我需要从SKScene的MyScene类子类中调用它

所以问题是如何从MyScene类调用removeAdvertisement(上面的按钮)方法?

1 个答案:

答案 0 :(得分:0)

在你的SKScene中创建一个ViewController单例,例如

//。h file

@property  UIViewController *gameViewController;

//。h file

你的SKScene的.il文件

 gameViewController= (UIViewController*)self.view.window.rootViewController;

现在您可以通过

访问UIViewController的任何公共函数
[gameViewController removeAdvertisement(@"value")];