Skip to content

Commit

Permalink
feat: supports hiding notification
Browse files Browse the repository at this point in the history
  • Loading branch information
johnborges committed Jan 15, 2021
1 parent daa2bd0 commit d634cb7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 30 deletions.
20 changes: 20 additions & 0 deletions example/ios/App/App/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@
<string>ionic-icon</string>
</array>
</dict>
<key>ionitron-icon</key>
<dict>
<key>UIPrerenderedIcon</key>
<true/>
<key>CFBundleIconFiles</key>
<array>
<!-- The actual filenames. Don't list the @2x/@3x files here, and you can use just the biggest one if you like -->
<string>ionitron-icon</string>
</array>
</dict>
<key>stencil-icon</key>
<dict>
<key>UIPrerenderedIcon</key>
<true/>
<key>CFBundleIconFiles</key>
<array>
<!-- The actual filenames. Don't list the @2x/@3x files here, and you can use just the biggest one if you like -->
<string>stencil-icon</string>
</array>
</dict>
</dict>
</dict>
</dict>
Expand Down
5 changes: 3 additions & 2 deletions example/src/components/app-home/app-home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class AppHome {

async resetIcon() {
try {
await AppIcon.reset()
await AppIcon.reset({suppressNotification: true})
} catch (error) {
console.debug(error)
}
Expand All @@ -27,10 +27,11 @@ export class AppHome {
let setIconName = await AppIcon.getName();
console.debug(`App Icon set to: `,setIconName.value);

await AppIcon.change({name: iconName});
await AppIcon.change({name: iconName, suppressNotification: true});

setIconName = await AppIcon.getName();
console.debug(`App Icon set to: `,setIconName.value);

} catch (error) {
console.debug(error)
}
Expand Down
53 changes: 28 additions & 25 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,51 @@ public class AppIcon: CAPPlugin {
}

@objc func reset(_ call: CAPPluginCall) {
changeIcon(iconName: nil, call)
let suppressNotification = call.getBool("suppressNotification") ?? true

setIcon(iconName: nil, suppressNotification: suppressNotification, call)
}

@objc func change(_ call: CAPPluginCall) {
CAPLog.print("Changing app icon.")

guard let iconName = call.getString("name") else {
call.reject("Must provide an icon name.")
return
}

DispatchQueue.main.sync {

if UIApplication.shared.responds(to: #selector(getter: UIApplication.supportsAlternateIcons)) && UIApplication.shared.supportsAlternateIcons {
typealias setAlternateIconName = @convention(c) (NSObject, Selector, NSString?, @escaping (NSError) -> ()) -> ()

let selectorString = "_setAlternateIconName:completionHandler:"

let selector = NSSelectorFromString(selectorString)
let imp = UIApplication.shared.method(for: selector)
let method = unsafeBitCast(imp, to: setAlternateIconName.self)
method(UIApplication.shared, selector, iconName as NSString?, { _ in })

call.resolve();
}
}
let suppressNotification = call.getBool("suppressNotification") ?? true

setIcon(iconName: iconName, suppressNotification: suppressNotification, call)
}

func changeIcon(iconName: String?, _ call: CAPPluginCall) {
func setIcon(iconName: String?, suppressNotification: Bool, _ call: CAPPluginCall) {
DispatchQueue.main.sync {
// Check if the app supports alternating icons
guard UIApplication.shared.supportsAlternateIcons else {
return call.reject("Alternate icons not supported.");
}

if(suppressNotification) {
if UIApplication.shared.responds(to: #selector(getter: UIApplication.supportsAlternateIcons)) && UIApplication.shared.supportsAlternateIcons {
typealias setAlternateIconName = @convention(c) (NSObject, Selector, NSString?, @escaping (NSError) -> ()) -> ()

let selectorString = "_setAlternateIconName:completionHandler:"

let selector = NSSelectorFromString(selectorString)
let imp = UIApplication.shared.method(for: selector)
let method = unsafeBitCast(imp, to: setAlternateIconName.self)
method(UIApplication.shared, selector, iconName as NSString?, { _ in })

call.resolve();
}

// Change the icon to a specific image with given name
UIApplication.shared.setAlternateIconName(iconName) { (error) in
// After app icon changed, print our error or success message
if let error = error {
call.reject("App icon failed to due to \(error.localizedDescription)")
} else {
call.resolve()
} else {
UIApplication.shared.setAlternateIconName(iconName) { (error) in
if let error = error {
call.reject("App icon failed to due to \(error.localizedDescription)")
} else {
call.resolve()
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@capacitor-community/app-icon",
"version": "0.0.1",
"version": "0.3.0",
"description": "Capacitor community plugin for changing an iOS app icon.",
"main": "dist/plugin.js",
"module": "dist/esm/index.js",
Expand Down
14 changes: 12 additions & 2 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@ declare module '@capacitor/core' {
}

interface IconOptions {
/**
* Name of alternate icon to set
*/
name: string;
/**
* Flag controlling the in app notification which shows after icon is changed.
*/
suppressNotification: boolean
}

export interface AppIconPlugin {
/**
* Checks if changing the app icon is supported
* @since 1.0.0
*/
isSupported(): Promise<{value: boolean}>;
/**
* Gets the name of currently set alternate icon. If original icon is set, returns null.
* @since 1.0.0
*/
getName(): Promise<{value: string | null}>;
/**
* Changes app icon to specified alternate.
* @since 1.0.0
*/
change(options: IconOptions): Promise<void>;
/**
* Reverts app icon to original.
* */
reset(suppressNotification: boolean): Promise<void>;
* @since 1.0.0
*/
reset(options: {suppressNotification: boolean}): Promise<void>;

// appIconBadgeNumber(): Promise<{value: number}>;
}

0 comments on commit d634cb7

Please sign in to comment.