Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / iostorch.m
Created October 2, 2012 13:05
Turn LED on/off on iOS
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn]; // use AVCaptureTorchModeOff to turn off
[device unlockForConfiguration];
}
@kristopherjohnson
kristopherjohnson / keyboardShowHide.m
Created October 8, 2012 02:03
View-controller methods for resizing view when keyboard is shown/hidden (from Apple's UICatalog sample)
- (void)keyboardWillShow:(NSNotification *)aNotification
{
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
@kristopherjohnson
kristopherjohnson / ShouldChangeCharactersInRange.m
Created October 8, 2012 02:21
Determine whether proposed change to UITextField will exceed maximum text length
-(BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string textField:(UITextField *)textField maxLength:(NSUInteger)maxTextFieldLength
{
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
return newLength <= maxTextFieldLength;
}
@kristopherjohnson
kristopherjohnson / dimScreenAndShowActivityIndicator.m
Created October 21, 2012 15:52
Display activity indicator over dimmed screen
- (IBAction)dimScreenAndShowActivityIndicator:(id)sender {
CGRect bounds = self.view.bounds;
// Create black view covering entire view
UIView *maskView = [[UIView alloc] initWithFrame:bounds];
maskView.backgroundColor = [UIColor blackColor];
[self.view addSubview:maskView];
// Put spinning activity indicator in middle of view
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
@kristopherjohnson
kristopherjohnson / testParseJSON.m
Created October 24, 2012 15:15
OCUnit Test for Reading JSON from a Resource File
#import <SenTestingKit/SenTestingKit.h>
@interface JSONTests : SenTestCase
@end
@implementation JSONTests
- (void)testParseJSON {
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
@kristopherjohnson
kristopherjohnson / readJSONResourceNamed.m
Created October 25, 2012 11:36
Read JSON resource into an NSDictionary
- (NSDictionary *)readJSONResourceNamed:(NSString *)resourceName {
NSString *filePath = [[NSBundle mainBundle] pathForResource:resourceName
ofType:@"json"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
if (!fileData)
return nil;
NSError *error = nil;
@kristopherjohnson
kristopherjohnson / streamToString.java
Created October 26, 2012 22:30
Read string from InputStream
// http://stackoverflow.com/a/9949592/1175
String readStringFromStream(InputStream in) throws IOException {
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
for(String line = br.readLine(); line != null; line = br.readLine())
out.append(line);
br.close();
return out.toString();
}
@kristopherjohnson
kristopherjohnson / DeviceInfo.java
Created November 3, 2012 21:40
Get unique identifier for an Android device
public class DeviceInfo {
private static final String PREFERENCES = "DeviceInfo.PREFERENCES";
private static final String KEY_DEVICEID = "DeviceId";
/**
* @return unique identifier string for the device
*/
public static synchronized String getDeviceId(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, 0);
@kristopherjohnson
kristopherjohnson / AppInfo.java
Created November 3, 2012 22:05
Get Android app version name and version code
public class AppInfo {
/**
* Get application version string
*
* @param context
* Interface to global information about an application
* environment
* @return String of the form "VV-CC", where VV is the version name and CC
* is the version code (e.g., "1.0.3-25")
@kristopherjohnson
kristopherjohnson / cheatsheet.js
Last active March 1, 2022 19:46
Random Javascript snippets/examples
// Get a reference to the global object (`window` in the browser, `global` on the server).
var root = (function () {
return this || (0 || eval)('this');
}());
var elemWithId = document.getElementById('anId');
elemWithId.innerHTML = markup;
var elemsWithName = document.getElementsByName('aName');
var firstElem = elems[0];