2014-11-01 107 views
0

我需要您的建議,使用「在應用程序購買」刪除iAd。 我有「In App Purchase」的工作代碼。但我不知道如何實現它(IOS)SpriteKit遊戲。當我在View Controller中實現它時工作得很好,但我需要它在MyScene.m中工作,所以當我點擊「-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event」方法中的「購買」警報窗口時,會彈出並讓玩家購買它In App購買集成SpriteKit

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 Purchase,所以請幫助。

+0

「如果你想看代碼,讓我知道」......不,我們**有**看代碼。如果你的代碼有問題,你應該發佈你的代碼,我們還有什麼建議改進和指出問題的? – LearnCocos2D 2014-11-01 10:35:05

回答

0

你的代碼對我來說確實很好。你只需要運行

-(IBAction)removeAdvertisment:(id)sender 

來觸發購買是這樣嗎? 可以通過在塊掩埋它像這樣鉤的objC方法調用到一個SKAction:

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

那麼你可以指示一些節點或其它(也許是一個textNode或spriteNode等冒充一個按鈕)來運行這個動作

我會提到你可能要考慮兩個不相干的東西...

  1. 一秒鐘是一個相當緩慢的動畫從視圖中刪除空adBanner。如果我是你,我會將動畫時間縮短一半(至少)。

  2. 如果購買是爲了移除廣告,那麼您應該在創建adBanner實例之前檢查默認設置,並且如果用戶已經付款,則不要添加。當用戶進行購買時,您應該徹底刪除橫幅並將其刪除,隱藏的橫幅除了綁定內存並且可能會損害您的收入外,沒有任何其他用途。

+0

感謝Jef的回覆,我非常感謝,但是問題一直沒有得到答覆,我喜歡你的建議,儘管減少了amimation時間。我忙於實施這種方法,甚至沒有意識到我可以這樣做,我認爲這是代碼的運行方式:),而不是真正的程序員經驗。應該有一些解決方案。也許我可以調用一些像xib這樣的輔助視圖控制器,並且所有代碼都會在xib或其他視圖控制器中,當我到達該視圖控制器時,我可以保證購買按鈕? – 2014-11-02 07:02:19