Skip to content

Commit

Permalink
Improve error handling for the Safari extension app (#4218)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 14, 2021
1 parent 36425c2 commit 7c4ce77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion safari/Refined GitHub.xcodeproj/project.pbxproj
Expand Up @@ -106,12 +106,12 @@
1FE2B2132540F6EE00A5D543 /* Refined GitHub */ = {
isa = PBXGroup;
children = (
1FE2B2142540F6EE00A5D543 /* Refined_GitHub.entitlements */,
1FE2B2152540F6EE00A5D543 /* AppDelegate.swift */,
1FE2B21A2540F6EE00A5D543 /* ViewController.swift */,
1FE2B2172540F6EE00A5D543 /* Main.storyboard */,
1FE2B21C2540F6EF00A5D543 /* Assets.xcassets */,
1FE2B21E2540F6EF00A5D543 /* Info.plist */,
1FE2B2142540F6EE00A5D543 /* Refined_GitHub.entitlements */,
);
path = "Refined GitHub";
sourceTree = "<group>";
Expand Down
40 changes: 31 additions & 9 deletions safari/Refined GitHub/ViewController.swift
Expand Up @@ -6,19 +6,25 @@ let appName = "Refined GitHub"
let extensionBundleIdentifier = "com.sindresorhus.Refined-GitHub.Extension"

final class ViewController: NSViewController {
@IBOutlet var appNameLabel: NSTextField!
@IBOutlet private var appNameLabel: NSTextField!

override func viewDidLoad() {
super.viewDidLoad()

appNameLabel.stringValue = appName

SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) { state, error in
guard
let state = state,
error == nil
else {
// Insert code to inform the user that something went wrong.
if let error = error {
NSLog("%@", error.localizedDescription)
DispatchQueue.main.async {
NSApp.presentError(error)
}
return
}

guard let state = state else {
// This should in theory not be hit.
NSLog("Could not get state.")
return
}

Expand All @@ -35,11 +41,27 @@ final class ViewController: NSViewController {
}
}

override func viewDidAppear() {
super.viewDidAppear()

if let window = view.window {
window.title = appName
window.level = .floating
window.styleMask = [.titled]
window.center()
}

NSApp.activate(ignoringOtherApps: true)
}

@IBAction
func openSafariExtensionPreferences(_ sender: AnyObject?) {
private func openSafariExtensionPreferences(_ sender: AnyObject?) {
SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { error in
guard error == nil else {
// Insert code to inform the user that something went wrong.
if let error = error {
NSLog("%@", error.localizedDescription)
DispatchQueue.main.async {
NSApp.presentError(error)
}
return
}

Expand Down

0 comments on commit 7c4ce77

Please sign in to comment.