일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- css
- 인증
- SwiftUI
- MFA
- MSYS2
- Nodejs
- openssl
- Android
- 2FA
- Xcode
- 안드로이드
- OSX
- otpkey
- 앱리소스
- apple
- appres
- albumbook
- FIDO2
- 앨범북
- MYSQL
- OTP
- SSH
- git
- WebAuthn
- fido
- kmip
- SSL
- 애플
- SWIFT
- 앱스토어
Archives
- Today
- Total
인디노트
AuthorizationExecuteWithPrivileges 대신 SMJobBless 본문
소스 팁/Objective C, Swift, iOS, macOS
AuthorizationExecuteWithPrivileges 대신 SMJobBless
인디개발자 2023. 3. 29. 14:48MacOS 10.7 (Lion)부터 Apple은 AuthorizationExecuteWithPrivileges 함수를 더 이상 권장하지 않으며, 대신 SMJobBless 함수를 사용하도록 권장합니다. SMJobBless 함수는 권한이 필요한 작업을 수행하기 위해 Launch Services를 사용하여 승인된 helper 도구를 설치하고 실행하는 기능을 제공합니다.
아래는 SMJobBless 함수를 사용하여 승인된 helper 도구를 설치하고 실행하는 예시 코드입니다:
// import 필요
#import <ServiceManagement/ServiceManagement.h>
// ...
// helper 도구의 Bundle ID
NSString *helperBundleID = @"com.example.helper";
// helper 도구를 설치하기 위한 인증 객체 생성
NSError *error = nil;
AuthorizationItem authItem = { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
AuthorizationRights authRights = { 1, &authItem };
AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
AuthorizationRef authRef = NULL;
OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authRef);
// helper 도구를 설치하고 실행
if (status == errAuthorizationSuccess) {
CFErrorRef blessError = NULL;
if (SMJobBless(kSMDomainSystemLaunchd, (__bridge CFStringRef)helperBundleID, authRef, &blessError)) {
// helper 도구 설치 성공, 실행
status = system("launchctl start com.example.helper");
if (status != 0) {
NSLog(@"Error: Failed to start helper tool (status=%d)", status);
}
} else {
// helper 도구 설치 실패
error = (__bridge NSError *)blessError;
NSLog(@"Error: Failed to bless helper tool (%@)", error.localizedDescription);
}
}
// ...
// 인증 객체 해제
AuthorizationFree(authRef, kAuthorizationFlagDefaults);
위 코드에서는 SMJobBless 함수를 사용하여 helper 도구를 설치하고 실행합니다. SMJobBless 함수를 호출하기 전에 인증 객체를 생성해야 합니다. SMJobBless 함수가 실패하면 NSError를 반환합니다.
위 코드에서는 system 함수를 사용하여 helper 도구를 실행합니다. 이 함수는 주어진 명령을 실행하고 명령의 반환값을 반환합니다. 여기서는 launchctl 명령을 사용하여 helper 도구를 실행합니다.
위 코드에서는 helper 도구의 Bundle ID를 com.example.helper로 가정하고 있습니다. 이 값을 실제 helper 도구의 Bundle ID로 바꾸어 사용해야 합니다.
반응형
'소스 팁 > Objective C, Swift, iOS, macOS' 카테고리의 다른 글
macOS 관리자 권한 얻어오기 (0) | 2023.03.29 |
---|---|
Mac에서 Objective-C를 사용하여 시스템을 종료하는 코드 (0) | 2023.03.29 |
macOS 재시작 하는 방법 (0) | 2023.03.29 |
사설 SSL 적용 HTTPS 접속하기 위해서 (Swift) (0) | 2022.12.08 |
[강좌] 앱 자체 화면 모드 기능 구현 - macOS (0) | 2022.11.04 |
Comments