2016-02-11 13 views
9

पर स्टार्टअप पर स्विफ्ट कोको ऐप लॉन्च करें मुझे एक ऐसा फ़ंक्शन लिखना होगा जो ओएस एक्स 10.11 पर स्टार्टअप आइटम पर मेरा एप्लिकेशन जोड़ता है। यही कारण है कि मैं इस समय मिल गया है:ओएस एक्स 10.11

func applicationIsInStartUpItems() -> Bool { 
    return (itemReferencesInLoginItems().existingReference != nil) 
} 

func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) { 

    if let appUrl : NSURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { 
     let loginItemsRef = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileListRef? 
     if loginItemsRef != nil { 
      let loginItems: NSArray = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray 
      if(loginItems.count > 0) { 
       let lastItemRef: LSSharedFileListItemRef = loginItems.lastObject as! LSSharedFileListItemRef 
       for var i = 0; i < loginItems.count; ++i { 
        let currentItemRef: LSSharedFileListItemRef = loginItems.objectAtIndex(i) as! LSSharedFileListItemRef 
        if let itemURL = LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, nil) { 
         if (itemURL.takeRetainedValue() as NSURL).isEqual(appUrl) { 
          return (currentItemRef, lastItemRef) 
         } 
        } 
       } 
       return (nil, lastItemRef) 
      } else { 
       let addatstart: LSSharedFileListItemRef = kLSSharedFileListItemBeforeFirst.takeRetainedValue() 
       return(nil,addatstart) 
      } 
     } 
    } 
    return (nil, nil) 
} 

func toggleLaunchAtStartup() { 
    let itemReferences = itemReferencesInLoginItems() 
    let shouldBeToggled = (itemReferences.existingReference == nil) 
    if let loginItemsRef = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileListRef? { 
     if shouldBeToggled { 
      if let appUrl : CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { 
       LSSharedFileListInsertItemURL(loginItemsRef, itemReferences.lastReference, nil, nil, appUrl, nil, nil) 
      } 
     } else { 
      if let itemRef = itemReferences.existingReference { 
       LSSharedFileListItemRemove(loginItemsRef,itemRef); 
      } 
     } 
    } 
} 

लेकिन LSSharedFileListCreate, LSSharedFileListInsertItemURL, LSSharedFileListItemRemove, kLSSharedFileListItemBeforeFirst, LSSharedFileListItemCopyResolvedURL, LSSharedFileListCopySnapshot, kLSSharedFileListSessionLoginItems ओएस एक्स 10.11 में पदावनत किया गया। मैक ओएस के नवीनतम संस्करण पर यह काम कैसे करें? इस कोड को कैसे बदलें या फिर से लिखें?

उत्तर

5

स्विफ्ट 3.0 में यह इस तरह दिखता है:

अपने मुख्य आवेदन AppDelegate में:

func applicationDidFinishLaunching(_ aNotification: Notification) { 
    // Check if the launcher app is started 
    var startedAtLogin = false 
    for app in NSWorkspace.shared().runningApplications { 
     if app.bundleIdentifier == NCConstants.launcherApplicationIdentifier { 
      startedAtLogin = true 
     } 
    } 

    // If the app's started, post to the notification center to kill the launcher app 
    if startedAtLogin { 
     DistributedNotificationCenter.default().postNotificationName(NCConstants.KILLME, object: Bundle.main.bundleIdentifier, userInfo: nil, options: DistributedNotificationCenter.Options.deliverImmediately) 
    } 
} 

लांचर आवेदन AppDelegate में:

func applicationDidFinishLaunching(_ aNotification: Notification) { 

    let mainAppIdentifier = "<main-app-bundle-id>" 
    let running = NSWorkspace.shared().runningApplications 
    var alreadyRunning = false 

    // loop through running apps - check if the Main application is running 
    for app in running { 
     if app.bundleIdentifier == mainAppIdentifier { 
      alreadyRunning = true 
      break 
     } 
    } 

    if !alreadyRunning { 
     // Register for the notification killme 
     DistributedNotificationCenter.default().addObserver(self, selector: #selector(self.terminate), name: NCConstants.KILLME, object: mainAppIdentifier) 

     // Get the path of the current app and navigate through them to find the Main Application 
     let path = Bundle.main.bundlePath as NSString 
     var components = path.pathComponents 
     components.removeLast(3) 
     components.append("MacOS") 
     components.append("<your-app-name>") 

     let newPath = NSString.path(withComponents: components) 

     // Launch the Main application 
     NSWorkspace.shared().launchApplication(newPath) 
    } 
    else { 
     // Main application is already running 
     self.terminate() 
    } 

} 

func terminate() { 
    print("Terminate application") 
    NSApp.terminate(nil) 
} 

आखिरकार, मुख्य आवेदन में मैं एक यूजर इंटरफेस एक टॉगल बटन के साथ जोड़ा। उपयोगकर्ता लॉगिन पर ऐप लॉन्च करना चुन सकता है या नहीं। पसंद UserDefaults में संग्रहीत है। देखें नियंत्रक में:

@IBAction func toggleLaunchAtLogin(_ sender: Any) { 
    if toggleOpenAppLogin.selectedSegment == 0 { 
     if !SMLoginItemSetEnabled(NCConstants.launcherApplicationIdentifier as CFString, true) { 
      print("The login item was not successfull") 
      toggleOpenAppLogin.setSelected(true, forSegment: 1) 
     } 
     else { 
      UserDefaults.standard.set("true", forKey: "appLoginStart") 
     } 
    } 
    else { 
     if !SMLoginItemSetEnabled(NCConstants.launcherApplicationIdentifier as CFString, false) { 
      print("The login item was not successfull") 
      toggleOpenAppLogin.setSelected(true, forSegment: 0) 
     } 
     else { 
      UserDefaults.standard.set("false", forKey: "appLoginStart") 
     } 
    } 

} 

मुझे आशा है कि यह किसी को कर सकते हैं।

+0

अच्छा और वास्तव में जवाब देने में बहुत समय लगता है! –

+0

'NCConstants.KILLME' कहां परिभाषित किया गया है? –

+0

लॉन्चर एप्लिकेशन और मुख्य एप्लिकेशन में। आप इसे इस तरह परिभाषित कर सकते हैं: कक्षा एनसीसीओन्स्टेंट्स { // निरंतर स्थिर बताएं किलीएम = अधिसूचना। नाम ("हत्यारा")} – Thomas

10

अब आपको सेवा प्रबंधन ढांचे का उपयोग करना होगा। आप एक सहायक एप्लिकेशन बनाते हैं जिसे आप अपने एप्लिकेशन बंडल में जोड़ते हैं और इसका काम आपके मुख्य एप्लिकेशन को लॉन्च करने के लिए कोड चलाने के लिए है। आप के लिए कुछ संसाधन:

  • टिम स्च्रोदर यह कैसे किया जाता
  • एलेक्स Zielenski एक open source project इस
  • साथ सहायता करने के लिए मैं मैन्युअल
यह बनाने पर एक video tutorial कर दिया है है पर एक excellent blog पोस्ट है
+0

उत्तर स्विफ्ट से संबंधित नहीं है, लेकिन सामान्य दिशा – zxcat

+0

देता है आपका ट्यूटोरियल शानदार है। मैंने अब यह कोशिश की है और सबकुछ कुछ छोटे तथ्य के अलावा वर्णित है जैसा कि यह काम नहीं करता है। मेरे पास कंसोल पर यह त्रुटि है 'सेवा द्वारा निर्दिष्ट CFBundleIdentifier को हल नहीं किया जा सकता है' जिसके बाद मेरी सहायता फ़ाइल के पहचानकर्ता के पास ट्रिपल चेक किया गया है, सही ढंग से टाइप किया गया है। इसकी अपेक्षा की जाती है क्योंकि मेरे अनुभव में ऐप्पल से एक एपीआई काम नहीं करता है जब आप पहली बार कोशिश करते हैं। – SpaceDog

+0

@ स्पेसडॉग क्या आपने गिटहब से स्रोत खींचने और उस प्रोजेक्ट को आजमाने की कोशिश की है? –