Skip to content

Instantly share code, notes, and snippets.

View kharmabum's full-sized avatar
💭
🧀

Juan-Carlos Foust kharmabum

💭
🧀
View GitHub Profile
@kharmabum
kharmabum / mongoose-list.js
Last active August 29, 2015 13:56
Mongoose list/load static functions.
/**
* Load
*
* @param {Object} options
* @param {Function} cb
* @api private
*/
schema.statics.load = function (options, cb) {
var criteria = options.criteria || {}
@kharmabum
kharmabum / locationManager-didChangeAuthStatus-handler.m
Created February 13, 2014 02:27
LocationManager: didChangeAuthorizationStatus: - handler
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(@"%s", __PRETTY_FUNCTION__);
switch (status) {
case kCLAuthorizationStatusAuthorized:
NSLog(@"kCLAuthorizationStatusAuthorized");
// Re-enable the post button if it was disabled before.
self.navigationItem.rightBarButtonItem.enabled = YES;
[locationManager startUpdatingLocation];
break;
case kCLAuthorizationStatusDenied:
@kharmabum
kharmabum / locationServiceEnabled-check.m
Created February 13, 2014 02:28
Check whether locationServices are enabled.
if ((![CLLocationManager locationServicesEnabled])
|| ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
|| ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"Location services must be enabled in Settings.", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
@kharmabum
kharmabum / HPGroupSelectViewController.h
Created February 14, 2014 19:18
For selecting mutually exclusive categories in a dependent hierarchy (i.e. tracing a taxonomy). See -> http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
#import <UIKit/UIKit.h>
@interface HPGroupSelectViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
@kharmabum
kharmabum / UITextField-CharacterCount.m
Created February 14, 2014 19:19
Delegate methods for tracking character count
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
int remainder = MAX_CHARACTER_COUNT - textField.text.length;
self.counterLabel.text = [NSString stringWithFormat:@"%d", remainder];
if (textField == self.activityNameTextField) {
self.counterLabel.yOrigin = self.activityNameContainerView.yOrigin;
[self closePlaceTableView];
} else {
@kharmabum
kharmabum / nsstring-unicode-loop.m
Created March 9, 2014 22:32
Looping through NSString Unicode characters - http://www.objc.io/issue-9/unicode.html
NSString *s = @"The weather on \U0001F30D is \U0001F31E today.";
// The weather on 🌍 is 🌞 today.
NSRange fullRange = NSMakeRange(0, [s length]);
[s enumerateSubstringsInRange:fullRange
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop)
{
NSLog(@"%@ %@", substring, NSStringFromRange(substringRange));
}];
NSUInteger realLength = [s lengthOfBytesUsingEncoding:NSUTF32StringEncoding] / 4;
NSLog(@"The real length of %@ is %lu", s, realLength);
// => The real length of 🌍 is 1
@kharmabum
kharmabum / nsstring-unicode-comparison.m
Created March 9, 2014 22:37
String objects are not normalized unless you perform that step manually. This means that comparing strings that contain combining character sequences can potentially lead to wrong results. Both isEqual: and isEqualToString: compare strings byte for byte. If you want precomposed and decomposed variants to match, you must normalize the strings first.
// http://www.objc.io/issue-9/unicode.html
NSString *s = @"\u00E9"; // é
NSString *t = @"e\u0301"; // e + ´
BOOL isEqual = [s isEqualToString:t];
NSLog(@"%@ is %@ to %@", s, isEqual ? @"equal" : @"not equal", t);
// => é is not equal to é
// Normalizing to form C
NSString *sNorm = [s precomposedStringWithCanonicalMapping];
NSString *tNorm = [t precomposedStringWithCanonicalMapping];
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
@kharmabum
kharmabum / feedback.m
Created March 12, 2014 17:41
Get feedback from user w/ bundle version id
} else if ([choice isEqualToString:@"Feedback"]) {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"[Hopp2IT] Feedback"];
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
NSString *device = ([HPManager isiPhone5]) ? @"iPhone5" : @"<=iPhone4";
NSString *messageBody = [NSString stringWithFormat:@"\n\n~~~~~~~~~~~~~~\nPlatform: iOS\nDevice: %@\nVersion: %@\n",device,version];
[mailer setMessageBody:messageBody isHTML:NO];