Skip to content

Instantly share code, notes, and snippets.

View nvkiet's full-sized avatar

Kiet Nguyen - CoFounder & CEO at GrabLingo.com nvkiet

View GitHub Profile
@nvkiet
nvkiet / gist:a28bbc22488a324df61d
Last active August 29, 2015 14:10
multi-context-coredata
Solution 1:
NSMangedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
temporaryContext.parentContext = mainMOC;
[temporaryContext performBlock:^{
// do something that takes some time asynchronously using the temp context
// push to parent
NSError *error;
@nvkiet
nvkiet / Books.markdown
Last active August 29, 2015 14:17
Books

Books

@nvkiet
nvkiet / SimpleEnum
Created March 31, 2015 10:05
How to make a enum conform to a protocol in Swift.
enum SimpleEnum : ExampleProtocol {
case Base, Adjusted
var simpleDescription: String {
get {
return self.getDescription()
}
}
func getDescription() -> String {
@nvkiet
nvkiet / ConvertNSDataToNSString
Last active August 29, 2015 14:19
ConvertNSDataToNSString
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ck"
ofType:@"pem"];
NSData *p12data = [NSData dataWithContentsOfFile:certificatePath];
NSString *apnsToken = [p12data base64EncodedString];
NSDictionary *parameters = @{@"apnsToken": apnsToken};
@nvkiet
nvkiet / SendFormDataByUsingAFNetworking
Created April 28, 2015 06:55
SendFormDataByUsingAFNetworking
- (void)sendEmailWithReferral:(Referral *)referral completionBlock:(NetworkCompletionBlock)completion {
[[NetworkHelper sharedInstance] updateEmailAuthorizationForRequestHeader];
NSString *appName=[[[NSBundle mainBundle] infoDictionary] objectForKey:(id)kCFBundleNameKey];
NSString *message = [NSString stringWithFormat:@"Hi %@\nI found this interesting. You can join %@ and get discount at http://quickref.io/app/%@?code=%@", referral.name, appName, kGroupID, referral.code];
[[NetworkHelper sharedInstance] POST:@"https://api.mailgun.net/v3/quickref.io/messages" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:[@"[email protected]" dataUsingEncoding:NSUTF8StringEncoding] name:@"from"];
[formData appendPartWithFormData:[referral.consentEmail dataUsingEncoding:NSUTF8StringEncoding] name:@"to"];
[formData appendPartWithFormData:[message dataUsingEncoding:NSUTF8StringEncoding] name:@"text"];
@nvkiet
nvkiet / GestureRecognizerTableView
Created May 4, 2015 14:56
GestureRecognizerTableView
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
SmoothLineView *smoothLineView = (SmoothLineView *)[self.tableFooterView viewWithTag:SMOOTH_LINE_VIEW_TAG];
CGRect smoothLineViewFrame = [smoothLineView convertRect:self.tableFooterView.bounds toView:self];
CGPoint touchedPoint = [gestureRecognizer locationInView:self];
BOOL result = !CGRectContainsPoint(smoothLineViewFrame, touchedPoint);
return result;
}
@nvkiet
nvkiet / reloadInputViews
Created May 4, 2015 17:27
reloadInputViews
It's worth noting that if you want a currently-focused field to update the keyboard type immediately, there's one extra step:
// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress
[textField setKeyboardType:UIKeyboardTypeEmailAddress];
[textField reloadInputViews];
@nvkiet
nvkiet / UITableViewCell with UITextView height in iOS 7?
Created May 8, 2015 11:25
UITableViewCell with UITextView height in iOS 7?
http://stackoverflow.com/questions/18368567/uitableviewcell-with-uitextview-height-in-ios-7
- (void)textViewDidChange:(UITextView *)textView {
if ([textView getContentHeight] > 31) {
self.noteHeightLayoutConstraint.constant = [textView getContentHeight] + 20;
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToCursorForTextView:textView];
}
@nvkiet
nvkiet / createPDFfromUIView
Created May 18, 2015 07:50
createPDFfromUIView
- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
@nvkiet
nvkiet / gist:29e4d5b5bef0865ee159
Created September 24, 2015 07:53
UIAlertController TextAlignment
if #available(iOS 8.0, *) {
let alertController = UIAlertController(title: "Terms & Conditions", message: kTermsAndConditions, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Cancel) { (action) in
alertController.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(OKAction)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Left