Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing an iPhone application from scratch

Developing an iPhone application from scratch

Introduction to iOS 5, Objective-C and Xcode 4.
Source code available at https://github.com/tiagofernandez/mustachify-ios

Tiago Fernandez

January 09, 2012
Tweet

More Decks by Tiago Fernandez

Other Decks in Programming

Transcript

  1. #import "Traveler.h" @implementation Traveler @end - (NSString *)name { return

    _name; } - (void)setName:(NSString *)name { _name = name; } “dot notation” syntax is used to call getters; [[self visitedCities] setValue:date forKey:city] @interface Traveler() @end #import "Person.h" @interface Traveler : Person @end Traveler.h Traveler.m @property (nonatomic) NSString *name; - (void)visitCity:(NSString *)city atDate:(NSDate *)date; - (NSString *)name; - (void)setName:(NSString *)name; @property (nonatomic) NSMutableDictionary *visitedCities; @synthesize visitedCities = _visitedCities; - (void)visitCity:(NSString *)city atDate:(NSDate *)date { } - (NSMutableDictionary *)visitedCities { if (!_visitedCities) { _visitedCities = [[NSMutableDictionary alloc] init]; } return _visitedCities; } @synthesize name = _name; class name superclass declares getter and setter for attributes own header file private properties are declared on the implementation file the “square brackets” syntax is used to send messages [self.visitedCities setValue:date forKey:city]; creates getter and setter implementation
  2. Image Search 1. create project 2. import libraries 3. build

    the interface 4. fetch image 5. add zoom & rotation
  3. #import "ImageFetcher.h" #import "AFHTTPRequestOperation.h" #import "JSONKit.h" #import "SVProgressHUD.h" #include <stdio.h>

    @implementation ImageFetcher + (NSString *)searchURLforQuery:(NSString *)query { NSString *baseURL = @"http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=1&start=%d&q=%@"; NSString *encodedQuery = [query stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; return [NSString stringWithFormat:baseURL, random() % 10, encodedQuery]; } + (NSString *)imageURLForQuery:(NSString *)query { NSString *searchURL = [self searchURLforQuery:query]; NSString *jsonResponse = [NSString stringWithContentsOfURL:[NSURL URLWithString:searchURL] encoding:NSASCIIStringEncoding error:nil]; NSLog(@"Parsing JSON response: %@", jsonResponse); NSDictionary *json = [jsonResponse objectFromJSONString]; return [[[[json objectForKey:@"responseData"] objectForKey:@"results"] objectAtIndex:0] objectForKey:@"unescapedUrl"]; } + (void)loadImage:(NSString *)imageURL intoView:(UIImageView *)imageView { if (!imageURL) return; NSLog(@"Fetching image from URL: %@", imageURL); NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageURL]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPRequestOperation * __weak theOperation = operation; [SVProgressHUD showWithStatus:@"Please wait"]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (theOperation) { [imageView setImage:[UIImage imageWithData:theOperation.responseData]]; [imageView setNeedsDisplay]; [SVProgressHUD dismiss]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (theOperation) { [SVProgressHUD dismissWithError:@"Network error"]; } }]; [operation start]; } @end #import <Foundation/Foundation.h> @interface ImageFetcher : NSObject + (NSString *)imageURLForQuery:(NSString *)query; + (void)loadImage:(NSString *)imageURL intoView:(UIImageView *)imageView; @end
  4. #import "ImageFetcherViewController.h" @implementation ImageFetcherViewController @synthesize searchField; - (void)viewDidLoad { [super

    viewDidLoad]; [self.searchField becomeFirstResponder]; } - (void)viewDidUnload { self.searchField = nil; [super viewDidUnload]; } @end adds getter and setter implementation release retained subviews brings up the keyboard once the view gets loaded
  5. #import "ImageDisplayViewController.h" @implementation ImageDisplayViewController @synthesize imageView; @synthesize imageURL; - (void)viewDidUnload

    { self.imageView = nil; [super viewDidUnload]; } @end #import <UIKit/UIKit.h> @interface ImageDisplayViewController : UIViewController @property (nonatomic, strong) IBOutlet UIImageView *imageView; @property (nonatomic, strong) NSString *imageURL; @end
  6. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSString *query = self.searchField.text; if

    (![query isEqualToString:@""]) { NSString *imageURL = [ImageFetcher imageURLForQuery:query]; ImageDisplayViewController *destinationVC = segue.destinationViewController; destinationVC.title = query; destinationVC.imageURL = imageURL; } } calls Google Image Search, then parse an URL from the JSON response ImageFetcherViewController.m sets the URL into the display view controller
  7. #import "ImageFetcher.h" // ... - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [ImageFetcher

    loadImage:self.imageURL intoView:self.imageView]; } ImageDisplayViewController.m
  8. @implementation ImageDisplayViewController // ... - (UIView *)viewForZoomingInScrollView:(UIScrollView *)inScroll { return

    self.imageView; } // ... - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { return (orientation != UIInterfaceOrientationPortraitUpsideDown); } @end callback method for telling which subview features zooming callback method for supporting device rotation @interface ImageDisplayViewController : UIViewController <UIScrollViewDelegate> // ... @end makes this controller aware of scroll view’s events