Mac OS X:在应用商店启动时启动? [英] Mac OS X: start on launch while in app store?

查看:192
本文介绍了Mac OS X:在应用商店启动时启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在系统启动时启动我的应用程序,但问题是:它已经在App Store中,所以我必须遵守一些规则,如使用沙箱。这会导致所需的函数(如 LSSharedFileListInsertItemURL 和 SMLoginItemSetEnabled )失败。在这种情况下,我应该如何处理?

I need to start my app at system launch, but the problem is: it is already in App Store, so I must follow some rules like using sandboxing. This leads to fail of desired functions like LSSharedFileListInsertItemURL and SMLoginItemSetEnabled. How should I act in this case?

推荐答案

我最近经历了同样的过程,不幸的是用沙盒它不是那么容易做它以前是。我使用非常详细的说明进行了测试,现在的在Github

I recently went through this same process, unfortunately with sandboxing it's not nearly as easy to do as it used to be. I made a test application with very detailed instructions that is now on Github

此演示应用程序和您的应用程序只有在部署时才能工作,最好在 / Applications / MyGreat

This demo application and your application will only work if they are deployed preferably in the /Applications/MyGreat.app and will not work reliably from the Xcode debug folder.

这些是我的项目的设置,与此实现完美配合。

These are the settings of my project that worked perfectly with this implementation.


  1. 创建一个新项目ARC启用

  2. Sandbox既是您的主应用程序也是帮助应用程序(如果您尚未创建帮助程序,我们很快就会获得帮助)我还启用了代码签名

  3. 由于这只是一个测试应用程序,我没有主应用程序或帮助器的活动权利

  4. 如果您尚未创建帮助应用程序。转到您的项目设置,然后单击添加目标选择一个Cocoa应用程序。命名它的东西,如MyAwesomeProjectHelper也启用ARC。 (我将其App Store类别留空)

  5. 现在选择主应用程序的目标。转到构建阶段 - >添加构建阶段 - >添加复制文件。

  6. 将目标更改为包装器。创建子路径 Contents / Library / LoginItems 仅在安装未选中时保留复制。

  1. Create a new project with ARC enabled
  2. Sandbox both your main application and helper application (if you haven't created a helper yet we will get to it shortly) I also enabled code signing
  3. Since this was just a test application I had no active entitlements for either the main application or the helper
  4. If you haven't already, create a helper app. Go to your project settings and click "Add Target" choose a Cocoa Application. Name it something such as MyAwesomeProjectHelper also with ARC enabled. (I left its "App Store Category" blank)
  5. Now select the Target of your main application. Go to Build Phases -> Add Build Phase -> Add Copy Files.
  6. Change the Destination to Wrapper. Make the Subpath Contents/Library/LoginItems leave Copy only when installing unchecked. Drag your helper application from Products on the left into the tableview.



主应用程式代码设定



Main Application code setup


  1. 将ServiceManagement.framework导入到您的主应用程序(而不是您的帮助程序),并包括 #import< ServiceManagement / ServiceManagement.h> 在您的.h文件中

  2. 从Github抓取 StartAtLoginController 。这是一个易于使用的类由Alex Zielenski处理添加,删除和查询登录项的复杂性。将 StartAtLoginController.h 导入到您的h文件中。

  3. 创建要控制此设置的任何界面。

  4. 如果您的应用程式自动启用这项功能,将会遭到拒绝(按照指南#2.26) - (IBAction)checkChanged:(id)sender 我做了一个简单的复选框绑定到StandardUserDefaults。 (如果你选择做别的事,你的实现可能会有所不同)。我还绑定复选框到 IBOutlet NSButton * loginCheck; ,以确定它的状态。这也可以通过 [[NSUserDefaults standardUserDefaults] boolForKey:YourKey]

  5. 实现与此类似的代码。 m文件。

  1. Import ServiceManagement.framework into your main application(not your helper) and include #import <ServiceManagement/ServiceManagement.h> in your .h file
  2. Grab StartAtLoginController from Github. This is an easy to use class by Alex Zielenski to deal with the complications of adding, removing and querying login items. Import StartAtLoginController.h into your h file.
  3. Create whatever interface you want to for controlling this setting. If your application automatically enables this it will be denied from the Mac App Store(per guideline #2.26)
  4. Implement a method such as - (IBAction)checkChanged:(id)sender I made a simple checkbox tied to the StandardUserDefaults. (If you chose to do something else your implementation for this may vary.) I also bound the checkbox to IBOutlet NSButton *loginCheck; in order to determine it's state. This could also be done through [[NSUserDefaults standardUserDefaults] boolForKey:YourKey]
  5. Implement code similar to this in your .m file.

StartAtLoginController *loginController = [[StartAtLoginController alloc] init];
[loginController setBundle:[NSBundle bundleWithPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Library/LoginItems/HelperApp.app"]]]; 
// Change "HelperApp.app" to the name of your helper

if ([loginCheck state]) {
    if (![loginController startAtLogin]) {
        [loginController setStartAtLogin: YES];
    }
} else {
    if ([loginController startAtLogin]) {
        [loginController setStartAtLogin:NO];
    }
}


  • 正如你在这个项目中可以看到的,还有一些其他的方法可以使用,如:

  • That's it. As you can see in this project there are some other methods you may want to use such as:

    if ([loginController startAtLogin]) {
        NSLog(@"Error");
    }
    

    在启用或禁用设置后进行检查以确保设置正确。或者:

    For checking after you enable or disable the setting to make sure it worked correctly. Or this:

    BOOL startsAtLogin = [loginController startAtLogin];
    if (startsAtLogin) {
        // Do stuff
    }
    



    To do something if the login helper is enabled.



    帮助程序代码设置



    请务必用您的实现来大力测试此代码。

    Helper Application code setup

    Make sure to test this code vigorously with your implementation.


    1. 通过导航到HelperApp使您的帮助应用程序成为UIElement默认情况下,.plist位于支持文件组中。在底部添加一行,其中键 Application is agent(UIElement) YES 作为值这将禁止应用程序在每次用户启用登录时启用时停止闪烁停靠图标)我还删除了界面生成器中的应用程序代理以外的所有内容

    2. 擦除默认方法 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 并替换为 - (void)applicationWillFinishLaunching:
    1. Make your helper application a UIElement by navigating to HelperApp.plist located in the Supporting Files group by default. Add a line at the bottom with the Key Application is agent (UIElement) and YES as the Value (this will suppress the application from flashing a dock icon each time the user enables launch at login) I also deleted everything except for the App Delegate in interface builder
    2. Erase the default method - (void)applicationDidFinishLaunching:(NSNotification *)aNotification and replace it with - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
    3. Within this method implement code similar to this.

    NSString *appPath = [[[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
    // This string takes you from MyGreat.App/Contents/Library/LoginItems/MyHelper.app to MyGreat.App This is an obnoxious but dynamic way to do this since that specific Subpath is required
    NSString *binaryPath = [[NSBundle bundleWithPath:appPath] executablePath]; // This gets the binary executable within your main application
    [[NSWorkspace sharedWorkspace] launchApplication:binaryPath];
    [NSApp terminate:nil];
    

    此代码找到您的主应用程序,确定它的二进制可执行文件(需要在沙箱中启动应用程序)


  • This code finds your main application, determines it's binary executable(required to launch the application within the sandbox) opens your application, then quits



    部署 h3>

    在为自己或Mac App Store部署应用程序时,最后一件应该做的事情是从已归档的项目中删除您的帮助应用程序。通过导航到您的HelperApp的目标 - >构建设置 - >跳过安装并设置为释放。 Apple提供了更多信息(http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/000-About_Xcode/about.html)

    Deploy

    The last thing you should do when deploying your application for yourself or to the Mac App store is remove your Helper app from the Archived items. Do this by navigating to the Target of your HelperApp -> Build Settings -> Skip Install and set Yes for Release. Apple provides more information at (http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/000-About_Xcode/about.html)

    这篇关于Mac OS X:在应用商店启动时启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆