Skip to content

Commit

Permalink
launch at login
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanxue.yang committed Oct 19, 2018
1 parent 4d2d0fd commit ffac0fe
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 74 deletions.
3 changes: 1 addition & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ platform :osx, '10.12'
target 'V2rayU' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

pod 'LaunchAtLogin'

# Pods for V2rayU
pod 'Alamofire', '~> 4.7'
pod 'SwiftyJSON', '~> 4.0'
pod 'Preferences'

end
10 changes: 5 additions & 5 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
PODS:
- Alamofire (4.7.3)
- LaunchAtLogin (2.3.0)
- Preferences (0.2.0)
- SwiftyJSON (4.2.0)

DEPENDENCIES:
- Alamofire (~> 4.7)
- LaunchAtLogin
- Preferences
- SwiftyJSON (~> 4.0)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- Alamofire
- LaunchAtLogin
- Preferences
- SwiftyJSON

SPEC CHECKSUMS:
Alamofire: c7287b6e5d7da964a70935e5db17046b7fde6568
LaunchAtLogin: 7ca1e1ab83a07ce89443d321eea20451658181d1
Preferences: ebc036a176a298cde835155c73251173768a2f8d
SwiftyJSON: c4bcba26dd9ec7a027fc8eade48e2c911f229e96

PODFILE CHECKSUM: 5f68cfc19b1d01d7c93f89ddcbd513b0f79ff679
PODFILE CHECKSUM: fb7a8a55278116ebdd4990811b0c58eba7bb91b1

COCOAPODS: 1.6.0.beta.1
171 changes: 142 additions & 29 deletions V2rayU.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<key>orderHint</key>
<integer>6</integer>
</dict>
<key>V2rayuHelper.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>5</integer>
</dict>
</dict>
</dict>
</plist>
34 changes: 27 additions & 7 deletions V2rayU/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Cocoa
import ServiceManagement
import os.log
import LaunchAtLogin
let launcherAppIdentifier = "net.yanue.v2rayu.v2rayuHelper"

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -19,15 +19,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
os_log("v2rayu init.")
// showAccessibilityDeniedAlert()

print(LaunchAtLogin.isEnabled)
//=> false
let startedAtLogin = NSWorkspace.shared.runningApplications.contains {
$0.bundleIdentifier == launcherAppIdentifier
}

LaunchAtLogin.isEnabled = true
print("startedAtLogin",startedAtLogin)
os_log("startedAtLogin", startedAtLogin)
if startedAtLogin {
DistributedNotificationCenter.default().post(name: Notification.Name("terminateV2ray"), object: Bundle.main.bundleIdentifier!)
}

print(LaunchAtLogin.isEnabled)
//=> true

// 定义NSUserNotification
let userNotification = NSUserNotification()
userNotification.title = "消息Title"
Expand All @@ -48,6 +51,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// StartV2rayCore()
}

func showAccessibilityDeniedAlert() {
NSApplication.shared.activate(ignoringOtherApps: true)

let alert: NSAlert = NSAlert()
alert.messageText = NSLocalizedString("alert.accessibility_disabled_message", comment: "Accessibility permissions for Shifty have been disabled")
alert.informativeText = NSLocalizedString("alert.accessibility_disabled_informative", comment: "Accessibility must be allowed to enable website shifting. Grant access to Shifty in Security & Privacy preferences, located in System Preferences.")
alert.alertStyle = NSAlert.Style.warning
alert.addButton(withTitle: NSLocalizedString("alert.open_preferences", comment: "Open System Preferences"))
alert.addButton(withTitle: NSLocalizedString("alert.not_now", comment: "Not now"))
if alert.runModal() == .alertFirstButtonReturn {
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!)
NSLog("Open System Preferences button clicked")
} else {
NSLog("Not now button clicked")
}
}


func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
Expand Down
2 changes: 2 additions & 0 deletions V2rayU/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
Expand Down
21 changes: 10 additions & 11 deletions V2rayU/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ class MenuController:NSObject,NSMenuDelegate {
}

@IBAction func toggleStartAtLogin(_ sender: NSMenuItem) {
print("toggle",sender.state)

// if sender.state.rawValue == 1 {
// sender.state = .off
// // off
// SMLoginItemSetEnabled(launcherAppIdentifier as CFString, false)
// } else {
// sender.state = .on
// // on
// SMLoginItemSetEnabled(launcherAppIdentifier as CFString, false)
// }
if sender.state.rawValue == 1 {
sender.state = .off
// off
SMLoginItemSetEnabled(launcherAppIdentifier as CFString, false)
} else {
sender.state = .on
// on
SMLoginItemSetEnabled(launcherAppIdentifier as CFString, true)
}
}

@IBAction func start(_ sender: NSMenuItem) {
guard let v2ray = V2rayServer.loadSelectedItem() else {
NSLog("v2ray config not fould")
Expand Down
20 changes: 0 additions & 20 deletions V2rayU/unzip1.sh

This file was deleted.

64 changes: 64 additions & 0 deletions V2rayuHelper/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// AppDelegate.swift
// V2rayuHelper
//
// Created by yanue on 2018/10/19.
// Copyright © 2018 yanue. All rights reserved.
//

import Cocoa

class V2rayuHelperApplication: 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("terminateV2ray"), 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) {
print("helper app terminated")
// Insert code here to tear down your application
}


}

32 changes: 32 additions & 0 deletions V2rayuHelper/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>net.yanue.v2rayu.v2rayuHelper</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>2</string>
<key>LSBackgroundOnly</key>
<true/>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
<true/>
<key>NSPrincipalClass</key>
<string>V2rayuHelper.V2rayuHelperApplication</string>
</dict>
</plist>
File renamed without changes.

0 comments on commit ffac0fe

Please sign in to comment.