SMLoginItemSetEnabled - Start at Login with App Sa

2020-06-20 03:37发布

问题:

How to make app with Start at Login function when app sandboxed?

回答1:

Thank you CORY BOHON, who created the following tutorial:

http://martiancraft.com/blog/2015/01/login-items/

I'll summary steps I have done:

  1. Add StartAtLogin project

  2. Import StartAtLoginHelper into StartAtLogin project

  3. In StartAtLogin Build Phase, add Copy Files is StartAtLoginHelper with below config:

    3.1. Destination: Wrapper

    3.2. Subpath: Contents/Library/LoginItems

  4. In StartAtLoginHelper, choose info.plist, add row "Application is background only" = YES

  5. In StartAtLoginHelper Build Settings, change Skip Install in Release = YES

  6. In StartAtLoginHelper, implement helper code

    NSArray *pathComponents = [[[NSBundle mainBundle] bundlePath] pathComponents]; pathComponents = [pathComponents subarrayWithRange:NSMakeRange(0, [pathComponents count] - 4)]; NSString *path = [NSString pathWithComponents:pathComponents]; [[NSWorkspace sharedWorkspace] launchApplication:path]; [NSApp terminate:nil];

NSArray *a = nil;

  1. In StartAtLogin, implement function call helper app

    SMLoginItemSetEnabled((__bridge CFStringRef)@"com.giauhuynh.StartAtLoginHelper", sender.state == NSOnState);

  2. Code sign in + enable App Sandbox

  3. Archive + Copy app to Applications folder and run

and my project:

http://s000.tinyupload.com/?file_id=03236580059305049920

Below is step by step fully (with illustration)

I. Setting up Main app

I.1. Add main app with name: StartAtLogin

I.2. Add helper app

I.2.1 Add a Target

I.2.2 Choose Cocoa Application

I.2.3 Name: StartAtLoginHelper

I.2.4 Add completely

I.3. add helper app to main app:”Contents/Library/LoginItems”

I.3.1 Choose Main App’s Build Phases

I.3.2 Add a new build phase

I.3.3 Select New Copy Files Phase

I.3.4 Copy Files section is still blank

I.3.5 Edit Destination, Subpath & add Helper app

I.4. Write SMLoginItemSetEnabled

I.4.1 Select main app’s MainMenu.xib

I.4.2 Add checkbox with name “Start at Login”

I.4.3 Set action

I.4.4 Select main app’s Build Phases

I.4.5 Import ServiceManagement.framework

I.4.6 Implement SMLoginItemSetEnabled code

II. Setting up Helper app

II.1. Application is background = YES

II.1.1 Select helper app’s Info

II.1.2 Add row

II.1.3 Add “Application is background only”, then set value is “YES”

II.2. Skip Install in Release = YES

II.2.1 Select helper app’s Build Settings, input word “skip” in search field

II.2.2 Expand Skip Install section

II.2.3 Set “Release” row is “YES”

II.3. Remove window

II.3.1 Select helper app’s MainMenu.xib

II.3.2 Remove StartAtLoginHelper’s window in xib file

II.4. Write helper code

II.4.1 Select helper app’s ApplicationDelegate.m

II.4.2 Implement helper code

III. Build and Testing

III.1. Code sign in + App Sandbox both Main app and Helper app

III.1.1 Code Sign In main app

III.1.1.1 Before Code Sign In

III.1.1.2 After Code Sign In

III.1.2 Enable App Sandbox main app

III.1.2.1 Before Enable App Sandbox

III.1.2.2 After Enable App Sandbox

III.1.3 Code Sign In main app

III.1.3.1 Before Code Sign In

III.1.3.2 After Code Sign In

III.1.4 Enable App Sandbox main app

III.1.4.1 Before Enable App Sandbox

III.1.4.2 After Enable App Sandbox

III.2. Archive + Copy app to Applications folder.

III.2.1 Archive

III.2.1.1 Select Product/ Archive

III.2.1.2 Waiting Xcode archiving

III.2.1.3 Select Export Button

III.2.1.4 Select Export as a Mac Application

III.2.1.5 Choose Desktop to Save

III.2.1.6 StartAtLogin app on Desktop

III.2.2 Copy app to Applications folder

III.2.2.1 Copy StartAtLogin app

III.2.2.2 Copy StartAtLogin app to Applications folder

III.2.3 Open app

III.2.3.1 Open StartAtLogin app

III.2.3.2 StartAtLogin app’s UI when started

III.2.3.3 Check Start at Login checkbox

III.2.3.4 Log Out (or Shut Down, Restart are OK)

III.2.3.5 Uncheck Reopen windows when logging back in, then Log Out