diff --git a/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj b/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj index ee35a70..ee952e5 100644 --- a/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj +++ b/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj @@ -7,6 +7,12 @@ objects = { /* Begin PBXBuildFile section */ + 0B5DCC161BB35BDF006754DB /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B5DCC151BB35BDF006754DB /* MapKit.framework */; }; + 0B5DCC191BB35C08006754DB /* MapViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B5DCC181BB35C08006754DB /* MapViewController.m */; settings = {ASSET_TAGS = (); }; }; + 0B5DCC1C1BB35DDB006754DB /* APIManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B5DCC1B1BB35DDB006754DB /* APIManager.m */; settings = {ASSET_TAGS = (); }; }; + 0B5DCC4D1BB62A41006754DB /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B5DCC4C1BB62A41006754DB /* CoreLocation.framework */; }; + 0B5DCC501BB62EAB006754DB /* CustomFSTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B5DCC4F1BB62EAB006754DB /* CustomFSTableViewCell.m */; settings = {ASSET_TAGS = (); }; }; + 0B5DCC531BB6375B006754DB /* Foursquare.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B5DCC521BB6375B006754DB /* Foursquare.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 +22,16 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 0B5DCC151BB35BDF006754DB /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; }; + 0B5DCC171BB35C08006754DB /* MapViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MapViewController.h; sourceTree = ""; }; + 0B5DCC181BB35C08006754DB /* MapViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MapViewController.m; sourceTree = ""; }; + 0B5DCC1A1BB35DDB006754DB /* APIManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APIManager.h; sourceTree = ""; }; + 0B5DCC1B1BB35DDB006754DB /* APIManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APIManager.m; sourceTree = ""; }; + 0B5DCC4C1BB62A41006754DB /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; }; + 0B5DCC4E1BB62EAB006754DB /* CustomFSTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomFSTableViewCell.h; sourceTree = ""; }; + 0B5DCC4F1BB62EAB006754DB /* CustomFSTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomFSTableViewCell.m; sourceTree = ""; }; + 0B5DCC511BB6375B006754DB /* Foursquare.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Foursquare.h; sourceTree = ""; }; + 0B5DCC521BB6375B006754DB /* Foursquare.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Foursquare.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 +49,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 0B5DCC4D1BB62A41006754DB /* CoreLocation.framework in Frameworks */, + 0B5DCC161BB35BDF006754DB /* MapKit.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -42,6 +60,8 @@ 8D7DCD3D1BAF859400A92AD2 = { isa = PBXGroup; children = ( + 0B5DCC4C1BB62A41006754DB /* CoreLocation.framework */, + 0B5DCC151BB35BDF006754DB /* MapKit.framework */, 8D7DCD481BAF859400A92AD2 /* TalkinToTheNet */, 8D7DCD471BAF859400A92AD2 /* Products */, ); @@ -60,8 +80,16 @@ children = ( 8D7DCD4C1BAF859400A92AD2 /* AppDelegate.h */, 8D7DCD4D1BAF859400A92AD2 /* AppDelegate.m */, + 0B5DCC1A1BB35DDB006754DB /* APIManager.h */, + 0B5DCC1B1BB35DDB006754DB /* APIManager.m */, + 0B5DCC511BB6375B006754DB /* Foursquare.h */, + 0B5DCC521BB6375B006754DB /* Foursquare.m */, 8D7DCD4F1BAF859400A92AD2 /* ViewController.h */, 8D7DCD501BAF859400A92AD2 /* ViewController.m */, + 0B5DCC171BB35C08006754DB /* MapViewController.h */, + 0B5DCC181BB35C08006754DB /* MapViewController.m */, + 0B5DCC4E1BB62EAB006754DB /* CustomFSTableViewCell.h */, + 0B5DCC4F1BB62EAB006754DB /* CustomFSTableViewCell.m */, 8D7DCD521BAF859400A92AD2 /* Main.storyboard */, 8D7DCD551BAF859400A92AD2 /* Assets.xcassets */, 8D7DCD571BAF859400A92AD2 /* LaunchScreen.storyboard */, @@ -152,6 +180,10 @@ 8D7DCD511BAF859400A92AD2 /* ViewController.m in Sources */, 8D7DCD4E1BAF859400A92AD2 /* AppDelegate.m in Sources */, 8D7DCD4B1BAF859400A92AD2 /* main.m in Sources */, + 0B5DCC191BB35C08006754DB /* MapViewController.m in Sources */, + 0B5DCC1C1BB35DDB006754DB /* APIManager.m in Sources */, + 0B5DCC501BB62EAB006754DB /* CustomFSTableViewCell.m in Sources */, + 0B5DCC531BB6375B006754DB /* Foursquare.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -300,6 +332,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..77ea42a --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/APIManager.h @@ -0,0 +1,16 @@ +// +// APIManager.h +// LearnAPIS +// +// Created by Artur Lan on 9/20/15. +// Copyright © 2015 Artur Lan. All rights reserved. +// + +#import + +@interface APIManager : NSObject + ++ (void)GETRequestWithURL:(NSURL *)URL + completionHandler:(void(^) (NSData *data, NSURLResponse *respone, NSError *error))completionHandler; + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/APIManager.m b/TalkinToTheNet/TalkinToTheNet/APIManager.m new file mode 100644 index 0000000..e09fe69 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/APIManager.m @@ -0,0 +1,33 @@ +// +// APIManager.m +// LearnAPIS +// +// Created by Artur Lan on 9/20/15. +// Copyright © 2015 Artur Lan. All rights reserved. +// + +#import "APIManager.h" + +@implementation APIManager + ++ (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..0ddba34 100644 --- a/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard +++ b/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard @@ -1,13 +1,31 @@ - + - + + + + + + + + + + + + + + + + + + + - + @@ -15,11 +33,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TalkinToTheNet/TalkinToTheNet/CustomFSTableViewCell.h b/TalkinToTheNet/TalkinToTheNet/CustomFSTableViewCell.h new file mode 100644 index 0000000..4ecefd7 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/CustomFSTableViewCell.h @@ -0,0 +1,15 @@ +// +// CustomFSTableViewCell.h +// TalkinToTheNet +// +// Created by Artur Lan on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface CustomFSTableViewCell : UITableViewCell +@property (weak, nonatomic) IBOutlet UILabel *nameOfVenue; +@property (weak, nonatomic) IBOutlet UILabel *typeOfVenue; + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/CustomFSTableViewCell.m b/TalkinToTheNet/TalkinToTheNet/CustomFSTableViewCell.m new file mode 100644 index 0000000..c344442 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/CustomFSTableViewCell.m @@ -0,0 +1,23 @@ +// +// CustomFSTableViewCell.m +// TalkinToTheNet +// +// Created by Artur Lan on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "CustomFSTableViewCell.h" + +@implementation CustomFSTableViewCell + +- (void)awakeFromNib { + // Initialization code +} + +- (void)setSelected:(BOOL)selected animated:(BOOL)animated { + [super setSelected:selected animated:animated]; + + // Configure the view for the selected state +} + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/Foursquare.h b/TalkinToTheNet/TalkinToTheNet/Foursquare.h new file mode 100644 index 0000000..8a54f81 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/Foursquare.h @@ -0,0 +1,23 @@ +// +// Foursquare.h +// TalkinToTheNet +// +// Created by Artur Lan on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import +#import + +@interface Foursquare : NSObject + +@property (nonatomic) NSString *venueName; +@property (nonatomic) NSString *venuecategories; +@property (nonatomic) NSString* name; +@property (nonatomic) NSString* address; +@property (nonatomic) CLLocationCoordinate2D coordinate; + +- (instancetype)initWithJSON: (NSDictionary *)json; + + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/Foursquare.m b/TalkinToTheNet/TalkinToTheNet/Foursquare.m new file mode 100644 index 0000000..a40817d --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/Foursquare.m @@ -0,0 +1,24 @@ +// +// Foursquare.m +// TalkinToTheNet +// +// Created by Artur Lan on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "Foursquare.h" + +@implementation Foursquare + +- (instancetype)initWithJSON: (NSDictionary *)json { + if (self = [super init]) { + + self.venueName = json[@"name"]; + self.venuecategories = json[@"categories"]; + return self; + } + return nil; +} + + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/Info.plist b/TalkinToTheNet/TalkinToTheNet/Info.plist index 6905cc6..521066c 100644 --- a/TalkinToTheNet/TalkinToTheNet/Info.plist +++ b/TalkinToTheNet/TalkinToTheNet/Info.plist @@ -2,6 +2,13 @@ + NSLocationWhenInUseUsageDescription + I want to find you + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + CFBundleDevelopmentRegion en CFBundleExecutable diff --git a/TalkinToTheNet/TalkinToTheNet/MapViewController.h b/TalkinToTheNet/TalkinToTheNet/MapViewController.h new file mode 100644 index 0000000..d1c684e --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/MapViewController.h @@ -0,0 +1,16 @@ +// +// MapViewController.h +// TalkinToTheNet +// +// Created by Artur Lan on 9/23/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface MapViewController : UIViewController + +@property (nonatomic) NSString* searchString; +@property (nonatomic) NSMutableArray *places; + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/MapViewController.m b/TalkinToTheNet/TalkinToTheNet/MapViewController.m new file mode 100644 index 0000000..4bab622 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/MapViewController.m @@ -0,0 +1,49 @@ +// +// MapViewController.m +// TalkinToTheNet +// +// Created by Artur Lan on 9/23/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "MapViewController.h" +#import +#import +#import "APIManager.h" +#import "Foursquare.h" + + +@interface MapViewController () +@property (weak, nonatomic) IBOutlet MKMapView *mapView; +@property (nonatomic) CLLocationManager *locationManager; + +@end + +@implementation MapViewController +- (void)viewDidLoad { + [super viewDidLoad]; + self.navigationItem.title = [NSString stringWithFormat:@"%@ near you", self.searchString]; + self.mapView.delegate = self; + +} + +- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation +{ + MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1150, 1150); + [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; + + for(Foursquare* place in self.places){ + + MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; + point.coordinate = place.coordinate; + point.title = place.name; + point.subtitle = place.address; + + [self.mapView addAnnotation:point]; + + } + +} + + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/ViewController.m b/TalkinToTheNet/TalkinToTheNet/ViewController.m index cbefa29..695f324 100644 --- a/TalkinToTheNet/TalkinToTheNet/ViewController.m +++ b/TalkinToTheNet/TalkinToTheNet/ViewController.m @@ -7,8 +7,27 @@ // #import "ViewController.h" +#import "MapViewController.h" +#import "APIManager.h" +#import +#import "CustomFSTableViewCell.h" +#import "Foursquare.h" + + + + +@interface ViewController () + +@property (weak, nonatomic) IBOutlet UITextField *searchTextField; +@property (nonatomic) NSArray *foursquareData; +@property (nonatomic) NSMutableArray *searchResults; +@property (nonatomic) CLLocationManager *locationManager; +@property (nonatomic) NSString* longitude; +@property (nonatomic) NSString* latitude; +@property (weak, nonatomic) IBOutlet UITableView *tableView; + + -@interface ViewController () @end @@ -16,7 +35,15 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + self.searchTextField.delegate = self; + self.locationManager = [[CLLocationManager alloc] init]; + self.locationManager.delegate = self; + self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; + [self.locationManager requestWhenInUseAuthorization]; + [self.locationManager startUpdatingLocation]; + + self.tableView.dataSource = self; + } - (void)didReceiveMemoryWarning { @@ -24,4 +51,113 @@ - (void)didReceiveMemoryWarning { // Dispose of any resources that can be recreated. } +-(BOOL)textFieldShouldReturn:(UITextField *)textField { + + [textField endEditing:YES]; + [self fetchFoursquareData:textField.text callBackBlock:^{ + + MapViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MapViewController"]; + mvc.places = self.searchResults; + mvc.searchString = textField.text; + [self.navigationController pushViewController:mvc animated:YES]; + + [self.tableView reloadData]; + + }]; + + + return YES; +} + +- (void)fetchFoursquareData:(NSString *)searchTerm callBackBlock:(void(^)())block { + + NSString *foursquareURL = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?client_id=KCX3F54DA42FOK4D0LFY0DZA1ZXUXMISE3KGXHRXDFPV5MH5&client_secret=VVJVDD522T3454CKV1CBRSABBJXQQ1DIXOZWGOMTCHQAHYK3&v=20130815&ll=37.785834,-122.406417&query=%@&radius=1000", searchTerm]; + + NSString *encodedString = [foursquareURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + + NSURL *requestURL = [NSURL URLWithString:encodedString]; + + + + [APIManager GETRequestWithURL:requestURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + + if (data != nil) { + + NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + NSDictionary *response = [json objectForKey:@"response"]; + NSArray *venues = [response objectForKey:@"venues"]; + + self.searchResults = [[NSMutableArray alloc] init]; + + for (NSDictionary *venue in venues) { + + NSString *longitude = [[venue objectForKey:@"location"] objectForKey:@"lng"]; + NSString *latitude = [[venue objectForKey:@"location"] objectForKey:@"lat"]; + + Foursquare *foursquareVenue = [[Foursquare alloc]init]; + foursquareVenue.name = [venue objectForKey:@"name"]; + foursquareVenue.address = [[venue objectForKey:@"location"] objectForKey:@"address"]; + foursquareVenue.coordinate = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]); + + + NSArray *categories = [venue objectForKey:@"categories"]; + NSDictionary *category = [categories firstObject]; + NSString *name = [venue objectForKey:@"name"]; + NSString *categoryName = [category objectForKey:@"name"]; + + foursquareVenue.venueName = name; + foursquareVenue.venuecategories = categoryName; + + + + [self.searchResults addObject:foursquareVenue]; + + } + + block(); + } + }]; +} + +-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ + UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to get your location" preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style: UIAlertActionStyleDefault handler:nil]; + + [errorAlert addAction:okAction]; + +} + +-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ + + CLLocation *location = locations.firstObject; + + self.latitude = [NSString stringWithFormat:@"%.1f", location.coordinate.latitude]; + self.longitude = [NSString stringWithFormat:@"%.1f",location.coordinate.longitude]; +} + + + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + + return self.searchResults.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + CustomFSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath]; + + Foursquare *post = self.searchResults[indexPath.row]; + + cell.nameOfVenue.text = post.venueName; + cell.typeOfVenue.text = post.venuecategories; + + + return cell; +} + + @end