diff --git a/TalkinToTheNet/Podfile b/TalkinToTheNet/Podfile new file mode 100644 index 0000000..86a5040 --- /dev/null +++ b/TalkinToTheNet/Podfile @@ -0,0 +1,2 @@ +platform :ios, '8.4' +pod 'FourSquareKit' \ No newline at end of file diff --git a/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj b/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj index ee35a70..b864854 100644 --- a/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj +++ b/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 69E41EF71BB0F5F600D7324C /* APIManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 69E41EF61BB0F5F600D7324C /* APIManager.m */; settings = {ASSET_TAGS = (); }; }; + 69E420171BB10B3D00D7324C /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 69E420161BB10B3D00D7324C /* CoreLocation.framework */; }; + 69E4201A1BB1BC1F00D7324C /* C4QRestaurant.m in Sources */ = {isa = PBXBuildFile; fileRef = 69E420191BB1BC1F00D7324C /* C4QRestaurant.m */; settings = {ASSET_TAGS = (); }; }; 8D7DCD4B1BAF859400A92AD2 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D7DCD4A1BAF859400A92AD2 /* main.m */; }; 8D7DCD4E1BAF859400A92AD2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D7DCD4D1BAF859400A92AD2 /* AppDelegate.m */; }; 8D7DCD511BAF859400A92AD2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D7DCD501BAF859400A92AD2 /* ViewController.m */; }; @@ -16,6 +19,11 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 69E41EF51BB0F5F600D7324C /* APIManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APIManager.h; sourceTree = ""; }; + 69E41EF61BB0F5F600D7324C /* APIManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APIManager.m; sourceTree = ""; }; + 69E420161BB10B3D00D7324C /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; }; + 69E420181BB1BC1F00D7324C /* C4QRestaurant.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = C4QRestaurant.h; sourceTree = ""; }; + 69E420191BB1BC1F00D7324C /* C4QRestaurant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = C4QRestaurant.m; sourceTree = ""; }; 8D7DCD461BAF859400A92AD2 /* TalkinToTheNet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TalkinToTheNet.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8D7DCD4A1BAF859400A92AD2 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 8D7DCD4C1BAF859400A92AD2 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -33,6 +41,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 69E420171BB10B3D00D7324C /* CoreLocation.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -42,6 +51,7 @@ 8D7DCD3D1BAF859400A92AD2 = { isa = PBXGroup; children = ( + 69E420161BB10B3D00D7324C /* CoreLocation.framework */, 8D7DCD481BAF859400A92AD2 /* TalkinToTheNet */, 8D7DCD471BAF859400A92AD2 /* Products */, ); @@ -60,6 +70,10 @@ children = ( 8D7DCD4C1BAF859400A92AD2 /* AppDelegate.h */, 8D7DCD4D1BAF859400A92AD2 /* AppDelegate.m */, + 69E420181BB1BC1F00D7324C /* C4QRestaurant.h */, + 69E420191BB1BC1F00D7324C /* C4QRestaurant.m */, + 69E41EF51BB0F5F600D7324C /* APIManager.h */, + 69E41EF61BB0F5F600D7324C /* APIManager.m */, 8D7DCD4F1BAF859400A92AD2 /* ViewController.h */, 8D7DCD501BAF859400A92AD2 /* ViewController.m */, 8D7DCD521BAF859400A92AD2 /* Main.storyboard */, @@ -149,6 +163,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 69E41EF71BB0F5F600D7324C /* APIManager.m in Sources */, + 69E4201A1BB1BC1F00D7324C /* C4QRestaurant.m in Sources */, 8D7DCD511BAF859400A92AD2 /* ViewController.m in Sources */, 8D7DCD4E1BAF859400A92AD2 /* AppDelegate.m in Sources */, 8D7DCD4B1BAF859400A92AD2 /* main.m in Sources */, @@ -300,6 +316,7 @@ 8D7DCD5F1BAF859400A92AD2 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/TalkinToTheNet/TalkinToTheNet/APIManager.h b/TalkinToTheNet/TalkinToTheNet/APIManager.h new file mode 100644 index 0000000..c4be67f --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/APIManager.h @@ -0,0 +1,16 @@ +// +// APIManager.h +// LearnAPI +// +// Created by Z on 9/20/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface APIManager : NSObject + ++ (void)GETRequestWithURL:(NSURL *)URL + completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler; + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/APIManager.m b/TalkinToTheNet/TalkinToTheNet/APIManager.m new file mode 100644 index 0000000..41672a7 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/APIManager.m @@ -0,0 +1,31 @@ +// +// APIManager.m +// LearnAPI +// +// Created by Z on 9/20/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "APIManager.h" + +@implementation APIManager + +//class methods +//no instance of an object +//no state ++ (void)GETRequestWithURL:(NSURL *)URL + completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler { + + NSURLSession *session = [NSURLSession sharedSession]; + + NSURLSessionDataTask *task = [session dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { + + dispatch_async(dispatch_get_main_queue(), ^{ + completionHandler(data, response, error); + }); + }]; + + [task resume]; +} + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard b/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard index f56d2f3..82ba250 100644 --- a/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard +++ b/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard @@ -1,13 +1,15 @@ - + - + + + - + @@ -15,8 +17,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TalkinToTheNet/TalkinToTheNet/C4QRestaurant.h b/TalkinToTheNet/TalkinToTheNet/C4QRestaurant.h new file mode 100644 index 0000000..029425a --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/C4QRestaurant.h @@ -0,0 +1,25 @@ +// +// C4QRestaurant.h +// TalkinToTheNet +// +// Created by Z on 9/22/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface C4QRestaurant : NSObject + +@property NSString *name; +@property NSString *phoneNumber; + +@property NSString *address; +@property NSString *buildingNumber; +@property NSString *street; +@property NSString *zipcode; + +@property NSInteger *ratCount; +@property NSInteger *roachCount; +@property NSInteger *flyCount; + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/C4QRestaurant.m b/TalkinToTheNet/TalkinToTheNet/C4QRestaurant.m new file mode 100644 index 0000000..8a8645c --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/C4QRestaurant.m @@ -0,0 +1,13 @@ +// +// C4QRestaurant.m +// TalkinToTheNet +// +// Created by Z on 9/22/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "C4QRestaurant.h" + +@implementation C4QRestaurant + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/Info.plist b/TalkinToTheNet/TalkinToTheNet/Info.plist index 6905cc6..dc007cb 100644 --- a/TalkinToTheNet/TalkinToTheNet/Info.plist +++ b/TalkinToTheNet/TalkinToTheNet/Info.plist @@ -2,6 +2,8 @@ + NSLocationWhenInUseUsageDescription + Location is required to find out where you are CFBundleDevelopmentRegion en CFBundleExecutable diff --git a/TalkinToTheNet/TalkinToTheNet/ViewController.m b/TalkinToTheNet/TalkinToTheNet/ViewController.m index cbefa29..82ecb5f 100644 --- a/TalkinToTheNet/TalkinToTheNet/ViewController.m +++ b/TalkinToTheNet/TalkinToTheNet/ViewController.m @@ -7,8 +7,26 @@ // #import "ViewController.h" +#import "APIManager.h" +#import +#import "C4QRestaurant.h" -@interface ViewController () +@interface ViewController () < +CLLocationManagerDelegate, +UITextFieldDelegate, +UITableViewDataSource, +UITableViewDelegate +> + +@property CLLocationManager *locationManager; +@property CLLocation *currentLocation; + +@property (weak, nonatomic) IBOutlet UILabel *latLabel; +@property (weak, nonatomic) IBOutlet UILabel *longLabel; +@property (weak, nonatomic) IBOutlet UITextField *searchTextField; +@property (weak, nonatomic) IBOutlet UITableView *tableView; + +@property (nonatomic) NSMutableArray *restaurants; @end @@ -16,12 +34,131 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + [self setupLocationManager]; + + self.searchTextField.delegate = self; + self.tableView.delegate = self; + self.tableView.dataSource = self; + +// [self getViolationDataForNYCRestaurants]; } -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. +#pragma mark - Location methods +-(void)setupLocationManager{ + self.locationManager = [[CLLocationManager alloc]init]; + self.locationManager.delegate = self; + self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; + if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { + [self.locationManager requestWhenInUseAuthorization]; + } } -@end +-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ + self.currentLocation = [locations lastObject]; + self.latLabel.text = [NSString stringWithFormat:@"%f.1f", self.currentLocation.coordinate.latitude]; + self.longLabel.text = [NSString stringWithFormat:@"%f.1f", self.currentLocation.coordinate.longitude]; +} + +-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ + UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an error retrieving your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; + [errorAlert show]; + NSLog(@"Error: %@",error.description); +} + +#pragma mark - textfield delegate methods + +-(BOOL)textFieldShouldReturn:(UITextField *)textField{ + + [self makeFoursquareRequestWithSearchTerm:textField.text ForLocation:self.currentLocation WithCallbackBlock:^{ + for (C4QRestaurant *restuarant in self.restaurants) { + NSLog(@"%@",restuarant.name); + NSLog(@"%@",restuarant.phoneNumber); + NSLog(@"********"); + } + [self.tableView reloadData]; + }]; + + //dismisses the keyboard + [self.view endEditing:YES]; + + return YES; +} + +#pragma mark - foursquare api methods + +-(void)makeFoursquareRequestWithSearchTerm:(NSString *)searchTerm ForLocation:(CLLocation *)location WithCallbackBlock:(void(^)())block{ + //start updating location at beginning of method; stop updating location at end + [self.locationManager startUpdatingLocation]; + + NSString *latitude = [NSString stringWithFormat:@"%.1f",location.coordinate.latitude]; + NSString *longitude = [NSString stringWithFormat:@"%.1f",location.coordinate.longitude]; + + NSString *urlString = [NSString stringWithFormat: @"https://api.foursquare.com/v2/venues/search?client_id=RCPNIN1V5V5GCZ0F3RSFCNFIOD2K2240ZIW2ZANAOFIJWV1O&client_secret=4YZZKIM1UUUXTVG4K3RX2Y4BV3TIG2RZDIB1KUTJJJPCTK5G&v=20130815§ion=food&ll=%@,%@&query=%@",latitude,longitude, searchTerm]; + NSString *encodedString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + NSURL *url = [NSURL URLWithString:encodedString]; + + [APIManager GETRequestWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (data != nil){ + + NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + NSDictionary *venues = [[json objectForKey:@"response"] objectForKey:@"venues"]; + + self.restaurants = [[NSMutableArray alloc] init]; + + for (NSDictionary *venue in venues) { + C4QRestaurant *restaurant = [[C4QRestaurant alloc] init]; + restaurant.name = [venue objectForKey: @"name"]; + NSString *phoneNumber = [[venue objectForKey:@"contact"] objectForKey:@"phone"]; + if (phoneNumber != nil) { //only add restaurants with a phone number + restaurant.phoneNumber = phoneNumber; + [self.restaurants addObject:restaurant]; + } + } + } + block(); + [self.locationManager stopUpdatingLocation]; + }]; +} + +#pragma mark - inspection data api methods + +-(void)getViolationDataForNYCRestaurants{ + + NSString *urlString = [NSString stringWithFormat: @"https://data.cityofnewyork.us/resource/9w7m-hzhe.json"]; + NSString *encodedString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + NSURL *url = [NSURL URLWithString:encodedString]; + + [APIManager GETRequestWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (data != nil){ + NSArray *jsonViolations = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + for (NSDictionary *violation in jsonViolations) { + NSString *name = [violation objectForKey:@"dba"]; + NSString *violationCode = [violation objectForKey:@"violation_code"]; + NSLog(@"%@ - %@",name,violationCode); + } + } + }]; +} + +#pragma mark - tableView methods + +-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ + return 1; +} + +-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ + return self.restaurants.count; +} + +-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ + + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RestaurantCellIdentifier" forIndexPath:indexPath]; + + C4QRestaurant *restaurantForCell = self.restaurants[indexPath.row]; + + cell.textLabel.text = restaurantForCell.name; + + return cell; +} + +@end \ No newline at end of file