Combine Buy/Restore in app purchase button - iphone

in our app I have a purchase button & a restore button however I would like to make it so that instead of 2 seperate buttons I can just use one, which then prompts what to do.
Would anyone be able to assist in editing the code below so that when the user clicks the button, a UIAlert pops up with one button to say Upgrade, one to say Restore, the other Cancel.
Here is the current code used to buy / restore;
-(IBAction) actionRemoveAds
{
[[MKStoreManager sharedManager] buyFeatureA];
}
-(IBAction) actionRestore
{
[MKStoreManager sharedManager];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==111) {
if (buttonIndex==0) {
[[MKStoreManager sharedManager]buyFeatureA];
}
}
}
Many thanks,
Chris

Related

Detect Cancel Button Tap for "Confirm Your In-App Purchase" UIAlert

I implemented in-app purchase based on this tutorial. The problem I experience is that I cannot detect when Cancel button is pressed on the "Confirm Your In-App Purchase" alert, which is a part of StoreKit framework.
Some sources suggest -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions is called when Cancel is pressed but in my case it never runs. My set up is the ViewController which imports IAPManager:NSObject class which conforms to SKProductRequestDelegate and SKPaymentTransactionObserver. Product is successfully being requested but transaction observer is never calling paymentQueue.
How can I make it work so I can detect Cancel button?
in delegate method i look at the tutorial failedtransaction does nothing if user cancels. but you can add it like that.
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled)
{
// error!
NSLog(#"Something went Wrong!");
[self finishTransaction:transaction wasSuccessful:NO];
NSLog(#"transaction error :%#", transaction.error.localizedDescription);
}
else
{
NSLog(#"Cancelled");
// this is fine, the user just cancelled
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
This line had to be added to make it work:
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
Thanks all for your help.
I haven't used StoreKit, but I'm guessing that your SKRequestDelegate will receive the message request:didFailWithError: if the user cancels.

How to create UIAlertView with multiple options?

I am trying to work out how I can use a UIAlertView to do more than one command.
Basically, in my ViewController, there is already an alertView, however I am now adding some storekit files, which, require there own alertView (to tell it whether to purchase the in-app or cancel etc.
Here is original alertView code;
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
if (buttonIndex == 1) {
[g_GameUtils removeAlbumFolder:deleteIndex];
[g_GameUtils readAllData];
[g_GameUtils getAlbumFolderList];
[m_pTable reloadData];
}
}
And here is what I need also - they are both called alertView so I cannot use both like this, is there a way to combine them? Or is it better to call one of them alertView2 ? If so, how does it know which one to call for the particular alert?
Thanks in advance!
Chris
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
//cancel button clicked. Do something here or nothing here
}
else{
//other button indexes clicked
[[MKStoreManager sharedManager] buyFeature:#"com.davis.apptoken.buy"];
}
}
Also you can use alertView.tag = 1; and alertView2.tag = 2; and add appropriate conditions to delegate:
if (alertView.tag == 1)
{
// First alert
}

Simple question on UIAlertView and UIAlertViewDelegate

I hope this is pretty straight-forward. As you'll see by my code, I'm simply trying to get a UIAlertView button-press to pop me back to the root view.
I don't get any compile errors or warnings, and when I run the app, the "RedeemCoupon" method is called in the IBAction, and the UIAlertView pops up as it should, but it doesn't seem the "doneRedeeming" method gets called at all - I don't see anything from NSLog (yes I'm aware that I am setting buttonIndex to 0 - once I get this working I'll fix it). So, basically it doesn't work. I click the "cancel" button and the alert just goes away.
By the way I'm not sure if this matters, but this "RedeemCouponViewController" view is number 4 on the stack, and it was added by use of presentModalViewController in the previous view.
I'm open to other ways of doing this if needed - all suggestions welcome!
Thanks in advance!
// RedeemCouponViewController.h
#interface RedeemCouponViewController : UIViewController <UIAlertViewDelegate> {
// RedeemCouponViewController.m
- (IBAction) redeemYes: (UIButton*) sender {
CouponRedeem *redeem = [[CouponDatabase database] couponRedeem:_uniqueId];
[redeem release];
UIAlertView *doneRedeeming = [[UIAlertView alloc]initWithTitle:#"Coupon Redeemed!"
message:#"Thanks for shopping!"
delegate:self
cancelButtonTitle:#"Back to Main Menu"
otherButtonTitles:nil];
[doneRedeeming show];
[doneRedeeming release];
}
-(void) doneRedeeming: (UIAlertView *) doneRedeeming clickedButtonAtIndex: (NSInteger)buttonIndex {
if (buttonIndex = 0) {
NSLog(#"doneRedeemed method called");
[self.navigationController popToRootViewControllerAnimated:YES];
} else {
//do nothing
}
}
You want to have
if (buttonIndex == 0) {
in place of
if (buttonIndex = 0) {
The former checks for equality whereas the latter assigns.
Also, you want to have
– alertView:clickedButtonAtIndex:
where you have
- doneRedeeming:clickedButtonAtIndex:
You need to use UIAlertViewDelegate methods:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {}
not
-(void) doneRedeeming: (UIAlertView *) doneRedeeming clickedButtonAtIndex: (NSInteger)buttonIndex {}
Use the delegate method -alertView:didDismissWithButtonIndex: to listen for your cancel button index
#PengOne's answer is correct: your problem is this:
if (buttonIndex = 0) {
You said
I know it's not correct, but I just
wanted to be sure the statement was
true for now...
But buttonIndex = 0 evaluates to 0, making it equivalent to
if (0)
The code within that block will never execute, regardless of the value of buttonIndex. If you really want to do it unconditionally, change the if to if( 1 ), or just take the if out.
This would have been trivial to spot if you ran this code in the debugger. You might think you know what your code is doing, but if you don't watch it run, you don't.

Alert View - How to use clickedButtonAtIndex:

I have this alert view (disclaimer) that pop up when app finish launching. It works (my app is much slower now), but I also want to exit from the app if the user press no, thanks. I think I should use clickedButtonAtIndex:.
1. Can somebody help me on this?
2. is viewDidLoad the best method to fire the alertView when the application start?
3. is there any reason why now my app take more time to start when I build and run it?
-(void)viewDidLoad {
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle: #"DISCLAIMER" message:#"This Application is provided without any express or implied warranty. Errors or omissions in either the software or the data are not guaranteed against. The application is not intented to replace official documentation or operational procedure. In no event shal the developer be held liable for any direct or indirect damages arising from the use of this application" delegate:self cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Accept", nil];
[disclaimer show];
[disclaimer release];
[super viewDidLoad];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
// Do something
else
// Some code
}
or
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
// Do something
else
// Some code
}
Make sure your class conforms to the UIAlertViewDelegate protocol.
And I dont think exiting from the app is a good approach though.You should only let the user close the application by pressing the home button. It's against the default behavior which the user expects every app to follow.
Try this magic:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.cancelButtonIndex == buttonIndex){
// Do cancel
}
else{
// Do the real thing
}
}

- (void)alertViewCancel:(UIAlertView *)alertView is not called

I've got the problem that the UIAlertViewDelegate method - (void)alertViewCancel:(UIAlertView *)alertView is not called when I cancel a AlertView with it's cancel button.
Weird is that the delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works perfectly.
Does anyone have an idea?
Thanks in advance
Sean
- (void)alertViewCancel:(UIAlertView *)alertView
{
if(![self aBooleanMethod])
{
exit(0);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
}
I call this when a button is clicked:
- (void)ImagePickDone
{
UIAlertView *alertDone = [[UIAlertView alloc]
initWithTitle:#"Done"
message:#"Are u sure?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: #"Yes", nil];
[alertDone show];
[alertDone release];
}
The alertViewCancel is used for when the system dismisses your alert view, not when the user presses the "Cancel" button. Quote from apple docs:
Optionally, you can implement the
alertViewCancel: method to take the
appropriate action when the system
cancels your alert view. If the
delegate does not implement this
method, the default behavior is to
simulate the user clicking the cancel
button and closing the view.
If you want to capture when the user presses the "Cancel" button you should use the clickedButtonAtIndex method and check that the index corresponds to the index for the cancel button. To obtain this index use:
index = alertDone.cancelButtonIndex;
You can handle the Cancel at the index 0 of this delegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
//cancel button clicked. Do something here.
}
else{
//other button indexes clicked
}
}
This can be improved in two ways. First, it only handles the case that the user actually clicked a button. It doesn't handle the situation that [myAlert dismissWithClickedButtonIndex:] is called, or that the alert is dismissed in some other way. Second, button 0 is not necessarily the cancel button. In an alert with two buttons, the left one is at index 0, and the right one is at index 1. If you changed the titles so that the right button says "Cancel", then button 1 is logically the Cancel button. Instead of "willDismiss" you can implement "didDismiss" which will be called after the dialog has disappeared and not before.
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == alertView.cancelButtonIndex)
{
//cancel button clicked. Do something here.
}
else
{
//other button indexes clicked
}
}