2016-02-19 71 views
0

我正在开发将要在应用程序商店外部发布的OSX应用程序。我已经存档并创建了pkg文件,但是当我安装该应用程序时,它不会自动启动。我必须从启动板手动启动它。以下是我在appdelegate中添加的代码,以便在启动时显示它。在Mac OS中安装后自动启动应用程序El Capitan

- (void)installAppIntoLoginItems { 

    if (![self appIsInLoginItems]) { 
     // Get the LoginItems list. 
     LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); 
     if (loginItemsRef == nil) return; 

     // Add the app to the LoginItems list. 
     LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, (__bridge CFURLRef)helperAppURL, NULL, NULL); 
     if (itemRef) { 
      CFRelease(itemRef); 
     } 
     CFRelease(loginItemsRef); 
    } 
    else { 
     // App is in the LoginItems List 
     NSLog(@"App is already in LoginItems List"); 
    } 
} 



- (BOOL) appIsInLoginItems { 
    // See if the app is currently in LoginItems. 
    LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; 
    // Store away that boolean. 
    BOOL isInList = (itemRef != nil); 
    // Release the reference if it exists. 
    if (itemRef != nil) CFRelease(itemRef); 

    return isInList; 
} 


- (LSSharedFileListItemRef)itemRefInLoginItems { 
    LSSharedFileListItemRef itemRef = nil; 
    CFURLRef itemUrl = NULL; 

    // Get the LoginItems list. 
    LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); 
    if (loginItemsRef == nil) return nil; 
    // Iterate over the LoginItems. 
    NSArray *loginItems = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil); 
    for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { 
     // Get the current LoginItem and resolve its URL. 
     LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex]; 
//  if (LSSharedFileListItemResolve(currentItemRef, 0, (CFURLRef *) &itemUrl, NULL) == noErr) { //Replacing deprecated method 
      if(LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, NULL) == noErr) { 
      // Compare the URLs for the current LoginItem and the app. 
      if ([(__bridge NSURL*)itemUrl isEqual:helperAppURL]) { 
       // Save the LoginItem reference. 
       itemRef = currentItemRef; 
      } 
     } 
    } 
    // Retain the LoginItem reference. 
    if (itemRef != nil) CFRetain(itemRef); 
    // Release the LoginItems lists. 
    CFRelease(loginItemsRef); 

    return itemRef; 
} 

我是否需要使用其他方法?有没有我可以参考的样本?

回答

0

我能解决使用SMLoginItemSetEnabled方法上登录问题的自动启动应用程序,并能实现自动启动后,安装使用后脚本

 if (!SMLoginItemSetEnabled ((__bridge CFStringRef)helperAppBundleIdentifier, YES)) 
     { 
       NSAlert *alert = [NSAlert alertWithMessageText:@"An error ocurred" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Couldn't add Helper App to launch at login item list."]; 
       [alert runModal]; 
      } 
0

你可以使用展开剂:

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

http://launchd.info

我想有它作为登录项目的优势在于,用户可以大概判断出如何删除它自己。如果您使用LaunchAgent,则可能必须在应用程序中构建“登录时启动”首选项。

+0

我的应用程序只在菜单栏显示,它不来在码头上。一旦我手动启动它就会出现并被添加到登录项中。我的应用程序安装完成后应该如何触发它才能启动? – Gamerlegend

相关问题