Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Nov 19, 2013
1 parent 1d2aa10 commit 6d5e276
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
6 changes: 6 additions & 0 deletions OnionKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
D9C5D922183C16380012262D /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D9C5D921183C16380012262D /* SystemConfiguration.framework */; };
D9C5D924183C16410012262D /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = D9C5D923183C16410012262D /* libz.dylib */; };
D9C5D926183C16A10012262D /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D9C5D925183C16A10012262D /* MobileCoreServices.framework */; };
D9C5DA02183C1ABF0012262D /* TORRootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D9C5DA01183C1ABF0012262D /* TORRootViewController.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -518,6 +519,8 @@
D9C5D9FC183C17480012262D /* tor-resolve.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = "tor-resolve.c"; sourceTree = "<group>"; };
D9C5D9FE183C17480012262D /* include.am */ = {isa = PBXFileReference; lastKnownFileType = text; path = include.am; sourceTree = "<group>"; };
D9C5D9FF183C17480012262D /* orconfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = orconfig.h; sourceTree = "<group>"; };
D9C5DA00183C1ABF0012262D /* TORRootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TORRootViewController.h; sourceTree = "<group>"; };
D9C5DA01183C1ABF0012262D /* TORRootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TORRootViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -649,6 +652,8 @@
children = (
D9C5D826183C14980012262D /* TORAppDelegate.h */,
D9C5D827183C14980012262D /* TORAppDelegate.m */,
D9C5DA00183C1ABF0012262D /* TORRootViewController.h */,
D9C5DA01183C1ABF0012262D /* TORRootViewController.m */,
D9C5D829183C14980012262D /* Images.xcassets */,
D9C5D81E183C14980012262D /* Supporting Files */,
);
Expand Down Expand Up @@ -1325,6 +1330,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D9C5DA02183C1ABF0012262D /* TORRootViewController.m in Sources */,
D9C5D828183C14980012262D /* TORAppDelegate.m in Sources */,
D9C5D824183C14980012262D /* main.m in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions OnionKitTest/TORAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "TORAppDelegate.h"
#import "HITorManager.h"
#import "TORRootViewController.h"

@implementation TORAppDelegate

Expand All @@ -16,7 +16,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[[HITorManager defaultManager] start];
self.window.rootViewController = [[TORRootViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
Expand Down
17 changes: 17 additions & 0 deletions OnionKitTest/TORRootViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TORRootViewController.h
// OnionKit
//
// Created by Christopher Ballinger on 11/19/13.
// Copyright (c) 2013 ChatSecure. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TORRootViewController : UIViewController

@property (nonatomic, strong) UILabel *connectionStatusLabel;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, strong) UIButton *connectButton;

@end
116 changes: 116 additions & 0 deletions OnionKitTest/TORRootViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// TORRootViewController.m
// OnionKit
//
// Created by Christopher Ballinger on 11/19/13.
// Copyright (c) 2013 ChatSecure. All rights reserved.
//

#import "TORRootViewController.h"
#import "HITorManager.h"


NSString * const kHITorManagerIsRunningKey = @"isRunning";
NSString * const CONNECTING_STRING = @"Connecting to Tor...";
NSString * const DISCONNECTING_STRING = @"Disconnecting from Tor...";
NSString * const DISCONNECTED_STRING = @"Disconnected from Tor";
NSString * const CONNECTED_STRING = @"Connected to Tor!";
NSString * const CONNECT_STRING = @"Connect";
NSString * const DISCONNECT_STRING = @"Disconnect";
NSString * const CANNOT_RECONNECT_STRING = @"Cannot Reconnect";


@interface TORRootViewController ()

@end

@implementation TORRootViewController
@synthesize connectionStatusLabel, activityIndicatorView, connectButton;

- (void) dealloc {
[[HITorManager defaultManager] removeObserver:self forKeyPath:kHITorManagerIsRunningKey];
}

- (id)init
{
self = [super init];
if (self) {
self.connectionStatusLabel = [[UILabel alloc] init];
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.connectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.connectButton addTarget:self action:@selector(connectButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[[HITorManager defaultManager] addObserver:self forKeyPath:kHITorManagerIsRunningKey options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:connectionStatusLabel];
[self.view addSubview:activityIndicatorView];
[self.view addSubview:connectButton];
}

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGFloat padding = 20.0f;
self.connectionStatusLabel.frame = CGRectMake(padding, padding, 200, 30);
self.connectionStatusLabel.text = DISCONNECTED_STRING;
self.connectionStatusLabel.textColor = [UIColor redColor];
[self.connectButton setTitle:CONNECT_STRING forState:UIControlStateNormal];
self.activityIndicatorView.frame = CGRectMake(connectionStatusLabel.frame.origin.x + connectionStatusLabel.frame.size.width + padding, padding, 30, 30);
self.connectButton.frame = CGRectMake(padding, connectionStatusLabel.frame.origin.y + connectionStatusLabel.frame.size.height + padding, 150, 50);
}

- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

}

- (void) connectButtonPressed:(id)sender {
[self.activityIndicatorView startAnimating];
if (!self.connectButton.enabled) {
// do nothing if already connecting
return;
}
self.connectButton.enabled = NO;
self.connectionStatusLabel.textColor = [UIColor orangeColor];
if (![HITorManager defaultManager].isRunning) {
self.connectionStatusLabel.text = CONNECTING_STRING;
[[HITorManager defaultManager] start];
} else {
self.connectionStatusLabel.text = DISCONNECTING_STRING;
[[HITorManager defaultManager] stop];
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqual:kHITorManagerIsRunningKey]) {
BOOL isRunning = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
if (isRunning) {
self.connectionStatusLabel.text = CONNECTED_STRING;
self.connectionStatusLabel.textColor = [UIColor greenColor];
[self.connectButton setTitle:DISCONNECT_STRING forState:UIControlStateNormal];
self.connectButton.enabled = YES;
} else {
self.connectionStatusLabel.text = DISCONNECTED_STRING;
self.connectionStatusLabel.textColor = [UIColor redColor];
[self.connectButton setTitle:CANNOT_RECONNECT_STRING forState:UIControlStateNormal];
self.connectButton.enabled = NO; // Tor crashes if you disconnect and reconnect, so only allow connecting once.
}
[self.activityIndicatorView stopAnimating];
}
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

0 comments on commit 6d5e276

Please sign in to comment.