<span id="Label3"></p><p><p>Monkey original, Welcome Reprint. Reproduced please specify: reproduced from cocos2der-csdn, thank you!<br>Original Address: http://blog.csdn.net/cocos2der/article/details/52104828</p></p><p><p>You can learn the instructions in the Mac Developer Library about how to add the start-up function to your app under Mac.</p></p> <blockquote> <blockquote> <p>There is ways to add a login item:using the Service Management framework, and using a shared file list</p> <p>Login items installed using the Service Management framework is not visible on System Preferences and can only be removed By the application, that installed Them.</p> <p>Login items installed using a shared file list is visible in System Preferences; Users has direct control over Them. If You use this API, your login item can is disabled by the user, so any other application that communicates with it it sh Ould has reasonable fallback behavior in case the login item is Disabled.</p> </blockquote> </blockquote><p><p>As you can see, Apple recommends two ways: the Service Management framework and the shared file List.</p></p><p><p>There are differences between the two ways:</p></p> <ul> <ul> <li><p>Using the service Management framework is not visible in the System's login Entries. Only uninstall the app to remove the login</p></li> <li><p>Using the shared file list is visible in the System's login entry. Users can control them directly on the Panel. (If You use this API, your login item can is disabled by the user, so any other application that communicates with it it s Hould has reasonable fallback behavior in case the login item is disabled.) The main idea is that the API is hidden, so the API is heavily dep on OS X 10.10 Systems. recated</p></li> </ul> </ul><p><p>The following is my main introduction to the use of the service Management framework to add power-on Self-boot.</p></p>first, Create the main project<p><p>Create a new Mainapp x engineering app<br></p></p>second, Add auto Start target <ul> <ul> <li><p>We need to register a helper Target app to use as a boot from our mainapp, click on the plus sign below Targets.<br></p></li> <li><p>Add a new OS X Application. Name is Mainapphelper<br></p></li> </ul> </ul>third, Set Configuration Properties <ul> <ul> <li><p>Remove Windows from Mainapphelper so that it does not have a window to display.<br></p></li> <li><p>Set Mainapphelper info in application is background only Yes</p></li> <li><p>Set Mainapphelper in Build setting skip install is Yes<br></p></li> <li><p>Add CopyFile to Contents/library/loginitems in Mainapp<br></p></li> <li><p>Set the build Setting under Mainapp strip Debug Symbols During Copy to no, This is the default for no</p></li> <li><p>Open the app Sandbox for Mainapp and mainapphelper, respectively<br></p></li> </ul> </ul>Iv. Adding the startup code <ul> <ul> <li>Please add the Startupappwhenlogin function to your own situation mainapp the main project, the program after the operation according to the situation call to turn on or off the self-boot, notice that the Bundleid changes to your own, as well as the name of the main app. If you are not sure whether the name is your own project name, you can directly right-click your own ipa, display the package content, look at the folder inside you will Understand.</li> </ul> </ul><pre class="prettyprint"><code class=" hljs cs">Func Startupappwhenlogin (startup:bool) {<span class="hljs-comment"><span class="hljs-comment">//here please fill in your own Heler Bundleid</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> let</span>Launcherappidentifier =<span class="hljs-string"><span class="hljs-string">"com.cocos2dev.MainApp.MainAppHelper"</span></span> <span class="hljs-comment"><span class="hljs-comment">//start registration/cancel Startup Item</span></span>smloginitemsetenabled (launcherappidentifier, Startup)<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Startedatlogin =<span class="hljs-keyword"><span class="hljs-keyword">false</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>App<span class="hljs-keyword"><span class="hljs-keyword">inch</span></span>Nsworkspace.sharedworkspace (). Runningapplications {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>App.bundleidentifier = = Launcherappidentifier {startedatlogin =<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>} }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>Startedatlogin {nsdistributednotificationcenter.defaultcenter (). Postnotificationname (<span class="hljs-string"><span class="hljs-string">"killhelper"</span></span>,<span class="hljs-keyword"><span class="hljs-keyword">Object</span></span>: Nsbundle.mainbundle (). bundleidentifier!) } }</code></pre> <ul> <ul> <li>In mainapphelper, Modify the appdelegate to the following code:</li> </ul> </ul><pre class="prettyprint"><code class=" hljs avrasm">Import Cocoa@nsapplicationmainclass appdelegate:nsobject, nsapplicationdelegate {func applicationdidfinishlaunching (anotification:nsnotification) {//Insert code Initialize your application Let Mainappidentifier =<span class="hljs-string"><span class="hljs-string">"com.cocos2dev.MainApp"</span></span>Let running = Nsworkspace<span class="hljs-preprocessor"><span class="hljs-preprocessor">. SharedWorkspace</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Runningapplications</span></span>var alreadyrunning = False for app<span class="hljs-keyword"><span class="hljs-keyword">inch</span></span>Running {if App<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Bundleidentifier</span></span>= = Mainappidentifier {alreadyrunning = True<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>}} if!alreadyrunning {nsdistributednotificationcenter<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Defaultcenter</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Addobserver</span></span>(self, selector:<span class="hljs-string"><span class="hljs-string">"terminate"</span></span>, name:<span class="hljs-string"><span class="hljs-string">"killhelper"</span></span>, object:mainappidentifier) let path = NSBundle<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Mainbundle</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Bundlepath</span></span>As nsstring var components = path<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Pathcomponents</span></span>Components<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Removelast</span></span>() components<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Removelast</span></span>() components<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Removelast</span></span>() components<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"MacOS"</span></span>) components<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Append</span></span>(<span class="hljs-string"><span class="hljs-string">"mainapp"</span></span>)//main app name Let NewPath = NSString<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Pathwithcomponents</span></span>(components) Nsworkspace<span class="hljs-preprocessor"><span class="hljs-preprocessor">. SharedWorkspace</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Launchapplication</span></span>(newpath)} else {self<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Terminate</span></span>()}} func applicationwillterminate (anotification:nsnotification) {//Insert code here to tear down Your application} func terminate () {//NSLog (<span class="hljs-string"><span class="hljs-string">"I ll be back!"</span></span>) Nsapp<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Terminate</span></span>(nil)}}</code></pre><p><p>The above is a self-launching app Practice. Of course if you have a Mac Store on the shelves, it is recommended that you do not turn on self-booting by default, put it in settings, and let the user choose to turn on/off.</p></p> <blockquote> <blockquote> <p>Reference Blog<br>Mac Developer Library<br>First OS X tutorial:how to launch a OS x app at login?<br>Implement program start-up under Sandbox sandbox</p> </blockquote> </blockquote> <p><p>Add Power-on-startup (Swift) to your MacOS app</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service