forked from yanue/V2rayU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
63 lines (50 loc) · 1.79 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// AppDelegate.swift
// V2rayULauncher
//
// Created by yanue on 2018/10/19.
// Copyright © 2018 yanue. All rights reserved.
//
import Cocoa
import os.log
class V2rayULauncherApplication: NSApplication {
let strongDelegate = AppDelegate()
override init() {
super.init()
self.delegate = strongDelegate
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
public func applicationDidFinishLaunching(_ notification: Notification) {
let mainAppIdentifier = "net.yanue.V2rayU"
let running = NSWorkspace.shared.runningApplications
var alreadyRunning = false
for app in running {
if app.bundleIdentifier == mainAppIdentifier {
alreadyRunning = true
break
}
}
if !alreadyRunning {
DistributedNotificationCenter.default().addObserver(NSApp, selector: #selector(NSApplication.terminate(_:)), name: Notification.Name("terminateV2rayU"), object: mainAppIdentifier)
let path = Bundle.main.bundlePath as NSString
var components = path.pathComponents
components.removeLast()
components.removeLast()
components.removeLast()
components.append("MacOS")
components.append("V2rayU")
let newPath = NSString.path(withComponents: components)
NSWorkspace.shared.launchApplication(newPath)
} else {
NSApp.terminate(self)
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}