ios - 在应用程序购买 - 应用程序有时会崩溃
<p><p>我已经在互联网上浏览了几天,但找不到解决问题的方法。我创建了一个应用程序,并希望您通过应用程序内购买来删除广告。我的问题是,当我单击“购买产品”按钮时,应用程序有时会崩溃。然后我在 <code>[ addPayment:payment];</code> 行之后收到错误消息“Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x16e6f6980)”。</p>
<p>这里是我的 PurchasedViewController 的 .h:</p>
<pre><code>#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface PurchasedViewController2 : UIViewController <SKPaymentTransactionObserver, SKProductsRequestDelegate>{
NSTimer *myTimer;
int countdown;
}
@property (strong, nonatomic) SKProduct *product;
@property (strong, nonatomic) NSString *productID;
@property (strong, nonatomic) IBOutlet UILabel *productTitle;
@property (strong, nonatomic) IBOutlet UITextView *productDescription;
@property (strong, nonatomic) IBOutlet UIButton *buyButton;
@property (weak, nonatomic) IBOutlet UIButton *goBack;
@property (weak, nonatomic) IBOutlet UILabel *labelGray;
@property (weak, nonatomic) IBOutlet UILabel *labelCountdown;
- (IBAction)GoBack:(id)sender;
- (IBAction)BuyProduct:(id)sender;
- (IBAction)Restore:(id)sender;
-(void)getProductID:(UIViewController *)viewController;
-(void)UnlockPurchase;
-(void)update;
@end
</code></pre>
<p>这里是.m</p>
<pre><code>#import "PurchasedViewController2.h"
#import "ViewController.h"
@interface PurchasedViewController2 ()
@property (strong, nonatomic) ViewController *homeViewController;
@end
@implementation PurchasedViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
countdown = 5;
_labelCountdown.text = ;
_goBack.enabled = FALSE;
myTimer = ;
// Do any additional setup after loading the view.
}
-(void)update{
countdown--;
if (countdown >=0) {
_labelCountdown.text = ;
}
if (countdown <=0) {
_labelCountdown.hidden = TRUE;
_labelGray.hidden = TRUE;
_goBack.enabled = TRUE;
;
}
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using .
// Pass the selected object to the new view controller.
}
*/
- (IBAction)GoBack:(id)sender {
;
}
- (IBAction)BuyProduct:(id)sender {
SKPayment *payment = ;
[ addTransactionObserver:self];
[ addPayment:payment];
}
-(void)viewDidUnload{
[removeTransactionObserver:self];
;
}
- (IBAction)Restore:(id)sender {
[ addTransactionObserver:self];
[ restoreCompletedTransactions];
}
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
;
}
-(void)getProductID:(ViewController *)viewController{
_homeViewController = viewController;
if () {
SKProductsRequest *request = [ initWithProductIdentifiers:];
request.delegate = self;
;
}
else{
_productDescription.text = @"Please enable in app purchase in your setting";
}
}
#pragma mark _
#pragma mark SKProductsRequestDelegate
-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSArray *products = response.products;
if (products.count != 0) {
_product = products;
_buyButton.enabled = YES;
_productTitle.text = _product.localizedTitle;
_productDescription.text = _product.localizedDescription;
}
else{
_productTitle.text = @"Product Not Found";
}
products = response.invalidProductIdentifiers;
for (SKProduct *product in products) {
NSLog(@"Product not Found: %@", product);
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:;
[ finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:NSLog(@"Transaction Failed");
[ finishTransaction:transaction];
default:
break;
}
}
}
-(void)UnlockPurchase{
_buyButton.enabled = NO;
;
;
}
@end
</code></pre>
<p>这是我的观点:</p>
<pre><code>- (IBAction)PurchaseItem:(id)sender {
_purchaseController = [ initWithNibName:nil bundle:nil];
_purchaseController.productID =@"PS.PileUp.RemoveAds";
[ addTransactionObserver:_purchaseController];
;
;
}
</code></pre>
<p>非常感谢你</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>谢谢,我解决了。我将 GoBack 方法更改为:</p>
<pre><code>- (IBAction)GoBack:(id)sender {
[removeTransactionObserver:self];
;
</code></pre>
<p>}</p></p>
<p style="font-size: 20px;">关于ios - 在应用程序购买 - 应用程序有时会崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23371475/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23371475/
</a>
</p>
页:
[1]