Skip to content

Instantly share code, notes, and snippets.

@SwapnanilDhol
Created July 17, 2020 13:10
Show Gist options
  • Save SwapnanilDhol/d519af6d4a517eb7b19a4dfd7f61105a to your computer and use it in GitHub Desktop.
Save SwapnanilDhol/d519af6d4a517eb7b19a4dfd7f61105a to your computer and use it in GitHub Desktop.
Change App Icon in an iOS/iPadOS application.
//
// AppIconChangeController.swift
// Color Picker
//
// Created by Swapnanil Dhol on 2/5/20.
// Copyright © 2020 Swapnanil Dhol. All rights reserved.
//
import UIKit
class AppIconChangeController: UIViewController {
@IBOutlet weak private var tableView: UITableView!
private let appIcons = ["BlueIcon", "PurpleIcon", "BlackIcon", "RedIcon", "GreenIcon", "ios6logo","DefaultIcon"]
private let appIconNames = ["Blue", "Purple", "Black", "Red", "Green", "iOS 6","Default"]
override func viewDidLoad() {
super.viewDidLoad()
self.title = "app icons"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.delegate = self
self.tableView.dataSource = self
}
private func setAppIcon(with name: String) {
UIApplication.shared.setAlternateIconName(name) { error in
assertionFailure("AppIconChangeController: Error while Changing App Icon. Error is \(error?.localizedDescription)")
}
}
}
extension AppIconChangeController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1 //It Defaults to 1. No need to use this method. I just do it out of habit
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
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", for: indexPath)
cell.imageView?.layer.cornerRadius = 8
cell.imageView?.image = UIImage(imageLiteralResourceName: appIcons[indexPath.row])
cell.textLabel?.text = appIconNames[indexPath.row]
return cell
}
}
extension AppIconChangeController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 110
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 10 : 6
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
UIImpactFeedbackGenerator().impactOccurred(intensity: 0.7)
let section = indexPath.row
switch section
{
case 0:
setAppIcon(with: "AppIcon-2")
case 1:
setAppIcon(with: "AppIcon-3")
case 2:
setAppIcon(with: "AppIcon-4")
case 3:
setAppIcon(with: "AppIcon-5")
case 4:
setAppIcon(with: "AppIcon-6")
case 5:
setAppIcon(with: "AppIcon-7")
case 6:
UIApplication.shared.setAlternateIconName(nil) //Sets Default App Icon
default:
break
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment