Hi all,

I've got problem with start at login in sandboxed environment on 10.8 (< 10.8 
is not supported now). It simply doesn't work sometimes. What does it mean 
exactly?

1. App is downloaded from the App Store
2. App is started by me/user and Start at login is checked (code below)
3. I do restart my Mac while main application is still running and application 
is not started after restart. Console contains error message that start of my 
helper is refused, because it wasn't previously launched by the user.

To fix this issue, I have to do this:

1. Start app after App Store download
2. Enable start at login in app
3. Quit application
4. Start app again manually
5. Restart Mac and now, it does work

In other words, it requires 2 steps - after enabling start at login, I have to 
quit app and start it manually. And it magically started to work then.

Any idea what can be wrong? Do I need some kind of entitlements to add to start 
my helper? According to logs, SMLoginItemSetEnabled call didn't fail and when I 
do this quit/start dance, it works. Application is currently in the Mac App 
Store (Moment), approved by Apple, I mean, correctly signed, etc. But we still 
experience this issue reported by some users.

Anyone can sched some light on this issue? What can be wrong? Googling, 
searching on SO, … but no helpful answer at all.

R.

Main application code to enable/disable/check start at login:

- (BOOL)willStartAtLogin:(NSString *)bundleID {
  NSArray * jobDicts = (__bridge NSArray *)SMCopyAllJobDictionaries( 
kSMDomainUserLaunchd );
  
  if ( (jobDicts != nil) && [jobDicts count] > 0 ) {
    BOOL bOnDemand = NO;
    
    for ( NSDictionary * job in jobDicts ) {
      
      if ( [bundleID isEqualToString:[job objectForKey:@"Label"]] ) {
        bOnDemand = [[job objectForKey:@"OnDemand"] boolValue];
        break;
      }
    }
    
    CFRelease((__bridge CFDictionaryRef)jobDicts); jobDicts = nil;
    return bOnDemand;
  }
  if ( jobDicts ) {
    CFRelease((__bridge CFDictionaryRef)jobDicts); jobDicts = nil;
  }
  return NO;
}

- (BOOL)setStartAtLogin:(NSString *)bundleID enabled:(BOOL)enabled {
  NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] 
URLByAppendingPathComponent:@"Contents/Library/LoginItems/MomentHelper.app"];
  LSRegisterURL( (__bridge CFURLRef)bundleURL, true);
  return ( SMLoginItemSetEnabled ((__bridge CFStringRef)bundleID, enabled ) == 
0 );
}

Main application entitlements are com.apple.security.app-sandbox and 
com.apple.security.network.client.

Here's the code in helper:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  BOOL alreadyRunning = NO;
  BOOL active = NO;
  NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications];
  for ( NSRunningApplication *app in running ) {
    if ( [[app bundleIdentifier] isEqualToString:@"com.tapmates.Moment"] ) {
      alreadyRunning = YES;
      active = [app isActive];
    }
  }
  if ( !alreadyRunning || !active ) {
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSArray *p = [path pathComponents];
    NSMutableArray *pathComponents = [NSMutableArray arrayWithArray:p];
    [pathComponents removeLastObject];
    [pathComponents removeLastObject];
    [pathComponents removeLastObject];
    [pathComponents addObject:@"MacOS"];
    [pathComponents addObject:@"Moment"];
    NSString *newPath = [NSString pathWithComponents:pathComponents];
    BOOL result = [[NSWorkspace sharedWorkspace] launchApplication:newPath];
  }
  [NSApp terminate:nil];
}

Helper's entitlements are com.apple.security.app-sandbox.

-- 
Robert @ Tapmates, Inc.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to