Update AppIconOptionsController.m

This commit is contained in:
arichornlover 2024-05-01 17:32:27 -05:00 committed by GitHub
parent f98e5f7f30
commit 74940a53fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -103,45 +103,46 @@
- (void)saveIcon { - (void)saveIcon {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *selectedIcon = self.selectedIconIndex >= 0 ? self.appIcons[self.selectedIconIndex] : nil; NSString *selectedIconName = self.selectedIconIndex >= 0 ? [self.appIcons[self.selectedIconIndex] lastPathComponent] : nil;
if (selectedIcon) {
NSString *iconName = [selectedIcon.lastPathComponent stringByDeletingPathExtension]; if (selectedIconName) {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; NSDictionary *iconsDict = [infoDict objectForKey:@"CFBundleIcons"];
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; NSDictionary *primaryIconDict = [iconsDict objectForKey:@"CFBundlePrimaryIcon"];
NSMutableDictionary *iconsDict = [infoDict objectForKey:@"CFBundleIcons"]; NSArray *iconFiles = [primaryIconDict objectForKey:@"CFBundleIconFiles"];
if (iconsDict) { UIImage *selectedIconImage = [UIImage imageWithContentsOfFile:self.appIcons[self.selectedIconIndex]];
NSMutableDictionary *primaryIconDict = [iconsDict objectForKey:@"CFBundlePrimaryIcon"]; CGSize appIconSize = CGSizeMake(60, 60);
if (primaryIconDict) {
NSMutableArray *iconFiles = [primaryIconDict objectForKey:@"CFBundleIconFiles"]; selectedIconImage = [self resizeImage:selectedIconImage toSize:appIconSize];
[iconFiles addObject:iconName];
primaryIconDict[@"CFBundleIconFiles"] = iconFiles; [[UIApplication sharedApplication] setAlternateIconImage:selectedIconImage completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"Error setting alternate icon: %@", error.localizedDescription);
[self showAlertWithTitle:@"Error" message:@"Failed to set alternate icon"];
} else {
NSLog(@"Alternate icon set successfully");
[self showAlertWithTitle:@"Success" message:@"Alternate icon set successfully"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
} }
[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"];
} else {
NSLog(@"Alternate icon set successfully");
[self showAlertWithTitle:@"Success" message:@"Alternate icon set successfully"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}];
} else {
NSLog(@"CFBundleIcons key not found in Info.plist");
}
} else { } else {
NSLog(@"Selected icon path is nil"); NSLog(@"Selected icon path is nil");
} }
}); });
} }
- (UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message { - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];