AceInfinity
Emeritus, Contributor
I've been trying out IOS app development lately, meaning, getting introduced to Objective-C over the past few days. I wrote a basic hello world as a test for UI interaction and took it a step further with incrementing a counter based on certain touch gestures. Here's an app that I've started which encrypts a string with a key...
TPViewController.h
TPViewController.m
I still have to deal with the keyboard and other things with setting proper first responders, application states, etc, but the functionality is in place, and a basic UI.
Going from C++ to Objective-C is a strange transition however regardless of how skilled you are in any other C based language I must admit...
:beerchug2:
TPViewController.h
Code:
#import <UIKit/UIKit.h>
@interface TPViewController : UIViewController
@property(nonatomic, retain) IBOutlet UIButton *btnEncrypt;
@property(nonatomic, retain) IBOutlet UIButton *btnDecrypt;
@property(nonatomic, retain) IBOutlet UITextView *textInput;
@property(nonatomic, retain) IBOutlet UITextField *textKey;
@property(nonatomic, retain) IBOutlet UITextView *textOutput;
- (IBAction)showEncryptedOutput:(id)sender;
- (IBAction)showDecryptedOutput:(id)sender;
@end
TPViewController.m
Code:
#import "TPViewController.h"
@interface TPViewController ()
NSMutableString * EncryptString(NSString *plainText, NSString *encryptKey);
NSMutableString * DecryptString(NSString *encryptedText, NSString *decryptKey);
@end
@implementation TPViewController
@synthesize btnEncrypt;
@synthesize btnDecrypt;
@synthesize textInput;
@synthesize textKey;
@synthesize textOutput;
NSMutableString * EncryptString(NSString *plainText, NSString *encryptKey)
{
int cycleIndex = 0;
NSMutableString *result = [[NSMutableString alloc] initWithCapacity:plainText.length];
for (int i = 0; i < plainText.length; ++i) {
int rotate = [encryptKey characterAtIndex:cycleIndex];
[result appendFormat:@"%c", [plainText characterAtIndex:i] + rotate];
cycleIndex = (cycleIndex + 1) % encryptKey.length;
}
return result;
}
NSMutableString * DecryptString(NSString *encryptedText, NSString *decryptKey)
{
int cycleIndex = 0;
NSMutableString *result = [[NSMutableString alloc] initWithCapacity:encryptedText.length];
for (int i = 0; i < encryptedText.length; ++i) {
int rotate = [decryptKey characterAtIndex:cycleIndex];
[result appendFormat:@"%c", [encryptedText characterAtIndex:i] - rotate];
cycleIndex = (cycleIndex + 1) % decryptKey.length;
}
return result;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showEncryptedOutput:(id)sender
{
[textKey resignFirstResponder];
NSMutableString *encryptedMutable = EncryptString(textInput.text, textKey.text);
textOutput.text = [NSString stringWithString:encryptedMutable];
}
- (IBAction)showDecryptedOutput:(id)sender
{
[textKey resignFirstResponder];
NSMutableString *decryptedMutable = DecryptString(textInput.text, textKey.text);
textOutput.text = [NSString stringWithString:decryptedMutable];
}
@end
I still have to deal with the keyboard and other things with setting proper first responders, application states, etc, but the functionality is in place, and a basic UI.
Going from C++ to Objective-C is a strange transition however regardless of how skilled you are in any other C based language I must admit...
:beerchug2: