Skip to content

Instantly share code, notes, and snippets.

View sag333ar's full-sized avatar

sagar kothari sag333ar

View GitHub Profile
@sag333ar
sag333ar / twoDaysAgo.m
Created December 3, 2013 12:50
* 'just now' * '2 minutes ago' * '24 days ago' * 'a month ago' Manipulate date & get data in string format as above.
+ (NSString *)stringForTimeIntervalSinceCreated:(NSDate *)dateTime serverTime:(NSDate *)serverDateTime{
NSInteger MinInterval;
NSInteger HourInterval;
NSInteger DayInterval;
NSInteger DayModules;
NSInteger interval = abs((NSInteger)[dateTime timeIntervalSinceDate:serverDateTime]);
if(interval >= 86400) {
DayInterval = interval/86400;
DayModules = interval%86400;
@sag333ar
sag333ar / validateAnEmail.m
Created December 3, 2013 12:43
Validate an email ID with NSPredicate
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
@sag333ar
sag333ar / dataToBase64EncodedString.m
Created December 3, 2013 12:39
Convert NSData to Base64 Encoded String.
- (NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
@sag333ar
sag333ar / scaleImage.m
Created December 3, 2013 12:34
Scale uiimage to specified resolution.
- (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
CGImageRef imgRef = [image CGImage];
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
//if already at the minimum resolution, return the orginal image, otherwise scale
if (width <= resolution && height <= resolution) {
return image;
@sag333ar
sag333ar / setoperations.m
Last active December 29, 2015 17:09
Set operations in Objective-C
// SET A - SET B ------------------------
// an array that contains many objects
NSMutableArray *arForAll = [NSMutableArray arrayWithObjects:@"1",@"2",@"Nimit",@"sagar",@"3", nil];
// an array which is sort of subset of above array.
NSMutableArray *selForSelected = [NSMutableArray arrayWithObjects:@"Nimit",@"sagar", nil];
// create a set with array - major set.
NSMutableSet* set1 = [NSMutableSet setWithArray:arForAll];
@sag333ar
sag333ar / STEntitiesConverter.m
Last active December 29, 2015 15:29
Decode or strip HTML tags from a string
// sample data is as follows
//// <h1>some data</h1> <h2>some other data</h2>
// and if you wish to extract data as follows
//// some data some other data
// use following class for decoding HTML string to normal text
@interface STEntitiesConverter : NSObject <NSXMLParserDelegate>
@property (nonatomic, retain) NSMutableString* resultString;
- (NSString*)convertEntiesInString:(NSString*)s;
@sag333ar
sag333ar / STCSVParser.m
Created November 27, 2013 10:40
An Objective-C CSV Parser which works with ' (single-quote) or/and " (double-quote) on iOS 7 + iOS 6 + iOS 5 + iOS 4
- (NSMutableArray *)loadAndParseWithStringData:(NSString*)stringData hasHeaderFields:(BOOL)hasHeaderFields{
NSArray *gcRawData = [stringData componentsSeparatedByString:@"\n"];
NSArray *singleGC = [NSArray array];
NSMutableArray *allGC = [NSMutableArray array];
for (int i = 0; i < gcRawData.count; i++)
{
NSString *nextGCString = [NSString stringWithFormat:@"%@", gcRawData[i]];
singleGC = [nextGCString componentsSeparatedByString:@","];
NSMutableArray *arrayOfComponents = [NSMutableArray array];
@sag333ar
sag333ar / STTimeline_twitter_api.h
Created November 27, 2013 10:36
STTimeline_twitter_api.h: Header-file for fetching tweets from twitter which works for iOS 7 + iOS 6 + iOS 5
//
// timeline_twitter_api.h
// Twitter demo
//
// Created by SagarRK on 09/10/13.
// Copyright (c) 2013 http://sugartin.info . All rights reserved.
//
#import <Foundation/Foundation.h>
@sag333ar
sag333ar / STTimeline_twitter_api.m
Created November 27, 2013 10:37
STTimeline_twitter_api.m: Header-file for fetching tweets from twitter which works for iOS 7 + iOS 6 + iOS 5
//
// timeline_twitter_api.m
// Twitter demo
//
// Created by SagarRK on 09/10/13.
// Copyright (c) 2013 http://sugartin.info . All rights reserved.
//
#import "timeline_twitter_api.h"
@sag333ar
sag333ar / zoomOutToWorldView.m
Last active December 29, 2015 08:09
Sometimes, in maps, you might need to display entire-world-view. Example, in case of no location found, zoom out to the world view. Following code-snip would help you to achieve the same.
// this code will help you to zoom out to world-view
// step.1 create co-ordianate-span as follows
MKCoordinateSpan span = {.latitudeDelta = 180, .longitudeDelta = 360};
// step.2 crate region as follows
MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(0.0000f, 0.0000f), span);
// step.3 zoom using region to map as follows
[self.mapView setRegion:region animated:YES];