2011-03-27 6 views
5

me trataron¿Cómo se puede agregar una aplicación Cocoa como un elemento de inicio de sesión global?

LSSharedFileListRef globalLoginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL); 
if (globalLoginItems) { 
    LSSharedFileListItemRef ourLoginItem = LSSharedFileListInsertItemURL(globalLoginItems, 
                     kLSSharedFileListItemLast, 
                     NULL, NULL, 
                     (CFURLRef)[[NSBundle mainBundle] bundleURL], 
                     NULL, NULL); 
    if (ourLoginItem) { 
     CFRelease(ourLoginItem); 
    } else { 
     NSLog(@"Could not insert ourselves as a global login item"); 
    } 

    CFRelease(globalLoginItems); 
} else { 
    NSLog(@"Could not get the global login items"); 
} 

LSSharedFileListInsertItemURL() simplemente devuelve NULL cuando construí y corriendo la aplicación. ¿Hay algo más que deba hacer? ¿Algún tipo de autorización?

NOTA: El caso de uso aquí es para elementos de inicio de sesión globales, es decir, kLSSharedFileListGlobalLoginItems y no kLSSharedFileListSessionLoginItems.

Respuesta

5

Me puse a trabajar. Todo lo que tenía que hacer era añadir estas líneas antes de insertar la aplicación en los elementos de inicio:

AuthorizationRef auth = NULL; 
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); 
LSSharedFileListSetAuthorization(globalLoginItems, auth); 

la documentación para LSSharedFileListSetAuthorization decir que tenemos para obtener el derecho system.global-login-items para esto, pero funcionó sin embargo!

Pero esto no funcionará si el usuario no es un administrador. Para que funcione entonces también, vas a tener que hacer esto:

AuthorizationItem right[1] = {{"system.global-login-items.", 0, NULL, 0}}; 
AuthorizationRights setOfRights = {1, right}; 
AuthorizationRef auth = NULL; 
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); 


AuthorizationCopyRights(auth, &setOfRights, kAuthorizationEmptyEnvironment, 
           (kAuthorizationFlagDefaults 
           | kAuthorizationFlagInteractionAllowed 
           | kAuthorizationFlagExtendRights), NULL); 

También es aconsejable consultar a the docs para más detalles.

1

Esto funciona para mí:

NSString * appPath = [[NSBundle mainBundle] bundlePath]; 

// This will retrieve the path for the application 
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath]; 

// Create a reference to the shared file list. 
// We are adding it to the current user only. 
// If we want to add it all users, use 
// kLSSharedFileListGlobalLoginItems instead of 
//kLSSharedFileListSessionLoginItems 
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); 
if (loginItems) { 
    //Insert an item to the list. 
    LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,kLSSharedFileListItemLast, NULL, NULL,url, NULL, NULL); 
    if (item){ 
     CFRelease(item); 
    } 
} 

CFRelease(loginItems); 
+0

Gracias, pero kLSSharedFileListSessionLoginItems también funciona para mí. Quiero que esto funcione para todos los usuarios. Usando kLSSharedFileListGlobalLoginItems, eso es. – Plumenator

0
NSString * appPath = [[NSBundle mainBundle] bundlePath]; 

     // This will retrieve the path for the application 
     CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath]; 

     LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL); 
     if (loginItems) { 
      //Insert an item to the list. 
      LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,kLSSharedFileListItemLast, NULL, NULL,url, NULL, NULL); 
      if (item){ 
       CFRelease(item); 
      } 
     } 

     CFRelease(loginItems); 

no funciona este código? Reemplacé kLSSharedFileListSessionLoginItems con kLSSharedFileListGlobalLoginItems

+0

Eso es correcto. Obtengo el artículo como NULL. – Plumenator

Cuestiones relacionadas