// 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 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");
//In order for your UI to display, you must add it to the view hierarchy [self.window addSubview:label];
//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);
}
- (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
[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 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];
- (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);
}
// 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];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(40.80827, -73.96098); MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; annotation.coordinate = coord; annotation.title = @"User Touch"; [map addAnnotation:annotation];
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!");
}
-(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; }
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];
(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 (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));
}
}
}
// 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);
UIImage *img = [UIImage imageNamed:@"Icon.png"]; [img drawInRect:CGRectMake(100, 100, 100, 100)];
// 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];
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);
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]);
}
-(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]);
}
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];
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];
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)

