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
NSDate *start = [NSDate date];
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:start];
NSLog(@"Execution Time: %f", executionTime);
- (void)refreshData
{
PFUser *currentUser = [PFUser currentUser];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ = '%@' OR %@ = '%@'",
kMessageUserSendIdKey, currentUser.objectId,
kMessageUserReceiveIdKey, currentUser.objectId]];
PFQuery *query = [PFQuery queryWithClassName:kMessageClassKey predicate:predicate];
[query addDescendingOrder:kMessageTimeCreatedKey];
NSString *predicateFormatString = [NSString stringWithFormat:@"(%@ = '%@' OR %@ = '%@') AND (%@ = '%@' OR %@ = '%@')",
kMessageUserSendIdKey, self.currentUser.objectId,
kMessageUserSendIdKey, self.userChat.objectId,
kMessageUserReceiveIdKey, self.currentUser.objectId,
kMessageUserReceiveIdKey, self.userChat.objectId];
@nvkiet
nvkiet / BackgroundThread
Last active August 29, 2015 14:02
BackgroundThread
//Start an activity indicator here
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Call your function or whatever work that needs to be done
//Code in this part is run on a background thread
dispatch_async(dispatch_get_main_queue(), ^(void) {
//Stop your activity indicator or anything else with the GUI
@nvkiet
nvkiet / CustomFont-iOS
Created July 6, 2014 16:56
How to use custom font in iOS
You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
You need to edit .plist file. Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.
Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
@nvkiet
nvkiet / [Swift] Init a subview
Last active August 29, 2015 14:03
Init a subview
Also for people referring to @akashivskyy answer, and having trouble or crash with error
fatal error: use of unimplemented initializer 'init(coder:)' for class
Implementing initWithCoder in Objective-C is not required but in Swift is required
So to use @akashivskyy's asnwer you need also to add these lines to your destinationViewController.
init(coder aDecoder: NSCoder!)
{
@nvkiet
nvkiet / [Xcode6beta2]NSURLAuthenticationMethodClientCertificate
Created July 7, 2014 06:02
dyld: Symbol not found: _NSURLAuthenticationMethodClientCertificate when trying to run iOS app
Re-ordering in XCode didn't do the trick; I'm using Cocoapods, which creates a Pods.xcconfig file. This has a OTHER_LDFLAGS line. I put -framework Foundation as the first entry, and that's made my project work.
OTHER_LDFLAGS = -framework Foundation -ObjC …
http://stackoverflow.com/questions/24043532/dyld-symbol-not-found-nsurlauthenticationmethodclientcertificate-when-trying#answer-24288986
@nvkiet
nvkiet / [Swift] Switch-RootViewController
Last active June 21, 2023 12:35
Switch root view controller
func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
if animated {
UIView.transitionWithView(window, duration: 0.5, options: .TransitionCrossDissolve, animations: {
let oldState: Bool = UIView.areAnimationsEnabled()
UIView.setAnimationsEnabled(false)
self.window!.rootViewController = rootViewController
UIView.setAnimationsEnabled(oldState)
}, completion: { (finished: Bool) -> () in
if completion {
completion!()
@nvkiet
nvkiet / RemoveCocoadPods
Created July 18, 2014 05:56
Removing CocoaPods from a project
Removing CocoaPods from a project is possible, but not currently automated by the CLI. First thing, if the only issue you have is not being able to use an xcworkspace you can use CocoaPods with just xcodeprojs by using the --no-integrate flag which will produce the Pods.xcodeproj but not a workspace. Then you can add this xcodeproj as a subproject to your main xcodeproj.
If you really want to remove all CocoaPods integration you need to do a few things:
NOTE editing some of these things if done incorrectly could break your main project. I strongly encourage you to check your projects into source control just in case. Also these instructions are for CocoaPods version 0.28.0, they could change with new versions.
Delete the standalone files (Podfile Podfile.lock and your Pods directory)
Delete the generated xcworkspace
Open your xcodeproj file, delete the references to Pods.xcconfig and libPods.a (in the Frameworks group)
Under your Build Phases delete the Copy Pods Resources and Check Pods Ma
@nvkiet
nvkiet / StrechImage
Created July 18, 2014 17:11
UIImage stretchableImageWithLeftCapWidth
- (UIImage *) resizableImageWithSize:(CGSize)size
{
if( [self respondsToSelector:@selector(resizableImageWithCapInsets:)] )
{
return [self resizableImageWithCapInsets:UIEdgeInsetsMake(size.height, size.width, size.height, size.width)];
} else {
return [self stretchableImageWithLeftCapWidth:size.width topCapHeight:size.height];
}
}