Objective c 调用canMakePayments后,应用内购买冻结并崩溃我的应用

Objective c 调用canMakePayments后,应用内购买冻结并崩溃我的应用,objective-c,in-app-purchase,sprite-kit,selector,Objective C,In App Purchase,Sprite Kit,Selector,我是业余爱好者。当按下BUY按钮时,代码运行良好。我使用断点来选择有问题的行,请参见下文,直到调用canMakePayments。负责IAP的场景包括游戏场景、InAppManager、InAppObserver或PurchaseProduct;下面提供了所有相关代码 我的应用程序在休息后崩溃。在突破线之前,一切正常。这是InAppManager.m的内部。向下滚动查看InAppManager.m中的所有代码。应用程序崩溃后,IAP会继续显示alertView,要求我购买。单击“购买”后,ale

我是业余爱好者。当按下BUY按钮时,代码运行良好。我使用断点来选择有问题的行,请参见下文,直到调用canMakePayments。负责IAP的场景包括游戏场景、InAppManager、InAppObserver或PurchaseProduct;下面提供了所有相关代码

我的应用程序在休息后崩溃。在突破线之前,一切正常。这是InAppManager.m的内部。向下滚动查看InAppManager.m中的所有代码。应用程序崩溃后,IAP会继续显示alertView,要求我购买。单击“购买”后,alertView将消失,应用程序不会响应任何触摸。当我关闭并打开应用程序时,它表明我已经购买了该产品,并且从那里一切正常。我分享了所有的信息,提前谢谢

有人能告诉我怎么走吗

-(void) buyFeature:(NSString*) featureID {

    if ([SKPaymentQueue canMakePayments]) {
        NSLog(@"Can make payments");
        SKProduct* selectedProduct;

        for (int i=0; i < [purchaseableProducts count]; i++) {
            selectedProduct = [purchaseableProducts objectAtIndex:i];

            if ([[selectedProduct productIdentifier] isEqualToString:featureID]) {

                // if we found a SKProduct in the purchaseableProducts array with the same ID as the one we want to buy, we proceed by putting it in the payment queue.

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

                NSLog(@"Proceeding by putting the product in the payment queue");

                break;

            }
            else {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"You can't purchase from the App Store" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];

            }

        }
    }
}
这就是InAppPurchase.m中的整个代码的样子

@interface InAppManager () {

    NSMutableArray* purchaseableProducts; // an array of possible products to purchase
    NSUserDefaults* defaults; // store a bool variable marking products that have been unlocked
    bool product1WasPurchased; // YES or NO

    InAppObserver* theObserver;

}

@end

@implementation InAppManager

static NSString* productID1 = @"MyProduct";


static InAppManager* sharedManager = nil;

+(InAppManager*) sharedManager {

    if(sharedManager == nil) {

        sharedManager = [[InAppManager alloc] init];

    }

    return sharedManager;
}

-(id) init {

    if  ((self = [super init])) {

        //do initialization

        sharedManager = self;
        defaults = [NSUserDefaults standardUserDefaults]; 
        product1WasPurchased = [defaults boolForKey:productID1]; 


        purchaseableProducts = [[NSMutableArray alloc] init];
        [self requestProductData]; 

        theObserver = [[InAppObserver alloc] init];
        [[SKPaymentQueue defaultQueue] addTransactionObserver:theObserver];
    }

    return self;

}

-(void) requestProductData {

    SKProductsRequest* request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:productID1, nil]]; 

    request.delegate = self;
    [request start];
}


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



    NSArray* skProducts = response.products; 

    if ( [skProducts count] != 0 && [purchaseableProducts count] == 0) {

        for (int i = 0; i < [skProducts count]; i++) {

            [purchaseableProducts addObject:[skProducts objectAtIndex:i]];
            SKProduct* product = [purchaseableProducts objectAtIndex:i];

            NSLog(@"Feature: %@, Cost: %f, ID: %@", [product localizedTitle], [[product price] doubleValue], [product productIdentifier] );

        }

    }

    NSLog(@" We found %lu In-App Purchases in iTunes Connect", (unsigned long)[purchaseableProducts count]);

}


-(void) failedTransaction:(SKPaymentTransaction*) transaction{

    NSString* failMessage = [NSString stringWithFormat:@"Reason: %@, You can try: %@", [transaction.error localizedFailureReason], [transaction.error localizedRecoverySuggestion]  ];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to complete your purchase" message:failMessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];


}
-(void) provideContent:(NSString*) productIdentifier{


    NSNotificationCenter* notification = [NSNotificationCenter defaultCenter]; 

    if ( [productIdentifier isEqualToString:productID1]) {
        product1WasPurchased = YES;
        [defaults setBool:YES forKey:productID1];
        [notification postNotificationName:@"feature1Purchased" object:nil];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you!" message:@"You have purchased MyProduct" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    }
    else {
        NSLog(@"Error: Something happened!");
    }


}


-(void) buyFeature1 {

    [self buyFeature:productID1];

}

-(void) buyFeature:(NSString*) featureID {

    if ([SKPaymentQueue canMakePayments]) {
        NSLog(@"Can make payments");
        SKProduct* selectedProduct;

        for (int i=0; i < [purchaseableProducts count]; i++) {
            selectedProduct = [purchaseableProducts objectAtIndex:i];

            if ([[selectedProduct productIdentifier] isEqualToString:featureID]) {

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

                NSLog(@"Proceeding by putting the product in the payment queue");

                break;

            }
            else {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oh no" message:@"You can't purchase from the App Store" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];

            }

        }
    }
}

-(bool) isFeature1PurchasedAlready {

    return product1WasPurchased;
}


-(void) restoreCompletedTransactions{

    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

}
我在游戏场景中有这样几行代码,从这里开始,我只选择了相关的代码

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

[InAppManager sharedManager];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unlockProduct1) name:@"feature1Purchased" object:nil];
}

-(void) unlockProduct1 {

    NSLog(@"this class knows we purchased the product!");

}
我在ORPurchaseProduct.m中有这些行。这里我有购买按钮,它检查产品是否已经购买。我从游戏场景过渡到这个

-(void)didMoveToView:(SKView *)view
{
[InAppManager sharedManager];

if ( [[InAppManager sharedManager] isFeature1PurchasedAlready] == NO) {
    NSLog(@" product was not bought yet");
    [self createBuyButton:@"BUY"];

} else {
    NSLog(@" product was bought");
    [self createBuyButton:alreadyBoughtMessage];
}
}

-(void) createBuyButton:(NSString*) theMessage {

    UIFont* theFont = [UIFont fontWithName:@"BUY" size:18];

    thePurchaseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    if( [theMessage isEqualToString:alreadyBoughtMessage]) {

        [thePurchaseButton addTarget:self action:@selector(doNothing) forControlEvents:UIControlEventTouchUpInside];

        NSLog(@"Already bought message: we are here");

    } else {

        [thePurchaseButton addTarget:[InAppManager sharedManager] action:@selector(buyFeature1) forControlEvents:UIControlEventTouchUpInside];
    }

    [thePurchaseButton.titleLabel setFont:theFont];
    [thePurchaseButton setTitle:theMessage forState:UIControlStateNormal];
    [thePurchaseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [thePurchaseButton setBackgroundColor:[UIColor blackColor]];
    thePurchaseButton.frame = CGRectMake(80, 380, 160, 30);
    [self.view addSubview:thePurchaseButton];      
}

-(void)doNothing
{
    NSLog(@"Nothing to be done because the product is already bought!");
}

我注释掉了这行代码,一切都解决了,这是在AppDelegate的applicationWillResignActive方法中。谢谢大家

SKView *view = (SKView *)self.window.rootViewController.view;
((GameScene *)view.scene).gamePaused = YES;

崩溃日志建议您尝试调用setGamePaused:或尝试在ORPurchaseProduct实例上设置名为gamePaused的变量。尝试添加一个异常断点,并查看异常抛出的位置。@AMI289我在AppDelegate.mSKView*view=SKView*self.window.rootViewController.view的应用程序willresignactive中有此代码;GameSecene*view.scene.gamePaused=是;我在游戏场景中也有它,你可以从台词中看到。也许问题就出在这里,但我想不出问题所在。
-(void)didMoveToView:(SKView *)view
{
[InAppManager sharedManager];

if ( [[InAppManager sharedManager] isFeature1PurchasedAlready] == NO) {
    NSLog(@" product was not bought yet");
    [self createBuyButton:@"BUY"];

} else {
    NSLog(@" product was bought");
    [self createBuyButton:alreadyBoughtMessage];
}
}

-(void) createBuyButton:(NSString*) theMessage {

    UIFont* theFont = [UIFont fontWithName:@"BUY" size:18];

    thePurchaseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    if( [theMessage isEqualToString:alreadyBoughtMessage]) {

        [thePurchaseButton addTarget:self action:@selector(doNothing) forControlEvents:UIControlEventTouchUpInside];

        NSLog(@"Already bought message: we are here");

    } else {

        [thePurchaseButton addTarget:[InAppManager sharedManager] action:@selector(buyFeature1) forControlEvents:UIControlEventTouchUpInside];
    }

    [thePurchaseButton.titleLabel setFont:theFont];
    [thePurchaseButton setTitle:theMessage forState:UIControlStateNormal];
    [thePurchaseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [thePurchaseButton setBackgroundColor:[UIColor blackColor]];
    thePurchaseButton.frame = CGRectMake(80, 380, 160, 30);
    [self.view addSubview:thePurchaseButton];      
}

-(void)doNothing
{
    NSLog(@"Nothing to be done because the product is already bought!");
}
SKView *view = (SKView *)self.window.rootViewController.view;
((GameScene *)view.scene).gamePaused = YES;