Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMLoginItemSetEnabled - Start at Login with App Sandboxed - Xcode 6.3 (Illustration)

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

like image 756
Huynh Inc Avatar asked Jun 02 '15 04:06

Huynh Inc


1 Answers

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 enter image description here

I.2. Add helper app

I.2.1 Add a Target enter image description here

I.2.2 Choose Cocoa Application enter image description here

I.2.3 Name: StartAtLoginHelper enter image description here

I.2.4 Add completely enter image description here

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

I.3.1 Choose Main App’s Build Phases enter image description here

I.3.2 Add a new build phase enter image description here

I.3.3 Select New Copy Files Phase enter image description here

I.3.4 Copy Files section is still blank enter image description here

I.3.5 Edit Destination, Subpath & add Helper app enter image description here

I.4. Write SMLoginItemSetEnabled

I.4.1 Select main app’s MainMenu.xib enter image description here

I.4.2 Add checkbox with name “Start at Login” enter image description here

I.4.3 Set action enter image description here

I.4.4 Select main app’s Build Phases enter image description here

I.4.5 Import ServiceManagement.framework enter image description here

I.4.6 Implement SMLoginItemSetEnabled code enter image description here

II. Setting up Helper app

II.1. Application is background = YES

II.1.1 Select helper app’s Info enter image description here

II.1.2 Add row enter image description here

II.1.3 Add “Application is background only”, then set value is “YES” enter image description here

II.2. Skip Install in Release = YES

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

II.2.2 Expand Skip Install section enter image description here

II.2.3 Set “Release” row is “YES” enter image description here

II.3. Remove window

II.3.1 Select helper app’s MainMenu.xib enter image description here

II.3.2 Remove StartAtLoginHelper’s window in xib file enter image description here

II.4. Write helper code

II.4.1 Select helper app’s ApplicationDelegate.m enter image description here

II.4.2 Implement helper code enter image description here

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 enter image description here

III.1.1.2 After Code Sign In enter image description here

III.1.2 Enable App Sandbox main app

III.1.2.1 Before Enable App Sandbox enter image description here

III.1.2.2 After Enable App Sandbox enter image description here

III.1.3 Code Sign In main app

III.1.3.1 Before Code Sign In enter image description here

III.1.3.2 After Code Sign In enter image description here

III.1.4 Enable App Sandbox main app

III.1.4.1 Before Enable App Sandbox enter image description here

III.1.4.2 After Enable App Sandbox enter image description here

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

III.2.1 Archive

III.2.1.1 Select Product/ Archive enter image description here

III.2.1.2 Waiting Xcode archiving enter image description here

III.2.1.3 Select Export Button enter image description here

III.2.1.4 Select Export as a Mac Application enter image description here

III.2.1.5 Choose Desktop to Save enter image description here

III.2.1.6 StartAtLogin app on Desktop enter image description here

III.2.2 Copy app to Applications folder

III.2.2.1 Copy StartAtLogin app enter image description here

III.2.2.2 Copy StartAtLogin app to Applications folder enter image description here

III.2.3 Open app

III.2.3.1 Open StartAtLogin app enter image description here

III.2.3.2 StartAtLogin app’s UI when started enter image description here

III.2.3.3 Check Start at Login checkbox enter image description here

III.2.3.4 Log Out (or Shut Down, Restart are OK) enter image description here

III.2.3.5 Uncheck Reopen windows when logging back in, then Log Out enter image description here

like image 144
Huynh Inc Avatar answered Oct 21 '22 11:10

Huynh Inc