在SpriteKit中的App Purchase集成中

我需要您的建议,使用"In App purchase"删除iAd . 我有"In App Purchase"的工作代码 . 但我不知道如何将其实现到(IOS)SpriteKit游戏中 . 当我在View Controller中实现它时,它工作得很好,但是我需要它在MyScene.m中工作,所以当我点击“ -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event " method " purchase”警告窗口中的按钮时会发出警告并让玩家购买它

This code is working in Storyboard and has IBbutton but it is not seen in SKScene.
I am beginner developer and know how SKScene and MyScene but thats now enough.
If you want to look at code let me know thanks




Here is ny Code:
in View Controller.h I declare


    @interface ViewController : UIViewController <ADBannerViewDelegate>

{
    IBOutlet ADBannerView *iadBanner;
//   

}
@property ( nonatomic,strong) NSArray*products;

@property(nonatomic,strong)NSUserDefaults*defaults;


@end


In ViewController.m

    #import "MyNewlyCreatedScene.h"

#import "ViewController.h"

#import <StoreKit/StoreKit.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize  /*idBanner*/ products,defaults;

- (void)viewDidLoad
{
    [super viewDidLoad];

   [[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{

    defaults =[NSUserDefaults standardUserDefaults];

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

    if (!isPro) {
        //user code here

[UIView beginAnimations:nil context:NULL];

   [UIView setAnimationDuration:1];

          [banner setAlpha:1];

    [UIView commitAnimations];


    }

    else

    {

        iadBanner.alpha=0.0;

    }  

}

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


    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:1];

    [banner setAlpha:0];

    [UIView commitAnimations];
}

-(void)fetchProducts{
    SKProductsRequest*request = [[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet
 setWithArray:@[@"testingAppPurchaseID "]]];

    request.delegate = (id)self;

    [request start];
}

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

    products = response.products;

    NSLog(@"Product was purchased");
}
-(void)request:(SKRequest*)request didFailWithError:(NSError *)error{

    NSLog(@"%@",error);

}

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

    SKPayment *payment = [SKPayment paymentWithProduct:product];

    [[SKPaymentQueue defaultQueue]addPayment:payment];
}
-(IBAction)removeAdvertisment:(id)sender
{
    SKProduct *prod = [products objectAtIndex:0];

    [self buy:prod];
}
-(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;
        }
    }

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];``
    }

@end


In Storyboard I add iAdbannerView and a button and connect button to action, and set iAdbannerView to delegate


Give me some hints how to solve this problem.

我相信有很多开发人员为什么在应用程序商店中有游戏,这意味着你们中的一些人在MyScene中实施了IN App,所以请帮助 .

回答(1)

2 years ago

你的代码看起来不错 . 你只需要运行

-(IBAction)removeAdvertisment:(id)sender

触发购买是这样的吗?您可以将objC方法调用挂钩到SKAction中,方法是将其挂在块中,如下所示:

[SKAction runBlock:^ {
            [self removeAdvertisment:self];
        }]

然后你可以指示一些节点或其他节点(可能是一个textNode或spriteNode等冒充按钮)来运行这个动作

我会提到你可能想要考虑的两个无关的事情......

  • 一整秒是从视图中删除空adBanner的相当慢的动画 . 如果我是你,我会将动画时间(至少)减半 .

  • 如果购买的目的是删除广告,那么您应该在创建adBanner实例之前检查默认值,如果用户已经付款,则不要添加一个 . 当用户进行购买时,您应该完全删除 Banner 并将其全部删除,隐藏的 Banner 除了占用RAM并可能损害您的收入之外没有任何其他用途 .