From 267f43e08afefadac7cdd9987973501da10275fe Mon Sep 17 00:00:00 2001 From: divyeshGondaliya <94589861+divyeshGondaliya@users.noreply.github.com> Date: Thu, 12 Jun 2025 16:29:18 +0530 Subject: [PATCH] Update NativeSettings.m Fork of cordova-open-native-settings with updated iOS support using modern openURL API and proper callback handling for iOS 10+ compatibility. --- src/ios/NativeSettings.m | 248 +++++++++++++-------------------------- 1 file changed, 80 insertions(+), 168 deletions(-) diff --git a/src/ios/NativeSettings.m b/src/ios/NativeSettings.m index b2bc346..ecd5387 100644 --- a/src/ios/NativeSettings.m +++ b/src/ios/NativeSettings.m @@ -2,184 +2,96 @@ @implementation NativeSettings -- (BOOL)do_open:(NSString *)pref { - if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:pref]]) { - return YES; +- (void)openURL:(NSString *)urlString withCallback:(CDVInvokedUrlCommand *)command { + NSURL *url = [NSURL URLWithString:urlString]; + + if (@available(iOS 10.0, *)) { + [[UIApplication sharedApplication] openURL:url + options:@{} + completionHandler:^(BOOL success) { + CDVPluginResult *pluginResult; + if (success) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Opened"]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot open"]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; } else { - return NO; + BOOL result = [[UIApplication sharedApplication] openURL:url]; + CDVPluginResult *pluginResult = result + ? [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Opened"] + : [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot open"]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } } -- (void)open:(CDVInvokedUrlCommand*)command -{ - CDVPluginResult* pluginResult = nil; +- (void)open:(CDVInvokedUrlCommand*)command { NSString* key = [command.arguments objectAtIndex:0]; - NSString* prefix = @"App-Prefs:"; - BOOL result = NO; + NSString* prefix = SYSTEM_VERSION_LESS_THAN(@"11.3") ? @"app-settings:" : @"App-Prefs:"; - if(SYSTEM_VERSION_LESS_THAN(@"11.3")){ - prefix = @"app-settings:"; - } + NSDictionary *pathMap = @{ + @"settings": @"", + @"about": @"General&path=About", + @"accessibility": @"General&path=ACCESSIBILITY", + @"account": @"ACCOUNT_SETTINGS", + @"autolock": @"DISPLAY&path=AUTOLOCK", + @"display": @"Brightness", + @"bluetooth": @"Bluetooth", + @"castle": @"CASTLE", + @"cellular_usage": @"General&path=USAGE/CELLULAR_USAGE", + @"configuration_list": @"General&path=ManagedConfigurationList", + @"date": @"General&path=DATE_AND_TIME", + @"facetime": @"FACETIME", + @"tethering": @"INTERNET_TETHERING", + @"music": @"MUSIC", + @"music_equalizer": @"MUSIC&path=EQ", + @"music_volume": @"MUSIC&path=VolumeLimit", + @"keyboard": @"General&path=Keyboard", + @"locale": @"General&path=INTERNATIONAL", + @"location": @"LOCATION_SERVICES", + @"locations": @"Privacy&path=LOCATION", + @"tracking": @"Privacy&path=USER_TRACKING", + @"network": @"General&path=Network", + @"nike_ipod": @"NIKE_PLUS_IPOD", + @"notes": @"NOTES", + @"notification_id": @"NOTIFICATIONS_ID", + @"passbook": @"PASSBOOK", + @"phone": @"Phone", + @"photos": @"Photos", + @"reset": @"General&path=Reset", + @"ringtone": @"Sounds&path=Ringtone", + @"browser": @"Safari", + @"search": @"SIRI", + @"sound": @"Sounds", + @"software_update": @"General&path=SOFTWARE_UPDATE_LINK", + @"storage": @"CASTLE&path=STORAGE_AND_BACKUP", + @"store": @"STORE", + @"usage": @"General&path=USAGE", + @"video": @"VIDEO", + @"vpn": @"General&path=Network/VPN", + @"wallpaper": @"Wallpaper", + @"wifi": @"WIFI", + @"touch": @"TOUCHID_PASSCODE", + @"battery": @"BATTERY_USAGE", + @"privacy": @"Privacy", + @"do_not_disturb": @"General&path=DO_NOT_DISTURB", + @"keyboards": @"General&path=Keyboard/KEYBOARDS", + @"mobile_data": @"MOBILE_DATA_SETTINGS_ID" + }; - if ([key isEqualToString:@"application_details"]) { - result = [self do_open:UIApplicationOpenSettingsURLString]; - } - else if ([key isEqualToString:@"settings"]) { - result = [self do_open:prefix]; - } - else if ([key isEqualToString:@"about"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=About"]]; - } - else if ([key isEqualToString:@"accessibility"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=ACCESSIBILITY"]]; - } - else if ([key isEqualToString:@"account"]) { - result = [self do_open:[prefix stringByAppendingString:@"ACCOUNT_SETTINGS"]]; - } - else if ([key isEqualToString:@"autolock"]) { - result = [self do_open:[prefix stringByAppendingString:@"DISPLAY&path=AUTOLOCK"]]; - } - else if ([key isEqualToString:@"display"]) { - result = [self do_open:[prefix stringByAppendingString:@"Brightness"]]; - } - else if ([key isEqualToString:@"bluetooth"]) { - result = [self do_open:[prefix stringByAppendingString:@"Bluetooth"]]; - } - else if ([key isEqualToString:@"castle"]) { - result = [self do_open:[prefix stringByAppendingString:@"CASTLE"]]; - } - else if ([key isEqualToString:@"cellular_usage"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=USAGE/CELLULAR_USAGE"]]; - } - else if ([key isEqualToString:@"configuration_list"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=ManagedConfigurationList"]]; - } - else if ([key isEqualToString:@"date"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=DATE_AND_TIME"]]; - } - else if ([key isEqualToString:@"facetime"]) { - result = [self do_open:[prefix stringByAppendingString:@"FACETIME"]]; - } - else if ([key isEqualToString:@"settings"]) { - result = [self do_open:[prefix stringByAppendingString:@"General"]]; - } - else if ([key isEqualToString:@"tethering"]) { - result = [self do_open:[prefix stringByAppendingString:@"INTERNET_TETHERING"]]; - } - else if ([key isEqualToString:@"music"]) { - result = [self do_open:[prefix stringByAppendingString:@"MUSIC"]]; - } - else if ([key isEqualToString:@"music_equalizer"]) { - result = [self do_open:[prefix stringByAppendingString:@"MUSIC&path=EQ"]]; - } - else if ([key isEqualToString:@"music_volume"]) { - result = [self do_open:[prefix stringByAppendingString:@"MUSIC&path=VolumeLimit"]]; - } - else if ([key isEqualToString:@"keyboard"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=Keyboard"]]; - } - else if ([key isEqualToString:@"locale"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=INTERNATIONAL"]]; - } - else if ([key isEqualToString:@"location"]) { - result = [self do_open:[prefix stringByAppendingString:@"LOCATION_SERVICES"]]; - } - else if ([key isEqualToString:@"locations"]) { - result = [self do_open:[prefix stringByAppendingString:@"Privacy&path=LOCATION"]]; - } - else if ([key isEqualToString:@"tracking"]) { - result = [self do_open:[prefix stringByAppendingString:@"Privacy&path=USER_TRACKING"]]; - } - else if ([key isEqualToString:@"network"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=Network"]]; - } - else if ([key isEqualToString:@"nike_ipod"]) { - result = [self do_open:[prefix stringByAppendingString:@"NIKE_PLUS_IPOD"]]; - } - else if ([key isEqualToString:@"notes"]) { - result = [self do_open:[prefix stringByAppendingString:@"NOTES"]]; + [self openURL:UIApplicationOpenSettingsURLString withCallback:command]; + return; } - else if ([key isEqualToString:@"notification_id"]) { - result = [self do_open:[prefix stringByAppendingString:@"NOTIFICATIONS_ID"]]; - } - else if ([key isEqualToString:@"passbook"]) { - result = [self do_open:[prefix stringByAppendingString:@"PASSBOOK"]]; - } - else if ([key isEqualToString:@"phone"]) { - result = [self do_open:[prefix stringByAppendingString:@"Phone"]]; - } - else if ([key isEqualToString:@"photos"]) { - result = [self do_open:[prefix stringByAppendingString:@"Photos"]]; - } - else if ([key isEqualToString:@"reset"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=Reset"]]; - } - else if ([key isEqualToString:@"ringtone"]) { - result = [self do_open:[prefix stringByAppendingString:@"Sounds&path=Ringtone"]]; - } - else if ([key isEqualToString:@"browser"]) { - result = [self do_open:[prefix stringByAppendingString:@"Safari"]]; - } - else if ([key isEqualToString:@"search"]) { - result = [self do_open:[prefix stringByAppendingString:@"SIRI"]]; - } - else if ([key isEqualToString:@"sound"]) { - result = [self do_open:[prefix stringByAppendingString:@"Sounds"]]; - } - else if ([key isEqualToString:@"software_update"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=SOFTWARE_UPDATE_LINK"]]; - } - else if ([key isEqualToString:@"storage"]) { - result = [self do_open:[prefix stringByAppendingString:@"CASTLE&path=STORAGE_AND_BACKUP"]]; - } - else if ([key isEqualToString:@"store"]) { - result = [self do_open:[prefix stringByAppendingString:@"STORE"]]; - } - else if ([key isEqualToString:@"usage"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=USAGE"]]; - } - else if ([key isEqualToString:@"video"]) { - result = [self do_open:[prefix stringByAppendingString:@"VIDEO"]]; - } - else if ([key isEqualToString:@"vpn"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=Network/VPN"]]; - } - else if ([key isEqualToString:@"wallpaper"]) { - result = [self do_open:[prefix stringByAppendingString:@"Wallpaper"]]; - } - else if ([key isEqualToString:@"wifi"]) { - result = [self do_open:[prefix stringByAppendingString:@"WIFI"]]; - } - else if ([key isEqualToString:@"touch"]) { - result = [self do_open:[prefix stringByAppendingString:@"TOUCHID_PASSCODE"]]; - } - else if ([key isEqualToString:@"battery"]) { - result = [self do_open:[prefix stringByAppendingString:@"BATTERY_USAGE"]]; - } - else if ([key isEqualToString:@"privacy"]) { - result = [self do_open:[prefix stringByAppendingString:@"Privacy"]]; - } - else if ([key isEqualToString:@"do_not_disturb"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=DO_NOT_DISTURB"]]; - } - else if ([key isEqualToString:@"keyboards"]) { - result = [self do_open:[prefix stringByAppendingString:@"General&path=Keyboard/KEYBOARDS"]]; - } - else if ([key isEqualToString:@"mobile_data"]) { - result = [self do_open:[prefix stringByAppendingString:@"MOBILE_DATA_SETTINGS_ID"]]; - } - else { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid Action"]; - } - - if (result) { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Opened"]; + + NSString *path = pathMap[key]; + if (path) { + NSString *fullURL = [prefix stringByAppendingString:path]; + [self openURL:fullURL withCallback:command]; } else { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot open"]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid Action"]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - @end