2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-09-21 14:49:57 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2014-09-21 14:49:57 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2014-09-21 14:49:57 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
#import "MPAppDelegate_InApp.h"
|
2014-09-24 05:07:02 +00:00
|
|
|
|
|
|
|
@interface MPAppDelegate_Shared(InApp_Private)<SKProductsRequestDelegate, SKPaymentTransactionObserver>
|
|
|
|
@end
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
@implementation MPAppDelegate_Shared(InApp)
|
|
|
|
|
|
|
|
PearlAssociatedObjectProperty( NSArray*, Products, products );
|
2014-09-26 04:32:07 +00:00
|
|
|
PearlAssociatedObjectProperty( NSMutableArray*, ProductObservers, productObservers );
|
2014-09-21 14:49:57 +00:00
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (void)registerProductsObserver:(id<MPInAppDelegate>)delegate {
|
|
|
|
|
|
|
|
if (!self.productObservers)
|
|
|
|
self.productObservers = [NSMutableArray array];
|
|
|
|
[self.productObservers addObject:delegate];
|
|
|
|
|
|
|
|
if (self.products)
|
|
|
|
[delegate updateWithProducts:self.products];
|
|
|
|
else
|
|
|
|
[self reloadProducts];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeProductsObserver:(id<MPInAppDelegate>)delegate {
|
|
|
|
|
|
|
|
[self.productObservers removeObject:delegate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reloadProducts {
|
2014-09-21 14:49:57 +00:00
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:
|
2016-01-14 15:03:30 +00:00
|
|
|
[[NSSet alloc] initWithObjects:MPProductGenerateLogins, MPProductGenerateAnswers, MPProductTouchID, MPProductFuel, nil]];
|
2014-09-24 05:07:02 +00:00
|
|
|
productsRequest.delegate = self;
|
|
|
|
[productsRequest start];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SKPaymentQueue *)paymentQueue {
|
|
|
|
|
2014-09-21 14:49:57 +00:00
|
|
|
static dispatch_once_t once = 0;
|
|
|
|
dispatch_once( &once, ^{
|
|
|
|
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
|
|
|
|
} );
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
return [SKPaymentQueue defaultQueue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canMakePayments {
|
|
|
|
|
|
|
|
return [SKPaymentQueue canMakePayments];
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (BOOL)isFeatureUnlocked:(NSString *)productIdentifier {
|
|
|
|
|
|
|
|
if (![productIdentifier length])
|
|
|
|
// Missing a product.
|
|
|
|
return NO;
|
|
|
|
if ([productIdentifier isEqualToString:MPProductFuel])
|
|
|
|
// Consumable product.
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
#if ADHOC || DEBUG
|
2016-05-16 04:50:17 +00:00
|
|
|
// All features are unlocked for beta / debug / mac versions.
|
2014-09-26 04:32:07 +00:00
|
|
|
return YES;
|
|
|
|
#else
|
|
|
|
// Check if product is purchased.
|
|
|
|
return [[NSUserDefaults standardUserDefaults] objectForKey:productIdentifier] != nil;
|
|
|
|
#endif
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
- (void)restoreCompletedTransactions {
|
|
|
|
|
|
|
|
[[self paymentQueue] restoreCompletedTransactions];
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (void)purchaseProductWithIdentifier:(NSString *)productIdentifier quantity:(NSInteger)quantity {
|
2014-09-24 05:07:02 +00:00
|
|
|
|
2014-09-29 02:15:55 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
if (![[MPAppDelegate_Shared get] canMakePayments]) {
|
|
|
|
[PearlAlert showAlertWithTitle:@"Store Not Set Up" message:
|
|
|
|
@"Try logging using the App Store or from Settings."
|
|
|
|
viewStyle:UIAlertViewStyleDefault initAlert:nil
|
|
|
|
tappedButtonBlock:nil cancelTitle:@"Thanks" otherTitles:nil];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
for (SKProduct *product in self.products)
|
|
|
|
if ([product.productIdentifier isEqualToString:productIdentifier]) {
|
2014-09-26 04:32:07 +00:00
|
|
|
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
|
|
|
|
payment.quantity = quantity;
|
|
|
|
[[self paymentQueue] addPayment:payment];
|
2014-09-24 05:07:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:49:57 +00:00
|
|
|
#pragma mark - SKProductsRequestDelegate
|
|
|
|
|
|
|
|
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
|
|
|
|
|
|
|
|
inf( @"products: %@, invalid: %@", response.products, response.invalidProductIdentifiers );
|
|
|
|
self.products = response.products;
|
2014-09-26 04:32:07 +00:00
|
|
|
|
|
|
|
for (id<MPInAppDelegate> productObserver in self.productObservers)
|
|
|
|
[productObserver updateWithProducts:self.products];
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
[PearlAlert showAlertWithTitle:@"Purchase Failed" message:
|
|
|
|
strf( @"%@\n\n%@", error.localizedDescription,
|
|
|
|
@"Ensure you are online and try logging out and back into iTunes from your device's Settings." )
|
|
|
|
viewStyle:UIAlertViewStyleDefault initAlert:nil tappedButtonBlock:nil
|
|
|
|
cancelTitle:@"OK" otherTitles:nil];
|
|
|
|
#else
|
|
|
|
#endif
|
2014-09-22 03:28:50 +00:00
|
|
|
err( @"StoreKit request (%@) failed: %@", request, [error fullDescription] );
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestDidFinish:(SKRequest *)request {
|
|
|
|
|
|
|
|
dbg( @"StoreKit request (%@) finished.", request );
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - SKPaymentTransactionObserver
|
|
|
|
|
|
|
|
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
|
|
|
|
|
|
|
|
for (SKPaymentTransaction *transaction in transactions) {
|
2014-09-27 16:52:17 +00:00
|
|
|
dbg( @"transaction updated: %@ -> %d", transaction.payment.productIdentifier, (int)(transaction.transactionState) );
|
2014-09-21 14:49:57 +00:00
|
|
|
switch (transaction.transactionState) {
|
2014-09-26 04:32:07 +00:00
|
|
|
case SKPaymentTransactionStatePurchased: {
|
2014-09-21 14:49:57 +00:00
|
|
|
inf( @"purchased: %@", transaction.payment.productIdentifier );
|
2014-09-26 04:32:07 +00:00
|
|
|
if ([transaction.payment.productIdentifier isEqualToString:MPProductFuel]) {
|
2014-09-30 04:01:33 +00:00
|
|
|
float currentFuel = [[MPiOSConfig get].developmentFuelRemaining floatValue];
|
2014-09-26 04:32:07 +00:00
|
|
|
float purchasedFuel = transaction.payment.quantity / MP_FUEL_HOURLY_RATE;
|
2014-09-30 04:01:33 +00:00
|
|
|
[MPiOSConfig get].developmentFuelRemaining = @(currentFuel + purchasedFuel);
|
2015-09-23 04:31:33 +00:00
|
|
|
if (![MPiOSConfig get].developmentFuelChecked || currentFuel < DBL_EPSILON)
|
2014-09-28 14:05:36 +00:00
|
|
|
[MPiOSConfig get].developmentFuelChecked = [NSDate date];
|
2014-09-26 04:32:07 +00:00
|
|
|
}
|
2014-09-21 14:49:57 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:transaction.transactionIdentifier
|
|
|
|
forKey:transaction.payment.productIdentifier];
|
2014-09-26 04:32:07 +00:00
|
|
|
[queue finishTransaction:transaction];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SKPaymentTransactionStateRestored: {
|
|
|
|
inf( @"restored: %@", transaction.payment.productIdentifier );
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:transaction.transactionIdentifier
|
|
|
|
forKey:transaction.payment.productIdentifier];
|
|
|
|
[queue finishTransaction:transaction];
|
2014-09-21 14:49:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SKPaymentTransactionStatePurchasing:
|
|
|
|
case SKPaymentTransactionStateDeferred:
|
|
|
|
break;
|
2014-09-26 04:32:07 +00:00
|
|
|
case SKPaymentTransactionStateFailed:
|
|
|
|
err( @"Transaction failed: %@, reason: %@", transaction.payment.productIdentifier, [transaction.error fullDescription] );
|
|
|
|
[queue finishTransaction:transaction];
|
|
|
|
break;
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
for (id<MPInAppDelegate> productObserver in self.productObservers)
|
|
|
|
[productObserver updateWithTransaction:transaction];
|
|
|
|
}
|
2014-10-24 04:35:05 +00:00
|
|
|
if (![[NSUserDefaults standardUserDefaults] synchronize])
|
|
|
|
wrn( @"Couldn't synchronize after transaction updates." );
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
|
|
|
|
|
2014-09-22 03:28:50 +00:00
|
|
|
err( @"StoreKit restore failed: %@", [error fullDescription] );
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|