-
-
Save stevenschmatz/7efb18ea3444e37d1645 to your computer and use it in GitHub Desktop.
Running local binaries with NSTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@interface GBAppDelegate : NSObject <NSApplicationDelegate> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GBAppDelegate.h" | |
#import "GBViewController.h" | |
@interface GBAppDelegate () | |
@property (assign) IBOutlet NSWindow *window; | |
@property (nonatomic, strong) IBOutlet GBViewController *viewController; | |
@end | |
@implementation GBAppDelegate | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { | |
self.viewController = [[GBViewController alloc] initWithNibName:@"GBViewController" bundle:nil]; | |
[self.window.contentView addSubview:self.viewController.view]; | |
self.viewController.view.frame = ((NSView *)self.window.contentView).bounds; | |
} | |
- (void)applicationWillTerminate:(NSNotification *)aNotification { | |
// Insert code here to tear down your application | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@interface GBViewController : NSViewController | |
@property (strong) NSString *username; | |
@property (strong) NSString *password; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GBViewController.h" | |
@interface GBViewController () | |
// User interface fields | |
@property (weak) IBOutlet NSTextField *usernameField; | |
@property (weak) IBOutlet NSSecureTextField *passwordField; | |
@property (weak) IBOutlet NSButton *loginButton; | |
@property (weak) IBOutlet NSProgressIndicator *loginIndicator; | |
@property (weak) IBOutlet NSTextField *errorView; | |
@property (nonatomic, copy) NSString *pathString; | |
- (IBAction)loginInitiated:(id)sender; | |
- (BOOL)verifyAccountWithUsername:(NSString *)username AndPassword:(NSString *)password; | |
@end | |
@implementation GBViewController | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { | |
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { | |
NSFileManager *filemgr = [[NSFileManager alloc] init]; | |
self.pathString = [[NSString alloc] initWithString:filemgr.currentDirectoryPath]; | |
NSLog(@"The working directory is %@", self.pathString); | |
self.username = @""; | |
self.password = @""; | |
[self.loginIndicator setHidden:YES]; | |
} | |
return self; | |
} | |
- (IBAction)loginInitiated:(id)sender { | |
NSString *username = self.usernameField.stringValue; | |
NSString *password = self.passwordField.stringValue; | |
self.username = username; | |
self.password = password; | |
BOOL verified = [self verifyAccountWithUsername:username AndPassword:password]; | |
NSLog(@"%d", verified); | |
} | |
- (BOOL)verifyAccountWithUsername:(NSString *)username AndPassword:(NSString *)password { | |
[self.loginIndicator setHidden: NO]; | |
NSFileManager *filemgr = [[NSFileManager alloc] init]; | |
NSString *currentPath = filemgr.currentDirectoryPath; | |
NSString *launchPath = [currentPath stringByAppendingString:@"/X.app/Contents/Resources/loginFromClient"]; | |
NSTask *task; | |
task = [[NSTask alloc] init]; | |
[task setLaunchPath: launchPath]; | |
NSArray *arguments; | |
arguments = [NSArray arrayWithObjects:username, password, nil]; | |
[task setArguments: arguments]; | |
NSPipe *pipe; | |
pipe = [NSPipe pipe]; | |
[task setStandardOutput: pipe]; | |
NSFileHandle *file; | |
file = [pipe fileHandleForReading]; | |
[task launch]; | |
NSData *data; | |
data = [file readDataToEndOfFile]; | |
NSString *string; | |
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; | |
NSLog(@"Request with username %@ and password %@ was %@", username, password, string); | |
[self.loginIndicator setHidden:YES]; | |
return FALSE; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment