iOS Code Snippets

Created for Appitecture Visual Studies Session B
Graduate School of Architecture, Planning Preservation
Columbia University


Creating Objects

// POINTERS, OBJECTS AND C STRUCTURES
// Use "*" when declaring objects (including your own). This designates a "pointer".

UILabel *label;
label = [[UILabel alloc] init];

/* All objects are "allocated" and "initialized". 
Allocation sets up a block of memory for the object.
Initializing sets up variables. If you don't see a "*", 
you are probably seeing a c struct (structure).
Structures are different from classes in that they only 
store data (no behaviors). */

CGRect rect = CGRectMake(0,0,300,200);
// Common structures in iOS are CGPoint, CGRect, CGSize.

Dot Syntax

//DOT VS BRACKET SYNTAX

UILabel *label = [[UILabel alloc] init];
[label setText:@"Hello World"];
//is functionally equivalent to...
label.text = @"Hello World";
//Note that a string can be created by prefixing "@" to a string in quotes, for instance:
NSlog(@"Made Label");

Adding to View Hierarchy

//In order for your UI to display, you must add it to the view hierarchy
[self.window addSubview:label];

Accessing Accelerometer

//Defining our object as a delegate in the ".h" file:
@interface AppDelegate_iPhone : NSObject <UIApplicationDelegate, UIAccelerometerDelegate> {
    UIWindow *window;
}

//Accessing the UIAccelerometer Singleton in the ".m" files
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
	
//To conform to the delegate protocol, we add a method in our ".m" file.
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
	NSLog(@"%f, %f, %f", acceleration.x, acceleration.y, acceleration.z);
} 

Touch Delegation

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

Animate View Properties

[UIView beginAnimations:@"id" context:nil];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	
[view setAlpha:0.4];
[view setCenter:CGPointMake(200, 400)];
	
[UIView commitAnimations];

Getting the GPS Location

// Getting our Location
// See delegate method "locationManager:(CLLocationManager *)manager didUpdateToLocation:" for response
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; // set ourselves as the delegate
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

Responding to a Location Event

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    float lat = newLocation.coordinate.latitude;
    float lon = newLocation.coordinate.longitude;
    NSLog(@"latitude %.5f, longitude %.5f\n",lat,lon);
}

Adding a Map

// Creating a Map with a custom center and span 
MKMapView *map = [[MKMapView alloc] initWithFrame:self.view.frame];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(40.80827, -73.96098);
MKCoordinateSpan span = MKCoordinateSpanMake(.01,.01);
[map setRegion:MKCoordinateRegionMake(coord, span)];
[self.view addSubview:map];

Adding a Pin Annotation

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(40.80827, -73.96098);
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coord;
annotation.title = @"User Touch";
[map addAnnotation:annotation];

Adding a Gesture Recognizer to a View

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleGesture:)];   
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 1;
[map addGestureRecognizer:tap];
[tap release];

// This assumes you have implemented:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer{
   NSLog(@"Tapped!");
}

Using an Image as an Annotation

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
    
    MKAnnotationView* aView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];
    if(!aView){ 
        MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] autorelease];
        aView.image = [UIImage imageNamed:@"GradientDot_128.png"];
    }
    return aView;
}

Creating Colors

UIColor *blueColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
UIColor *whiteColor = [UIColor colorWithWhite:1.0 alpha:1.0];
UIColor *randomColor = [UIColor colorWithHue:(random()%100)/100.0 saturation:1.0 brightness:1.0 alpha:1.0];
	

Simple DrawRect:

 (void)drawRect:(CGRect)rect {
    
	// Create a CGContext and clear it
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextClearRect(context, [[UIScreen mainScreen] bounds]);

	// draw a rectangle
	[[UIColor lightGrayColor] setFill];
	CGContextFillRect(context, CGRectMake(20, 40, 280, 420));
}

Draw a matrix of rectangles

// Draw a matrix of rectangles (half not drawn)
[[UIColor blackColor] setStroke];
[[UIColor whiteColor] setFill];
srandom(100);
for(int i=0; i<rect.size.width; i+=20){
  for(int j=0; j<rect.size.height; j+=20){
    if( random()%100 > 50){
      CGContextStrokeRect(context, CGRectMake(i,j,20,20));
      CGContextFillRect(context, CGRectMake(i,j,20,20));
    }
  }
}

Draw a Shape

// Make a Path
CGPoint origin = self.center;
CGContextBeginPath(context);
float radius = 100;
float segs = 6.0;
for (int i=0; i<segs; i++) {
	float x = sinf(i/segs * 6.28) * radius + origin.x;
	float y = cosf(i/segs * 6.28) * radius + origin.y;
	if(i==0){
		CGContextMoveToPoint(context, x, y);
	} else {		
		CGContextAddLineToPoint(context, x, y);
	}
}
//CGContextClosePath(context);
[[UIColor greenColor] setFill];
[[UIColor whiteColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);

Draw an Image

UIImage *img = [UIImage imageNamed:@"Icon.png"];
[img drawInRect:CGRectMake(100, 100, 100, 100)];

Draw Text in a View

// Put a string to the window
NSString  *str = [NSString stringWithString:@"Hello World"]; 
UIFont *font = [UIFont systemFontOfSize:32.0];
[[UIColor blackColor] setFill];
[str drawAtPoint:CGPointMake(40.0, 60.0) withFont:font];

Generating Random Numbers

srandom(time(NULL)); //set a unique seed
srandom(200); // set a specific seed
int r = random()%100;
float f = (random()%100) / 100.0f;
NSLog(@"Random int: %i  Random float: %.4f", r, f);

Parsing the Contents of a Text File

NSString *file = [[NSBundle mainBundle] pathForResource:@"Schools" ofType:@"txt" ];
NSLog(@"File is at %@",file);
NSString *data = [NSString stringWithContentsOfFile:file usedEncoding:nil error:nil];
NSLog(@"Data is %i characters", [data length]);
NSArray *arr = [data componentsSeparatedByString:@"\n"];
NSLog(@"Array has %i elements", [arr count]);
for (NSString *line in arr){
    NSArray *components = [line componentsSeparatedByString:@","];
    NSLog(@"%f",[[components objectAtIndex:0] floatValue]);
}

Useful NSXMLParser Delegate Methods

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    cachedElementName = [NSString stringWithFormat:@"%@", elementName];
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"%@: %@",cachedElementName,string);
}

-(void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"Done with %@", [parser description]);
}

Starting an XML Parser

NSString *url_string = @"https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=GSAPPOnline&count=3";
NSURL *url = [NSURL URLWithString:url_string];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
parser.delegate = self;
[parser parse];

Building a Geocoded Twitter Search

NSString *query = @"http://search.twitter.com/search.rss?q=";
NSString *search_term = @"GSAPP";
float lat = 40.80830;
float lon = -73.96097;
int accuracy =  2;
NSString *geourl_string =  [NSString stringWithFormat:@"%@%@&geocode=%f,%f,%imi", query,search_term,lat,lon,accuracy];

Simple Twitter API Searches

Twitter Timeline Search
https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
Returns the 20 most recent statuses posted by the authenticating user.

https://api.twitter.com/1/statuses/user_timeline.json?screen_name=GSAPPOnline&count=3
https://api.twitter.com/1/statuses/user_timeline.json?screen_name=ProxyMark&count=3

Twitter Get Search 
https://dev.twitter.com/docs/api/1/get/search
Returns tweets that match a specified query

http://search.twitter.com/search.rss?q=GSAPP
http://search.twitter.com/search.rss?q=%40GSAPPonline  //@ (http://en.wikipedia.org/wiki/URL_encoding)
http://search.twitter.com/search.rss?q=%23GSAPP   //# (http://en.wikipedia.org/wiki/URL_encoding)

Contributors

@proxyToru Toru Hasegawa is co-director of Proxy. Toru investigates the culture of innovation and technology in architecture as an adjunct assistant professor at the Columbia University Graduate School of Architecture Toru is a co-director of the Cloud Lab.

@ProxyMark Mark Collins is co-director of Proxy, an innovation-focused design firm working across a range of scales and platforms. Mark is as an adjunct assistant professor at the Columbia University Graduate School of Architecture where he co-directs the Cloud Lab.