Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

add restore purchases button

roryclear67a0051194b9
diff --git a/clearcam/SettingsViewController.m b/clearcam/SettingsViewController.m
index 8511ef5..ff87aa4 100644
--- a/clearcam/SettingsViewController.m
+++ b/clearcam/SettingsViewController.m
@@ -291,7 +291,7 @@
#pragma mark - UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 3; // Camera Settings, Viewer Settings, Upgrade to Premium
+ return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
@@ -299,8 +299,9 @@
return @"Camera Settings";
} else if (section == 1) {
return @"Viewer Settings";
- } else if (section == 2 && ![[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"]) {
- return nil; // No header for Upgrade to Premium
+ } else if (section == 2) {
+ BOOL isSubscribed = [[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"];
+ return isSubscribed ? @"Subscription" : @"Upgrade to Premium";
}
return nil;
}
@@ -319,7 +320,8 @@
} else if (section == 1) { // Viewer Settings
return 1; // Just Receive Notifications
} else if (section == 2) { // Upgrade to Premium
- return [[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] ? 0 : 1;
+ BOOL isSubscribed = [[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"];
+ return isSubscribed ? 1 : 2; // 1 row for "Restore Purchases" if subscribed, 2 rows ("Upgrade" + "Restore") if not
}
return 0;
}
@@ -450,12 +452,20 @@
cell.textLabel.textColor = isPremium ? [UIColor labelColor] : [UIColor grayColor];
cell.userInteractionEnabled = YES; // Cell remains tappable regardless of premium status
}
- } else if (indexPath.section == 2) { // Upgrade to Premium
- cell.textLabel.text = @"Upgrade to Premium";
- cell.textLabel.textColor = [UIColor systemBlueColor];
- cell.textLabel.textAlignment = NSTextAlignmentCenter;
- cell.accessoryType = UITableViewCellAccessoryNone;
- cell.userInteractionEnabled = YES;
+ } else if (indexPath.section == 2) { // Upgrade to Premium / Subscription
+ if (!isPremium && indexPath.row == 0) {
+ cell.textLabel.text = @"Upgrade to Premium";
+ cell.textLabel.textColor = [UIColor systemBlueColor];
+ cell.textLabel.textAlignment = NSTextAlignmentCenter;
+ cell.accessoryType = UITableViewCellAccessoryNone;
+ cell.userInteractionEnabled = YES;
+ } else if ((isPremium && indexPath.row == 0) || (!isPremium && indexPath.row == 1)) {
+ cell.textLabel.text = @"Restore Purchases";
+ cell.textLabel.textColor = [UIColor systemBlueColor];
+ cell.textLabel.textAlignment = NSTextAlignmentCenter;
+ cell.accessoryType = UITableViewCellAccessoryNone;
+ cell.userInteractionEnabled = YES;
+ }
}
return cell;
@@ -564,8 +574,23 @@
[self showUpgradePopup];
} // No else needed since switch handles the toggle when premium
}
- } else if (indexPath.section == 2) { // Upgrade to Premium
- [[StoreManager sharedInstance] fetchAndPurchaseProduct];
+ } else if (indexPath.section == 2) { // Upgrade to Premium / Subscription
+ if (!isPremium && indexPath.row == 0) {
+ [[StoreManager sharedInstance] fetchAndPurchaseProduct];
+ } else if ((isPremium && indexPath.row == 0) || (!isPremium && indexPath.row == 1)) {
+ // Show a loading indicator or confirmation
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Restoring Purchases"
+ message:@"Please wait while we restore your previous purchases..."
+ preferredStyle:UIAlertControllerStyleAlert];
+ [self presentViewController:alert animated:YES completion:nil];
+
+ [[StoreManager sharedInstance] restorePurchases];
+
+ // Dismiss the alert after a delay or handle this in StoreManager's callbacks
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ [alert dismissViewControllerAnimated:YES completion:nil];
+ });
+ }
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
diff --git a/clearcam/StoreManager.h b/clearcam/StoreManager.h
index 2ce8eef..74a682e 100644
--- a/clearcam/StoreManager.h
+++ b/clearcam/StoreManager.h
@@ -13,6 +13,7 @@ extern NSString *const StoreManagerSubscriptionStatusDidChangeNotification;
- (void)verifySubscriptionWithCompletion:(void (^)(BOOL isActive, NSDate * _Nullable expiryDate))completion;
- (NSString *)retrieveSessionTokenFromKeychain;
- (void)getPremiumProductInfo:(void (^)(SKProduct * _Nullable product, NSError * _Nullable error))completion;
+- (void)restorePurchases;
@end
diff --git a/clearcam/StoreManager.m b/clearcam/StoreManager.m
index 65aa001..2d5122c 100644
--- a/clearcam/StoreManager.m
+++ b/clearcam/StoreManager.m
@@ -14,6 +14,10 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan
@implementation StoreManager
+- (void)restorePurchases {
+ [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
+}
+
+ (instancetype)sharedInstance {
static StoreManager *sharedInstance = nil;
static dispatch_once_t onceToken;
@@ -125,10 +129,8 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
- // After a successful purchase, verify the subscription status
[self verifySubscriptionWithCompletion:^(BOOL isActive, NSDate *expiryDate) {
if (isActive) {
- // Post the notification to inform listeners of the status change
[[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];
}
}];
@@ -136,16 +138,48 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan
case SKPaymentTransactionStateFailed:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
+ if (transaction.error.code != SKErrorPaymentCancelled) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Purchase Failed"
+ message:transaction.error.localizedDescription
+ preferredStyle:UIAlertControllerStyleAlert];
+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
+ UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
+ while (topController.presentedViewController) {
+ topController = topController.presentedViewController;
+ }
+ [topController presentViewController:alert animated:YES completion:nil];
+ });
+ }
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
- // After restoring purchases, verify the subscription status
[self verifySubscriptionWithCompletion:^(BOOL isActive, NSDate *expiryDate) {
- if (isActive) {
- // Post the notification to inform listeners of the status change
- [[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];
- }
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (isActive) {
+ [[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Purchases Restored"
+ message:@"Your previous purchases have been successfully restored."
+ preferredStyle:UIAlertControllerStyleAlert];
+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
+ UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
+ while (topController.presentedViewController) {
+ topController = topController.presentedViewController;
+ }
+ [topController presentViewController:alert animated:YES completion:nil];
+ } else {
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No Purchases Found"
+ message:@"No previous purchases were found to restore."
+ preferredStyle:UIAlertControllerStyleAlert];
+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
+ UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
+ while (topController.presentedViewController) {
+ topController = topController.presentedViewController;
+ }
+ [topController presentViewController:alert animated:YES completion:nil];
+ }
+ });
}];
break;
@@ -155,6 +189,24 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan
}
}
+- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
+ NSLog(@"Restore completed transactions finished.");
+}
+
+- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Restore Failed"
+ message:error.localizedDescription
+ preferredStyle:UIAlertControllerStyleAlert];
+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
+ UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
+ while (topController.presentedViewController) {
+ topController = topController.presentedViewController;
+ }
+ [topController presentViewController:alert animated:YES completion:nil];
+ });
+}
+
- (void)verifySubscriptionWithCompletion:(void (^)(BOOL isActive, NSDate *expiryDate))completion {
static BOOL isRequestInProgress = NO;