【问题标题】:Automatically start app after install in Mac OS El Capitan在 Mac OS El Capitan 中安装后自动启动应用程序
【发布时间】:2016-02-19 05:49:15
【问题描述】:

我正在开发一个 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;
}

我必须使用其他替代方法吗?有没有我也可以参考的样本?

【问题讨论】:

    标签: ios objective-c macos osx-elcapitan xcode7.1


    【解决方案1】:

    您可以使用启动代理:

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

    http://launchd.info

    我想将它作为登录项的好处是用户可能会弄​​清楚如何自己删除它。如果您使用 LaunchAgent,您可能必须在您的应用中构建“登录时启动”首选项选项。

    【讨论】:

    • 我的应用程序仅用于在菜单栏中显示,它不会出现在 Dock 上。一旦我手动启动它,它就会出现并添加到登录项中。安装我的应用后,我应该怎么做才能触发它启动?
    【解决方案2】:

    我能够使用 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];
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-23
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 2015-12-30
      相关资源
      最近更新 更多