Add object detection, tracking, mobile notifications, and search to any security camera.
new popup
rorycleardb251b385738
diff --git a/clearcam/SettingsViewController.m b/clearcam/SettingsViewController.mindex dd69709..85b2d7c 100644--- a/clearcam/SettingsViewController.m+++ b/clearcam/SettingsViewController.m@@ -7,6 +7,7 @@#import "FileServer.h"#import "ScheduleManagementViewController.h"#import <UserNotifications/UserNotifications.h>+#import <StoreKit/StoreKit.h>@interface SettingsViewController () <UITableViewDelegate, UITableViewDataSource>@@ -526,14 +527,14 @@[self showThresholdInputDialog];} else if (indexPath.row == 5 + offset) {if (!isPremium) {- [self showPremiumRequiredAlert];+ [self showUpgradePopup];}} else if (indexPath.row == 6 + offset) {if (isPremium) {[self promptForPasswordWithCompletion:^(BOOL success) {}];} else {- [self showPremiumRequiredAlert];+ [self showUpgradePopup];}} else if (indexPath.row == 7 + offset) {ScheduleManagementViewController *scheduleVC = [[ScheduleManagementViewController alloc] init];@@ -557,7 +558,7 @@} else if (indexPath.section == 1) { // Viewer Settingsif (indexPath.row == 0) {if (!isPremium) {- [self showPremiumRequiredAlert];+ [self showUpgradePopup];} // No else needed since switch handles the toggle when premium}} else if (indexPath.section == 2) { // Upgrade to Premium@@ -566,17 +567,67 @@[tableView deselectRowAtIndexPath:indexPath animated:YES];}-- (void)showPremiumRequiredAlert {- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Premium Required"- message:@"This feature requires a premium subscription."- preferredStyle:UIAlertControllerStyleAlert];-- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"- style:UIAlertActionStyleDefault- handler:nil];-- [alert addAction:okAction];- [self presentViewController:alert animated:YES completion:nil];+- (void)showUpgradePopup {+ // Optional: Show loading indicator here if needed++ [[StoreManager sharedInstance] getPremiumProductInfo:^(SKProduct * _Nullable product, NSError * _Nullable error) {+ dispatch_async(dispatch_get_main_queue(), ^{ // Ensure UI updates happen on the main thread+ // Optional: Hide loading indicator here++ if (product) {+ // Format the price+ NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];+ [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];+ [formatter setLocale:product.priceLocale];+ NSString *localizedPrice = [formatter stringFromNumber:product.price];++ // Create the message body with features+ NSString *features = @"• View Captured Events From Anywhere\n• Receive Event Alerts";+ NSString *message = [NSString stringWithFormat:@"Unlock premium features:\n\n%@\n", features];++ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Upgrade to ClearCamera Premium"+ message:message+ preferredStyle:UIAlertControllerStyleAlert];++ // Upgrade Action+ UIAlertAction *upgradeAction = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Upgrade for %@", localizedPrice ?: @"Price"] // Fallback text+ style:UIAlertActionStyleDefault+ handler:^(UIAlertAction * _Nonnull action) {+ // Initiate the purchase flow+ [[StoreManager sharedInstance] fetchAndPurchaseProduct];+ }];+ // Cancel Action+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"+ style:UIAlertActionStyleCancel+ handler:nil];++ [alert addAction:upgradeAction];+ [alert addAction:cancelAction];++ [self presentViewController:alert animated:YES completion:nil];++ } else {+ // Fallback if product info failed to load+ NSLog(@"Error fetching product info: %@", error.localizedDescription);+ UIAlertController *fallbackAlert = [UIAlertController alertControllerWithTitle:@"Upgrade to Premium"+ message:@"Unlock premium features like remote event viewing and alerts. Would you like to upgrade?"+ preferredStyle:UIAlertControllerStyleAlert];++ UIAlertAction *upgradeFallbackAction = [UIAlertAction actionWithTitle:@"Upgrade"+ style:UIAlertActionStyleDefault+ handler:^(UIAlertAction * _Nonnull action) {+ [[StoreManager sharedInstance] fetchAndPurchaseProduct];+ }];+ UIAlertAction *cancelFallbackAction = [UIAlertAction actionWithTitle:@"Cancel"+ style:UIAlertActionStyleCancel+ handler:nil];++ [fallbackAlert addAction:upgradeFallbackAction];+ [fallbackAlert addAction:cancelFallbackAction];+ [self presentViewController:fallbackAlert animated:YES completion:nil];+ }+ });+ }];}- (void)useOwnnotificationServerSwitchToggled:(UISwitch *)sender {@@ -638,7 +689,7 @@- (void)sendNotifSwitchToggled:(UISwitch *)sender {if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"]) {sender.on = NO;- [self showPremiumRequiredAlert];+ [self showUpgradePopup];return;}diff --git a/clearcam/StoreManager.h b/clearcam/StoreManager.hindex 9f00548..2ce8eef 100644--- a/clearcam/StoreManager.h+++ b/clearcam/StoreManager.h@@ -12,6 +12,7 @@ extern NSString *const StoreManagerSubscriptionStatusDidChangeNotification;- (void)fetchAndPurchaseProduct;- (void)verifySubscriptionWithCompletion:(void (^)(BOOL isActive, NSDate * _Nullable expiryDate))completion;- (NSString *)retrieveSessionTokenFromKeychain;+- (void)getPremiumProductInfo:(void (^)(SKProduct * _Nullable product, NSError * _Nullable error))completion;@enddiff --git a/clearcam/StoreManager.m b/clearcam/StoreManager.mindex 22141c6..0c7266f 100644--- a/clearcam/StoreManager.m+++ b/clearcam/StoreManager.m@@ -1,5 +1,6 @@#import "StoreManager.h"#import "FileServer.h"+#import <StoreKit/StoreKit.h>// Define the notification nameNSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreManagerSubscriptionStatusDidChangeNotification";@@ -7,6 +8,8 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan@interface StoreManager ()@property (nonatomic, strong) SKProductsRequest *productsRequest;@property (nonatomic, strong) SKProduct *premiumProduct;+@property (nonatomic, strong) void (^productInfoCompletionHandler)(SKProduct * _Nullable product, NSError * _Nullable error); // Store completion handler for fetching info+- (void)getPremiumProductInfo:(void (^)(SKProduct * _Nullable product, NSError * _Nullable error))completion;@end@implementation StoreManager@@ -29,26 +32,109 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan}- (void)fetchAndPurchaseProduct {- NSSet *productIdentifiers = [NSSet setWithObject:@"monthly.premium"];+ if (self.premiumProduct) {+ // Product info already available, proceed to purchase+ [self purchaseProduct:self.premiumProduct];+ } else {+ // Fetch the product info first, and *then* call purchase.+ NSLog(@"Premium product info not yet fetched. Fetching now for purchase...");+ [self getPremiumProductInfo:^(SKProduct * _Nullable product, NSError * _Nullable error) {+ // Ensure UI updates potentially triggered by purchase alerts run on main thread+ dispatch_async(dispatch_get_main_queue(), ^{+ if (product) {+ [self purchaseProduct:product];+ } else {+ // Handle error - maybe show an alert?+ NSLog(@"Failed to get product info for purchase: %@", error.localizedDescription);+ // Consider showing an error alert here, maybe via a notification or delegate.+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error"+ message:@"Could not retrieve product information. Please try again later."+ preferredStyle:UIAlertControllerStyleAlert];+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];+ // Find the top view controller to present the alert+ UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;+ while (topController.presentedViewController) {+ topController = topController.presentedViewController;+ }+ [topController presentViewController:alert animated:YES completion:nil];+ }+ });+ }];+ }+}++- (void)getPremiumProductInfo:(void (^)(SKProduct * _Nullable product, NSError * _Nullable error))completion {+ if (self.premiumProduct) {+ if (completion) {+ completion(self.premiumProduct, nil);+ }+ return;+ }++ // Store the completion handler+ self.productInfoCompletionHandler = completion;++ // Fetch product info from App Store+ NSSet *productIdentifiers = [NSSet setWithObject:@"monthly.premium"]; // Your product IDself.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];- self.productsRequest.delegate = self;+ self.productsRequest.delegate = self; // Delegate methods will handle the response[self.productsRequest start];}#pragma mark - SKProductsRequestDelegate- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {- if (response.products.count == 0) return;- self.premiumProduct = response.products.firstObject;- [self purchaseProduct];+ NSLog(@"Received product response.");+ if (response.products.count > 0) {+ self.premiumProduct = response.products.firstObject;+ NSLog(@"Premium product fetched: %@", self.premiumProduct.localizedTitle);++ // If there's a completion handler waiting for product info (from getPremiumProductInfo), call it.+ if (self.productInfoCompletionHandler) {+ // Ensure handler is called on main thread if it involves UI updates later+ dispatch_async(dispatch_get_main_queue(), ^{+ self.productInfoCompletionHandler(self.premiumProduct, nil);+ self.productInfoCompletionHandler = nil; // Clear the handler+ });+ }+ } else {+ NSLog(@"No products found.");+ NSError *error = [NSError errorWithDomain:@"StoreManagerError" code:101 userInfo:@{NSLocalizedDescriptionKey: @"No products found."}];+ if (self.productInfoCompletionHandler) {+ dispatch_async(dispatch_get_main_queue(), ^{+ self.productInfoCompletionHandler(nil, error);+ self.productInfoCompletionHandler = nil;+ });+ }+ }+ // Clean up the request object+ self.productsRequest.delegate = nil;+ self.productsRequest = nil;+}++- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {+ NSLog(@"Failed to fetch products: %@", error.localizedDescription);+ if (self.productInfoCompletionHandler) {+ dispatch_async(dispatch_get_main_queue(), ^{+ self.productInfoCompletionHandler(nil, error);+ self.productInfoCompletionHandler = nil; // Clear the handler+ });+ }+ // Clean up the request object+ self.productsRequest.delegate = nil;+ self.productsRequest = nil;}#pragma mark - Purchase Product-- (void)purchaseProduct {- if ([SKPaymentQueue canMakePayments] && self.premiumProduct) {- SKPayment *payment = [SKPayment paymentWithProduct:self.premiumProduct];+- (void)purchaseProduct:(SKProduct *)product {+ if ([SKPaymentQueue canMakePayments] && product) {+ NSLog(@"Initiating purchase for: %@", product.localizedTitle);+ SKPayment *payment = [SKPayment paymentWithProduct:product];[[SKPaymentQueue defaultQueue] addPayment:payment];+ } else {+ NSLog(@"Cannot make payments or product is nil.");+ // Maybe show an alert to the user here?}}