Experiment

This commit is contained in:
arichornlover 2024-04-28 22:45:25 -05:00 committed by GitHub
parent 100cce165f
commit 7af45396f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 138 additions and 147 deletions

View file

@ -1,147 +0,0 @@
#import "AppIconOptionsController.h"
@interface AppIconOptionsController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray<NSString *> *appIcons;
@property (assign, nonatomic) NSInteger selectedIconIndex;
@end
@implementation AppIconOptionsController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Change App Icon";
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"YTSans-Bold" size:22], NSForegroundColorAttributeName: [UIColor whiteColor]}];
self.selectedIconIndex = -1;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Back.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = backButton;
self.appIcons = [self loadAppIcons];
[self setupNavigationBar];
}
- (NSArray<NSString *> *)loadAppIcons {
NSString *path = [[NSBundle mainBundle] pathForResource:@"uYouPlus" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
return [bundle pathsForResourcesOfType:@"png" inDirectory:@"AppIcons"];
}
- (void)setupNavigationBar {
UIBarButtonItem *resetButton = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"arrow.clockwise.circle.fill"] style:UIBarButtonItemStylePlain target:self action:@selector(resetIcon)];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveIcon)];
[self.navigationItem setRightBarButtonItems:@[saveButton, resetButton]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appIcons.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSString *iconPath = self.appIcons[indexPath.row];
UIImage *iconImage = [UIImage imageWithContentsOfFile:iconPath];
UIImageView *iconImageView = [[UIImageView alloc] initWithImage:iconImage];
iconImageView.contentMode = UIViewContentModeScaleAspectFit;
iconImageView.frame = CGRectMake(16, 10, 60, 60);
iconImageView.layer.cornerRadius = 8;
iconImageView.layer.masksToBounds = YES;
[cell.contentView addSubview:iconImageView];
UILabel *iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, self.view.frame.size.width - 90, 60)];
iconNameLabel.text = [iconPath.lastPathComponent stringByDeletingPathExtension];
iconNameLabel.textColor = [UIColor blackColor];
iconNameLabel.font = [UIFont systemFontOfSize:16.0 weight:UIFontWeightMedium];
[cell.contentView addSubview:iconNameLabel];
cell.accessoryType = (indexPath.row == self.selectedIconIndex) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)resetIcon {
[[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"Error resetting icon: %@", error.localizedDescription);
[self showAlertWithTitle:@"Error" message:@"Failed to reset icon"];
} else {
NSLog(@"Icon reset successfully");
[self showAlertWithTitle:@"Success" message:@"Icon reset successfully"];
[self.tableView reloadData];
}
}];
}
- (void)saveIcon {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
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"];
} 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 {
NSLog(@"Selected icon path is nil");
}
});
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
});
}
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
@end

View file

@ -0,0 +1,138 @@
import UIKit
class AppIconOptionsController: UIViewController {
var tableView: UITableView!
var appIcons: [String] = []
var selectedIconIndex: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
title = "Change App Icon"
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "YTSans-Bold", size: 22)!, NSAttributedString.Key.foregroundColor: UIColor.white]
selectedIconIndex = -1
tableView = UITableView(frame: view.bounds, style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.separatorStyle = .none
view.addSubview(tableView)
let backButton = UIBarButtonItem(image: UIImage(named: "Back.png"), style: .plain, target: self, action: #selector(back))
navigationItem.leftBarButtonItem = backButton
appIcons = loadAppIcons()
setupNavigationBar()
}
func loadAppIcons() -> [String] {
guard let path = Bundle.main.path(forResource: "uYouPlus", ofType: "bundle"),
let bundle = Bundle(path: path) else {
return []
}
return bundle.paths(forResourcesOfType: "png", inDirectory: "AppIcons")
}
func setupNavigationBar() {
let resetButton = UIBarButtonItem(image: UIImage(systemName: "arrow.clockwise.circle.fill"), style: .plain, target: self, action: #selector(resetIcon))
let saveButton = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(saveIcon))
navigationItem.rightBarButtonItems = [saveButton, resetButton]
}
@objc func resetIcon() {
UIApplication.shared.setAlternateIconName(nil) { error in
if let error = error {
print("Error resetting icon: \(error.localizedDescription)")
showAlertWithTitle("Error", message: "Failed to reset icon")
} else {
print("Icon reset successfully")
showAlertWithTitle("Success", message: "Icon reset successfully")
tableView.reloadData()
}
}
}
@objc func saveIcon() {
DispatchQueue.global().async {
let selectedIcon = self.selectedIconIndex >= 0 ? self.appIcons[self.selectedIconIndex] : nil
guard let iconName = selectedIcon,
let plistPath = Bundle.main.path(forResource: "Info", ofType: "plist"),
var infoDict = NSMutableDictionary(contentsOfFile: plistPath),
var iconsDict = infoDict["CFBundleIcons"] as? NSMutableDictionary,
var primaryIconDict = iconsDict["CFBundlePrimaryIcon"] as? NSMutableDictionary,
var iconFiles = primaryIconDict["CFBundleIconFiles"] as? [String] else {
print("Error accessing Info.plist")
return
}
iconFiles.append(iconName)
primaryIconDict["CFBundleIconFiles"] = iconFiles
infoDict["CFBundleIcons"] = iconsDict
infoDict.write(toFile: plistPath, atomically: true)
UIApplication.shared.setAlternateIconName(iconName) { error in
if let error = error {
print("Error setting alternate icon: \(error.localizedDescription)")
showAlertWithTitle("Error", message: "Failed to set alternate icon")
} else {
print("Alternate icon set successfully")
showAlertWithTitle("Success", message: "Alternate icon set successfully")
DispatchQueue.main.async {
tableView.reloadData()
}
}
}
}
}
func showAlertWithTitle(_ title: String, message: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
}
@objc func back() {
navigationController?.popViewController(animated: true)
}
}
extension AppIconOptionsController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return appIcons.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
for view in cell.contentView.subviews {
view.removeFromSuperview()
}
let iconPath = appIcons[indexPath.row]
if let iconImage = UIImage(contentsOfFile: iconPath) {
let iconImageView = UIImageView(image: iconImage)
iconImageView.contentMode = .scaleAspectFit
iconImageView.frame = CGRect(x: 16, y: 10, width: 60, height: 60)
iconImageView.layer.cornerRadius = 8
iconImageView.layer.masksToBounds = true
cell.contentView.addSubview(iconImageView)
let iconNameLabel = UILabel(frame: CGRect(x: 90, y: 10, width: view.frame.size.width - 90, height: 60))
iconNameLabel.text = URL(fileURLWithPath: iconPath).deletingPathExtension().lastPathComponent
iconNameLabel.textColor = .black
iconNameLabel.font = UIFont.systemFont(ofSize: 16.0, weight: .medium)
cell.contentView.addSubview(iconNameLabel)
cell.accessoryType = (indexPath.row == selectedIconIndex) ? .checkmark : .none
}
return cell
}
}