2016-02-19 118 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

相關問題