SMJobBless gives error CFErrorDomainLaunchd Code=8

主宰稳场 提交于 2019-12-01 11:47:07
Faisal Ikwal

Verify Following Things:

1.Code

- (BOOL)blessHelperWithLabel:(NSString *)label
                       error:(NSError **)error {   

    BOOL result = NO;

    AuthorizationItem authItem      = { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
    AuthorizationRights authRights  = { 1, &authItem };
    AuthorizationFlags flags        =   kAuthorizationFlagDefaults              |
    kAuthorizationFlagInteractionAllowed    |
    kAuthorizationFlagPreAuthorize          |
    kAuthorizationFlagExtendRights;

    AuthorizationRef authRef = NULL;

    /* Obtain the right to install privileged helper tools (kSMRightBlessPrivilegedHelper). */
    OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authRef);
    if (status != errAuthorizationSuccess) {
        NSLog(@"%@", [NSString stringWithFormat:@"Failed to create AuthorizationRef. Error code: %d", (int)status]);

    } else {
        /* This does all the work of verifying the helper tool against the application
         * and vice-versa. Once verification has passed, the embedded launchd.plist
         * is extracted and placed in /Library/LaunchDaemons and then loaded. The
         * executable is placed in /Library/PrivilegedHelperTools.
         */
        result = SMJobBless(kSMDomainSystemLaunchd, (CFStringRef)label, authRef, (CFErrorRef *)error);
    }
    return result;
}

2.Tools owned after installation field in info.plist(main app)

helper bundle : identifier <Helper Bundle Identifier> and certificate leaf[subject.CN] = "Developer ID Application: xxxxx (YYXSFDHZ6W)"

3.Clients allowed to add and remove tool field in helper info.plist.

item 0 : identifier <Main App Bundle Identifier> and certificate leaf[subject.CN] = "Developer ID Application: xxxxxx (YYXSFDHZ6W)"

4.Check MachServices filed in xxxxHelperTool-Launchd.plist.it should be

helper tool bundle : YES

Find the tool SMJobBlessUtil.py in the (otherwise superseded sample code) SMJobBless sample code: https://developer.apple.com/library/archive/samplecode/SMJobBless/Introduction/Intro.html

NOTE: SMJobBless IS the correct way to bless jobs. The old way, AuthorizationExecuteWithPrivileges, is deprecated and will be actively shunned in the near future. The "SMJobBless" sample code is deprecated, because the following example code is superior in pretty much every way.

OK, get the tool, and now get the current example code: https://developer.apple.com/library/archive/samplecode/EvenBetterAuthorizationSample/Introduction/Intro.html

Use the SMJobBlessUtil.py script to verify and or set the Info.plist stuff inside your main program and helper.

1、You can use SMJobBlessUtil.py to check your app; maybe it will come to some error decribtion like dump malformed;

2、then you can compare the sample code's project setting, finding the helper target other linker flags ,see if you should set this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!