I have a problem occuring while running and testing my app.
I've been using a tutorial offered by GeekyLemon.
Part 1: http://www.geekylemon.com/xcode-in-app-purchase-part-1
Part 2: http://www.geekylemon.com/xcode-in-app-purchase-part-2
Part 3: http://www.geekylemon.com/xcode-in-app-purchase-part-3
Part 4: http://www.geekylemon.com/xcode-in-app-purchase-part-4
The tutorial teaches you how to use in-app purchases in your app and use it to create a "remove ads" feature for your app.
After finishing the second part of the tutorial, I decided to run my code to see what I've accomplished so far. The view controller with my "Remove Ads" button shows up perfectly when I go to run my app. However, when I click on the button, it sends me to a screen that is pitch black instead of the view controller that I want it to go to.
This is a screenshot of my two view controllers in my storyboard file. The view controller on the left has my "Removes ads" button. When it is clicked, I would like for it to go to the view controller on the right.
Below are screenshots of my app in the simulator before and after I touch the "Remove Ads" button:
My SettingsViewController.h (Custom class for my first View controller in storyboard):
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <StoreKit/StoreKit.h>
#import "PurchasedViewController.h"
@interface SettingsViewController : UITableViewController < MFMailComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (weak, nonatomic) IBOutlet UILabel *Label;
- (IBAction)PurchaseItem:(id)sender;
@property (strong, nonatomic) PurchasedViewController *purchaseController;
-(void)Purchased;
@end
My SettingsViewController.m:
#import "SettingsViewController.h"
#import "SWRevealViewController.h"
#define k_Save @"Saveitem"
@interface SettingsViewController ()
@property (nonatomic, strong) NSArray *menuItems;
@end
@implementation SettingsViewController
@synthesize Label;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
_sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.7f];
NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
bool saved = [saveapp boolForKey:k_Save];
if (!saved) {
/// not save code here
} else {
///saved code here
Label.text = @"Item has been purchased.";
// Add pan gesture to hide the sidebar
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
// Do any additional setup after loading the view.
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0 && indexPath.section == 3){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://howtocrackthedbq.blogspot.com/"]];
} else if(indexPath.row == 1 && indexPath.section == 1){
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"[How to crack the DBQ] Support request"];
[mailViewController setToRecipients:[NSArray arrayWithObject:@"moappsco@gmail.com"]];
[self presentModalViewController:mailViewController animated:YES];
}
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)Purchased {
Label.text = @"Item has been purchased.";
NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
[saveapp setBool:TRUE forKey:k_Save];
[saveapp synchronize];
}
- (IBAction)PurchaseItem:(id)sender {
_purchaseController = [[PurchasedViewController alloc] initWithNibName:nil bundle:nil];
_purchaseController.productID = @"com.moappsco.crackingthedbq.dbq1";
[[SKPaymentQueue defaultQueue] addTransactionObserver:_purchaseController];
[self presentViewController:_purchaseController animated:YES completion:NULL];
[_purchaseController getProductID:self];
}
@end
My PurchasedViewController.h (Custom Class for my other view controller shown on the right side of my storyboard):
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface PurchasedViewController : UIViewController<SKPaymentTransactionObserver,SKProductsRequestDelegate> {
}
@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;
- (IBAction)Restore:(id)sender;
- (IBAction)BuyProduct:(id)sender;
- (IBAction)GoBack:(id)sender;
-(void)getProductID:(UIViewController *)viewController;
@end
My PurchasedViewController.m:
#import "PurchasedViewController.h"
#import "SettingsViewController.h"
@interface PurchasedViewController ()
@property (strong, nonatomic) SettingsViewController *homeViewController;
@end
@implementation PurchasedViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_buyButton.enabled = NO;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Restore:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (IBAction)BuyProduct:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:_product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction)GoBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
[self UnlockPurchase];
}
-(void)getProductID:(SettingsViewController *)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 purchases in your settings.";
}
#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
If needed, you can download my xcode project on Github: https://github.com/MoAppsCo/iOS-Apps/tree/master/How%20to%20crack%20the%20DBQ
You're not instantiating your controller properly. When the controller is in a storyboard, you shouldn't use initWithNibName:bundle: (that's for xib based controllers), you should give your controller an identifier in the storyboard, and use:
_purchaseController = [self.storyboard instantiateViewControllerWithIdentifier:@"YourNameHere"];
Of course, you could also do it by connecting up a segue to that controller, from the settings controller, and use performSegueWithIdentifier:sender: instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.