![](/img/trans.png)
[英]ios-deploy fail to install on Mac OS X El Capitan 10.11
[英]Automatically start app after install in Mac OS El Capitan
我正在開發OSX應用,該應用將在應用商店外部分發。 我已經存檔並創建了pkg文件,但是當我安裝該應用程序時,它不會自動啟動。 我必須從啟動板手動啟動它。 下面是我在appdelegate中添加的代碼,用於在啟動時顯示它。
- (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;
}
我還必須使用其他替代方法嗎? 有沒有我可以參考的樣本?
您可以使用啟動代理:
我想將其作為登錄項的好處在於,用戶可能會想出如何自己刪除它。 如果使用LaunchAgent,則可能必須在應用程序中構建“登錄時啟動”首選項。
我能夠使用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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.