인디노트

SFAuthorizationPluginView 본문

소스 팁/Objective C, Swift, iOS, macOS

SFAuthorizationPluginView

인디개발자 2023. 3. 31. 07:22
#import <Security/Security.h>
#import <SecurityInterface/SecurityInterface.h>
#import "MyAuthorizationView.h"

@interface MyAuthorizationView ()

@property (nonatomic, strong) SFAuthorization *authorization;
@property (nonatomic, strong) IBOutlet NSButton *myButton;

@end

@implementation MyAuthorizationView

- (instancetype)initWithFrame:(NSRect)frameRect {
    self = [super initWithFrame:frameRect];
    if (self) {
        [NSBundle loadNibNamed:@"MyAuthorizationView" owner:self];
        [self addSubview:self.view];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.myButton.title = @"Authorize";
}

- (IBAction)myButtonClicked:(id)sender {
    if (self.authorization == nil) {
        self.authorization = [SFAuthorization authorization];
    }
    AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights;
    AuthorizationRef authRef = [self.authorization authorizationRef];
    AuthorizationItem authItem = {kAuthorizationEnvironmentPassword, 0, NULL, 0};
    AuthorizationRights authRights = {1, &authItem};
    OSStatus status = AuthorizationCopyRights(authRef, &authRights, NULL, flags, NULL);
    if (status == errAuthorizationSuccess) {
        NSLog(@"Authorization granted!");
        // perform the action you want to do after successful authorization
    } else {
        NSLog(@"Authorization denied!");
    }
}

@end
#import <Cocoa/Cocoa.h>
#import <SecurityInterface/SecurityInterface.h>

@interface MyAuthorizationView : SFAuthorizationPluginView

@property (nonatomic, strong) NSView *view;

@end
MyAuthorizationView.nib 파일의 구성 요소

Window

Title: MyAuthorizationView
Class: SFAuthorizationPluginView
Autosave name: MyAuthorizationView
View

Class: NSView
Autosizing: All Margins
Frame: (0, 0, 240, 120)
Button

Title: Authorize
Class: NSButton
Autosizing: None
Frame: (20, 20, 200, 80)
Target: MyAuthorizationView
Action: myButtonClicked:
File's Owner

Class: MyAuthorizationView

#import <SecurityInterface/SFAuthorizationPluginView.h>

반응형
Comments