Hallo zusammen,
ich durchforste bereits tagelang das Internet und finde keine Lösung für mein Problem. Ich habe eine App erstellt und möchte die Werbung durch einen In App Kauf entfernen lassen. Mein Problem ist, das die App manchmal abstürzt wenn ich auf den Button "BuyProduct" klicke. Ich erhalte dann die Fehlermeldung "Thread 1: EXC_BAD_ACCESS (code=1, address = 0x16e6f6980)" hinter der Zeile .
Hier mein code in der .h Datei meines PurchasedViewController:
Alles anzeigen
hier die entsprechende .m
Alles anzeigen
und hier der Aufruf des Views:
Vielen dank & Grüße
ich durchforste bereits tagelang das Internet und finde keine Lösung für mein Problem. Ich habe eine App erstellt und möchte die Werbung durch einen In App Kauf entfernen lassen. Mein Problem ist, das die App manchmal abstürzt wenn ich auf den Button "BuyProduct" klicke. Ich erhalte dann die Fehlermeldung "Thread 1: EXC_BAD_ACCESS (code=1, address = 0x16e6f6980)" hinter der Zeile .
Hier mein code in der .h Datei meines PurchasedViewController:
Quellcode
- #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
hier die entsprechende .m
Quellcode
- #import "PurchasedViewController2.h"
- #import "ViewController.h"
- @interface PurchasedViewController2 ()
- @property (strong, nonatomic) ViewController *homeViewController;
- @end
- @implementation PurchasedViewController2
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- countdown = 5;
- _labelCountdown.text = [NSString stringWithFormat:@"%i",countdown];
- _goBack.enabled = FALSE;
- myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
- // Do any additional setup after loading the view.
- }
- -(void)update{
- countdown--;
- if (countdown >=0) {
- _labelCountdown.text = [NSString stringWithFormat:@"%i",countdown];
- }
- if (countdown <=0) {
- _labelCountdown.hidden = TRUE;
- _labelGray.hidden = TRUE;
- _goBack.enabled = TRUE;
- [myTimer invalidate];
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super 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 [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- - (IBAction)GoBack:(id)sender {
- [self dismissViewControllerAnimated:YES completion:NULL];
- }
- - (IBAction)BuyProduct:(id)sender {
- SKPayment *payment = [SKPayment paymentWithProduct:_product];
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- [[SKPaymentQueue defaultQueue] addPayment:payment];
- }
- -(void)viewDidUnload{
- [[SKPaymentQueue defaultQueue]removeTransactionObserver:self];
- [super viewDidUnload];
- }
- - (IBAction)Restore:(id)sender {
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
- }
- -(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
- [self UnlockPurchase];
- }
- -(void)getProductID:(ViewController *)viewController{
- _homeViewController = viewController;
- if ([SKPaymentQueue canMakePayments]) {
- SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.productID]];
- request.delegate = self;
- [request start];
- }
- 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[0];
- _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:[self UnlockPurchase];
- [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
- break;
- case SKPaymentTransactionStateFailed:NSLog(@"Transaction Failed");
- [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
- default:
- break;
- }
- }
- }
- -(void)UnlockPurchase{
- _buyButton.enabled = NO;
- [_buyButton setTitle:@"Purchased" forState:UIControlStateDisabled];
- [_homeViewController Purchased];
- }
- @end
und hier der Aufruf des Views:
Quellcode
- - (IBAction)PurchaseItem:(id)sender {
- _purchaseController = [[PurchasedViewController2 alloc] initWithNibName:nil bundle:nil];
- _purchaseController.productID =@"PS.PileUp.RemoveAds";
- [[SKPaymentQueue defaultQueue] addTransactionObserver:_purchaseController];
- [self presentViewController:_purchaseController animated:YES completion:NULL];
- [_purchaseController getProductID:self];
- }
Vielen dank & Grüße