![](/img/trans.png)
[英]How can I tell my Cocoa application to quit from within the application itself?
[英]How can a Cocoa application add itself as a global login item?
我試過了
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()剛返回了NULL。 還有什么我需要做的嗎? 某種授權?
注意:這里的用例適用於全局登錄項目,即使用kLSSharedFileListGlobalLoginItems而不是kLSSharedFileListSessionLoginItems。
我工作了。 我要做的就是在將應用程序插入登錄項之前添加以下行:
AuthorizationRef auth = NULL;
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth);
LSSharedFileListSetAuthorization(globalLoginItems, auth);
LSSharedFileListSetAuthorization
的文檔說,我們必須為此找到正確的system.global-login-items
,但是它仍然有效!
但是,如果用戶不是管理員,這將失敗。 為了使其正常工作,您必須執行以下操作:
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);
建議參考文檔以獲取詳細信息。
這對我有用:
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);
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);
該代碼不起作用? 我將kLSSharedFileListSessionLoginItems替換為kLSSharedFileListGlobalLoginItems
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.