Skip to content

Instantly share code, notes, and snippets.

@hlung
hlung / CocoaAsyncSocket startTLS_without_cert.m
Last active August 22, 2017 21:38
CocoaAsyncSocket (https://github.com/robbiehanson/CocoaAsyncSocket) code for where/how to run -startTLS: method. We are connecting to server with self-signed certificate and don't include the certificate (public key .pem) in the client.
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
[self secureSocket:sock];
}
// We are connecting to server with self-signed certificate and don't include the certificate
// (public key .pem) in the client. So we skip all the cert checking. To actually check, see
// http://stackoverflow.com/questions/9874932/ssl-identity-certificate-to-run-an-https-server-on-ios
- (void)secureSocket:(GCDAsyncSocket *)sock {
// It has been changed in CocoaAsyncSocket v7.4, some old option keys are now unavailable and will throw exception.
// Use GCDAsyncSocketManuallyEvaluateTrust and evaluate in -socket:didReceiveTrust: delegate instead.
@hlung
hlung / fix-xcode-sdk
Last active December 24, 2015 13:39 — forked from rnapier/fix-xcode
More verbose print and check to process only *.platform folders.
#!/usr/bin/python
# fix-xcode-sdk
# Rob Napier <robnapier@gmail.com>
# Forked by Thongchai Kolyutsakul <thongchaikol@gmail.com>
# (https://gist.github.com/hlung/6806651)
# Script to link in all your old SDKs every time you upgrade Xcode
# Create a directory called /SDKs (or modify source_path).
# Under it, put all the platform directories:
@hlung
hlung / TestFlight-upload.sh
Last active October 13, 2016 07:30
A bash script to build on top of Shenzhen (https://github.com/nomad/shenzhen). Use specified targets of an Xcode iOS project.xcworkspace file, then upload to TestFlight.
#!/bin/bash
# TestFlight-upload.sh
# https://gist.github.com/hlung/7818585
#
# By Thongchai Kolyutsakul @hlungx
# Version: v1.0.3 - 27 Oct 2014
#
# Requirement: Shenzhen (https://github.com/nomad/shenzhen)
#
@hlung
hlung / How to connect a PS3 controller.md
Last active March 25, 2024 14:45
How to connect PS3 controller on Mac OSX, PC

How to connect PS3 controller on Mac OSX, PC, etc.

This is how you connect PS3 controller to Mac OSX, PC, etc. when previously connected to a PS3. You will need a Mini USB cable. Overcome your laziness, get up of your chair, and go get one!

A big misconception is that keep holding PS button will reset the controller's pairing. It DOES NOT! From my testings, the controller keeps paring with the last machine it was CONNECTED VIA A USB CABLE.

Here are the steps:

@hlung
hlung / iOS - Checking empty NSError** param.m
Last active January 3, 2016 23:19
This is how to check if an `NSError**` or the data it points to is empty. Very straight forward, we check if pointer is nil before checking if the data it points to is nil.Doing `*error` on a nil pointer will raise an exception.
+ (void)errTest2:(NSError**)error {
// Use error object ONLY IF it is provided, otherwise it will CRASH!!!
if (error != NULL) {
NSLog(@"Creating error object...");
*error = [NSError errorWithDomain:...];
}
else {
NSLog(@"No error object provided, skip error object creation.");
}
}
@hlung
hlung / iOS - In-line block declaration.m
Last active September 8, 2015 08:15
Objective-C In-line block declaration can be hard to remember. Here are example declaration and usage.
// using block in interface property, use "copy"
@property (copy, nonatomic) void (^simpleBlock)(void);
@property (copy, nonatomic) BOOL (^blockWithParamter)(NSString *input);
// block variable declaration
void (^addButton)(SEL, NSString *) = ^(SEL action, NSString *imageName) {
};
// block typedef shorthand
typedef BOOL (^FailureHandler)(NSError *error);
@hlung
hlung / Raspberry Pi + SSH via ethernet quick setup (Mac).md
Last active July 27, 2016 14:26
A quick setup checklist from downloading an image file for Raspberry Pi until getting SSH via ethernet cable working with login password bypass.

Raspberry Pi + SSH via ethernet quick setup (Mac)

  1. download RASPBIAN from http://www.raspberrypi.org/downloads/. Download with torrent. Direct download usually fails (without any warning in Chrome! File size will be differ if download fails in the middle but there's no size to check in the download website!).
  2. And it's a good idea to check SHA1 of the zip file first. (e.g. openssl sha1 2014-01-07-wheezy-raspbian.zip)
  3. format SD card as FAT-32
  4. run df -h, find disk location. e.g. /dev/disk2s4", add r and remove s4 "/dev/rdisk2
  5. write the image file into SD card with command sudo dd bs=1m if=2014-01-07-wheezy-raspbian.img of=/dev/rdisk2 https://www.andrewmunsell.com/blog/getting-started-raspberry-pi-install-raspbian
  6. plug in HDMI before turn on RPi or else display will not work. If forgot, do NOT pull the power to restart right away. Wait for some time (about 3 min) for RPi to finish setting up its keys and stuff. Otherwise you will
@hlung
hlung / iOS - Equality.h
Last active August 29, 2015 14:02
An example for class equality method implementation
@interface FGEntityLine : NSObject
@property (readonly, nonatomic) NSUInteger companyId;
@property (readonly, nonatomic) NSUInteger brandId;
@property (readonly, nonatomic) NSUInteger propertyId;
/** Returns YES if all identifiers are equal. */
- (BOOL)isEqualToEntityLine:(FGEntityLine *)line;
@end
@hlung
hlung / iOS - comparing class objects.m
Last active August 29, 2015 14:02
Calling `-isKindOfClass` on a class object doesn't raise any warning and will give wrong comparison results. We have to use `+isSubclassOfClass:` instead.
+ (NSString*)levelName {
if ([self isSubclassOfClass:[FGRoom class]]) {
return @"Room";
}
}
@hlung
hlung / iOS - constants declaration.m
Last active November 25, 2015 07:41
iOS - how to create constants in private and public scope
// ----------------------------------
//declaration in PRIVATE scope (for using only in your class)
// ----------------------------------
// declare in .m
static const int kTweetMaxCharacters = 140;
static NSString * const kHelloString = @"HELLO!";
// ----------------------------------
// declaration in PUBLIC scope (for using in other classes too)
// ----------------------------------