From af5559bd6ad0431ba6f0bdfaef1991e23f34d2f0 Mon Sep 17 00:00:00 2001 From: arichornlover <78001398+arichornlover@users.noreply.github.com> Date: Sat, 27 Apr 2024 00:42:58 -0500 Subject: [PATCH] Expand functionality (AppIconOptionsController.m) --- Sources/AppIconOptionsController.m | 32 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Sources/AppIconOptionsController.m b/Sources/AppIconOptionsController.m index fc7fa20..2a3f0f8 100644 --- a/Sources/AppIconOptionsController.m +++ b/Sources/AppIconOptionsController.m @@ -108,11 +108,24 @@ } - (void)saveIcon { - NSString *selectedIconPath = self.selectedIconIndex >= 0 ? self.appIcons[self.selectedIconIndex] : nil; - if (selectedIconPath) { - NSURL *iconURL = [NSURL fileURLWithPath:selectedIconPath]; - if ([[UIApplication sharedApplication] respondsToSelector:@selector(setAlternateIconName:completionHandler:)]) { - [[UIApplication sharedApplication] setAlternateIconName:selectedIconPath completionHandler:^(NSError * _Nullable error) { + NSString *selectedIcon = self.selectedIconIndex >= 0 ? self.appIcons[self.selectedIconIndex] : nil; + if (selectedIcon) { + NSString *iconName = [selectedIcon.lastPathComponent stringByDeletingPathExtension]; + NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; + NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; + NSMutableDictionary *iconsDict = [infoDict objectForKey:@"CFBundleIcons"]; + + if (iconsDict) { + NSMutableDictionary *primaryIconDict = [iconsDict objectForKey:@"CFBundlePrimaryIcon"]; + if (primaryIconDict) { + NSMutableArray *iconFiles = [primaryIconDict objectForKey:@"CFBundleIconFiles"]; + [iconFiles addObject:iconName]; + primaryIconDict[@"CFBundleIconFiles"] = iconFiles; + } + [infoDict setObject:iconsDict forKey:@"CFBundleIcons"]; + [infoDict writeToFile:plistPath atomically:YES]; + + [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"Error setting alternate icon: %@", error.localizedDescription); [self showAlertWithTitle:@"Error" message:@"Failed to set alternate icon"]; @@ -122,13 +135,10 @@ } }]; } else { - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - [dict setObject:iconURL forKey:@"iconURL"]; - NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; - [dict writeToFile:filePath atomically:YES]; - - [self showAlertWithTitle:@"Alternate Icon" message:@"Please restart the app to apply the alternate icon"]; + NSLog(@"CFBundleIcons key not found in Info.plist"); } + } else { + NSLog(@"Selected icon path is nil"); } }