簡體   English   中英

Objective-C,授權無法執行功能

[英]Objective-C, Authorization fails to perform function

我正在嘗試創建一個授權以使用SMJobBless復制文件,盡管我無法使其正常工作。 幫助程序已成功獲得授權,並且該Job is available! 消息出現在[self copyFile]方法之前,但copyFile始終失敗。 如果有人可以闡明我做錯了什么或提供如何做這項工作的例子,那將是很棒的。

appDelegate.h

#import <Cocoa/Cocoa.h>

@interface SMJobBlessAppController : NSObject {
    IBOutlet NSTextField *_textField;
}

- (BOOL)blessHelperWithLabel:(NSString *)label error:(NSError **)error;
- (void)copyFile;

@end

appDelegate.m

#import <ServiceManagement/ServiceManagement.h>
#import <Security/Authorization.h>
#import "appDelegate.h"

@implementation SMJobBlessAppController
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    NSError *error = nil;
    if (![self blessHelperWithLabel:@"com.apple.bsd.SMJobBlessHelper" error:&error]) {
        NSLog(@"Something went wrong!");
    } else {
        /* At this point, the job is available. However, this is a very
         * simple sample, and there is no IPC infrastructure set up to
         * make it launch-on-demand. You would normally achieve this by
         * using a Sockets or MachServices dictionary in your launchd.plist.
         */
        NSLog(@"Job is available!");

        [self->_textField setHidden:false];

        [self copyFile];

    }
}

- (void)copyFile {

    NSError *error = nil;

    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *sourceFile = @"~/path/to/file.txt";
    NSString *destFile = @"~/Library/Application Support/myApp/file.txt";

    if ([fileManager copyItemAtPath:sourceFile toPath:destFile error:&error] == YES) {
        NSLog (@"[FILE] Copied.");
        // NSLog (@"Copy successful");
    } else {
        NSLog (@"[FILE] Copy failed.");
        NSLog (@" %@ %@",sourceFile, destFile);
        // NSLog (@"Copy failed");
    }

    [fileManager release];

    return;
}

- (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(@"Failed to create AuthorizationRef, return code %i", 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;
}
@end

您完全錯過了SMJobBless 它並不能神奇地使您當前的應用程序能夠執行特權操作。 相反,它安裝並運行一個單獨的幫助程序工具,該工具可以執行特權操作,但不應執行其他任何操作(盡可能少)。

您需要在移動代碼copyFilemain的功能SMJobBlessHelper.c (由於這是一個C文件,因此您必須使用C(可能是使用CoreFoundation)將其重寫為C,或者必須將工具更改為使用Objective-C。沒人說過這很容易。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM