Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 应用内购买代码的奇怪崩溃报告_Ios_Crash_In App Purchase_Crash Reports_Crashlytics - Fatal编程技术网

Ios 应用内购买代码的奇怪崩溃报告

Ios 应用内购买代码的奇怪崩溃报告,ios,crash,in-app-purchase,crash-reports,crashlytics,Ios,Crash,In App Purchase,Crash Reports,Crashlytics,我有以下代码将应用程序内购买添加到应用程序中。所有这些都在我的设备上完美运行。但是,当我向应用程序添加Crashlytics支持时,我每天都会收到数百份崩溃报告。为什么?我真的不明白所提供的代码有什么问题 #define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification" #define kInAppPurchaseManagerTransacti

我有以下代码将应用程序内购买添加到应用程序中。所有这些都在我的设备上完美运行。但是,当我向应用程序添加
Crashlytics
支持时,我每天都会收到数百份崩溃报告。为什么?我真的不明白所提供的代码有什么问题

#define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification"
#define kInAppPurchaseManagerTransactionFailedNotification @"kInAppPurchaseManagerTransactionFailedNotification"
#define kInAppPurchaseManagerTransactionSucceededNotification @"kInAppPurchaseManagerTransactionSucceededNotification"
#define gFullVersion @"%@.FullVersion"

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self loadStore];
    }
}

- (NSString*)getProductId:(NSString*)feature {
    NSBundle *bundle = [NSBundle mainBundle];
    NSDictionary *info = [bundle infoDictionary];
    NSString *bundleIdentifier = [info objectForKey: @"CFBundleIdentifier"];
    return bundleIdentifier;
}

- (void)requestProducts:(NSString*)feature {
    NSSet *productIdentifiers = [NSSet setWithObject:[self getProductId:feature]];
    if ([feature isEqualToString:gFullVersion]) {
        if (productFullVersionRequest) {
            [productFullVersionRequest release];
            productFullVersionRequest = nil;
        }
        productFullVersionRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productFullVersionRequest.delegate = self;
        [productFullVersionRequest start];
        // we will release the request object in the delegate callback
    }
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [products addObjectsFromArray:response.products];
    for (SKProduct *product in response.products) {
        if (product && [product.productIdentifier isEqualToString:[self getProductId:gFullVersion]]) {
            // finally release the reqest we alloc/init’ed in requestCompilations
            [productFullVersionRequest release];
            productFullVersionRequest = nil;
        }
    }

    for (NSString *invalidProductId in response.invalidProductIdentifiers) {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

// call this method once on startup
- (void)loadStore {
    // restarts any purchases if they were interrupted last time the app was open
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    // get the product description (defined in early sections)
    [self requestProducts:gFullVersion];
}

// call this before making a purchase
- (BOOL)canMakePurchases {
    return [SKPaymentQueue canMakePayments];
}

// kick off the upgrade transaction
- (void)purchaseProduct:(NSString*)feature {
    bool ok = false;
    for (SKProduct *product in products) {
        if ([product.productIdentifier isEqualToString:[self getProductId:feature]]) {
            SKPayment *payment = [SKPayment paymentWithProduct:product];
            if (payment) {
                [[SKPaymentQueue defaultQueue] addPayment:payment];
                break;
            }
        }
    }
}

// saves a record of the transaction by storing the receipt to disk
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
    if ([transaction.payment.productIdentifier isEqualToString:[self getProductId:gFullVersion]]) {
        // save the transaction receipt to disk
        [[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:[self getProductId:gFullVersion]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

// enable pro features
- (bool)provideContent:(NSString *)productId {
    if ([productId isEqualToString:[self getProductId:gFullVersion]]) {
        // ...provide content here...
        return true;
    }
    return false;
}

// removes the transaction from the queue and posts a notification with the transaction result
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful {
    // remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];
    if (wasSuccessful) {
        // send out a notification that we’ve finished the transaction
        [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo];
    } else {
        // send out a notification for the failed transaction
        [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo];
    }
}

// called when the transaction was successful
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    [self recordTransaction:transaction];
    bool provided = [self provideContent:transaction.payment.productIdentifier];
    [self finishTransaction:transaction wasSuccessful:YES];
}

// called when a transaction has been restored and and successfully completed
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    [self recordTransaction:transaction.originalTransaction];
    [self provideContent:transaction.originalTransaction.payment.productIdentifier];
    [self finishTransaction:transaction wasSuccessful:YES];
}

// called when a transaction has failed
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    if (transaction.error.code != SKErrorPaymentCancelled) {
        // error!
        [self finishTransaction:transaction wasSuccessful:NO];
        [self showAlert:NSLocalizedString(@"InAppPurchase", @"") alertStr:[transaction.error localizedDescription]];
    } else {
        // this is fine, the user just cancelled, so don’t notify
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }
}

// called when the transaction status is updated
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

- (IBAction)processFullVersion:(id)sender {
    if ([self canMakePurchases]) {
        [self purchaseProduct:gFullVersion];
    } else {
        [self showAlert:NSLocalizedString(@"InAppPurchase", @"") alertStr:NSLocalizedString(@"CanNotMakePurchases", @"")];
    }
}

- (IBAction)restoreCompletedTransactions:(id)sender {
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
事故报告:

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0xa1c57ae2
Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x3bf5e5d0 objc_msgSend + 15
1  StoreKit                       0x360260a7 __NotifyObserverAboutChanges + 66
2  CoreFoundation                 0x341aeacd CFArrayApplyFunction + 176
3  StoreKit                       0x36026055 -[SKPaymentQueue _notifyObserversAboutChanges:sendUpdatedDownloads:] + 128
4  StoreKit                       0x36024bc9 -[SKPaymentQueue addPayment:] + 464
5  MyApplication                  0x000a76d7 -[CalendarView purchaseProduct:] (CalendarView.m:952)
6  MyApplication                  0x000a8c17 -[CalendarView processFullVersion:] (CalendarView.m:1107)
7  MyApplication                  0x000a361b -[CalendarView clickHandler:] (CalendarView.m:458)
8  MyApplication                  0x000a55cf -[CalendarView liteVersionDisplayAlert] (CalendarView.m:671)
9  MyApplication                  0x000a5d2f -[CalendarView toggleView:] (CalendarView.m:735)
10 MyApplication                  0x000a625f -[CalendarView modeButtonPressed] (CalendarView.m:795)
11 MyApplication                  0x000c245f -[ToolbarView modePressed] (ToolbarView.m:304)
12 UIKit                          0x36162087 -[UIApplication sendAction:to:from:forEvent:] + 70
13 UIKit                          0x3616203b -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 30
14 UIKit                          0x36162015 -[UIControl sendAction:to:forEvent:] + 44
15 UIKit                          0x361618cb -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 502
16 UIKit                          0x36161db9 -[UIControl touchesEnded:withEvent:] + 488
17 UIKit                          0x3608a5f9 -[UIWindow _sendTouchesForEvent:] + 524
18 UIKit                          0x360778e1 -[UIApplication sendEvent:] + 380
19 UIKit                          0x360771ef _UIApplicationHandleEvent + 6198
20 GraphicsServices               0x37d8f5f7 _PurpleEventCallback + 590
21 GraphicsServices               0x37d8f227 PurpleEventCallback + 34
22 CoreFoundation                 0x3423d3e7 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
23 CoreFoundation                 0x3423d38b __CFRunLoopDoSource1 + 138
24 CoreFoundation                 0x3423c20f __CFRunLoopRun + 1382
25 CoreFoundation                 0x341af23d CFRunLoopRunSpecific + 356
26 CoreFoundation                 0x341af0c9 CFRunLoopRunInMode + 104
27 GraphicsServices               0x37d8e33b GSEventRunModal + 74
28 UIKit                          0x360cb2b9 UIApplicationMain + 1120
29 MyApplication                  0x0007daff main (main.m:13)

非常感谢你的帮助

看起来您正在使用键值。我在猜测应用程序何时运行此部分

- (void)purchaseProduct:(NSString*)feature {
    bool ok = false;
    for (SKProduct *product in products) {
        if ([product.productIdentifier isEqualToString:[self getProductId:feature]]) {
            SKPayment *payment = [SKPayment paymentWithProduct:product];
            if (payment) {
                [[SKPaymentQueue defaultQueue] addPayment:payment];
                break;
            }
        }
    }
}
并且成功地添加了一笔付款,用于更新不存在的观察者,从而导致内存访问错误


我会检查[[SKPaymentQueue defaultQueue]addTransactionObserver:self]

您添加到付款队列的观察者似乎已被解除分配。您可以调用
SKPaymentQueue addTransactionObserver:
,但从未调用
SKPaymentQueue removetTransactionObserver:

dealloc
方法添加到视图控制器:

- (void)dealloc {
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
最有可能的是,你的应用程序显示了一个视图控制器,它将自己添加为观察者。然后该视图控制器被解除(但仍标记为观察者)。稍后,您将显示控制器的另一个实例并执行一些支付事务。然后,支付队列尝试通知新旧观察者。崩溃的发生是因为旧的观察者早就被释放了


通过添加上述
dealloc
方法,此问题将得到解决。

SKPaymentQueue是否使用KVO进行更新?无论如何,我没有意识到它是苹果图书馆的一部分。。我还以为他自己实现了addTransactionObserver:@Apollo啊,你仔细查看了我的答案,想找到一些让自己感觉更好的方法吗?不是KVO,而是iOS框架。我也明白我错在哪里了。Caleb当然提供了一个更好的解决方案,我理解我的错误,这就是为什么我删除了我的帖子。我很抱歉伤害了你的感情,但你这么做太小气了。下一步你打算用谷歌搜索我的用户名吗?@Apollo我们在这里谈论的是性格天才。你用谷歌搜索过我的名字了吗?你们喜欢到处去投票否决我的答案吗?但我的应用程序一次只创建一个视图(观察者)。崩溃是否发生在下一次启动应用程序时?