diff --git a/TalkinToTheNet/FourSquareSearchResult.h b/TalkinToTheNet/FourSquareSearchResult.h new file mode 100644 index 0000000..764a72c --- /dev/null +++ b/TalkinToTheNet/FourSquareSearchResult.h @@ -0,0 +1,25 @@ +// +// FourSquareSearchResult.h +// TalkinToTheNet +// +// Created by Kaisha Jones on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import + + +@interface FourSquareSearchResult : NSObject + + +@property (nonatomic) NSString *barsName; + +@property (nonatomic) NSString *barsAddress; + +@property (nonatomic) NSString *barsPhoneNumber; + +@property (nonatomic) NSString *barsDistance; + +@property (nonatomic) NSString *barsSearchTerm; + +@end \ No newline at end of file diff --git a/TalkinToTheNet/FourSquareSearchResult.m b/TalkinToTheNet/FourSquareSearchResult.m new file mode 100644 index 0000000..4ffa2a9 --- /dev/null +++ b/TalkinToTheNet/FourSquareSearchResult.m @@ -0,0 +1,13 @@ +// +// FourSquareSearchResult.m +// TalkinToTheNet +// +// Created by Kaisha Jones on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "FourSquareSearchResult.h" + +@implementation FourSquareSearchResult + +@end diff --git a/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj b/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj index ee35a70..a55f44b 100644 --- a/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj +++ b/TalkinToTheNet/TalkinToTheNet.xcodeproj/project.pbxproj @@ -13,6 +13,10 @@ 8D7DCD541BAF859400A92AD2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8D7DCD521BAF859400A92AD2 /* Main.storyboard */; }; 8D7DCD561BAF859400A92AD2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8D7DCD551BAF859400A92AD2 /* Assets.xcassets */; }; 8D7DCD591BAF859400A92AD2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8D7DCD571BAF859400A92AD2 /* LaunchScreen.storyboard */; }; + A45248AC1BB629A000638D83 /* APIManager.m in Sources */ = {isa = PBXBuildFile; fileRef = A45248AB1BB629A000638D83 /* APIManager.m */; }; + A45248AF1BB629FE00638D83 /* FourSquareSearchResult.m in Sources */ = {isa = PBXBuildFile; fileRef = A45248AE1BB629FE00638D83 /* FourSquareSearchResult.m */; }; + A45248B21BB62C6700638D83 /* LocationFinderViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A45248B11BB62C6700638D83 /* LocationFinderViewController.m */; }; + A45248B51BB6302F00638D83 /* DetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A45248B41BB6302F00638D83 /* DetailViewController.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -26,6 +30,14 @@ 8D7DCD551BAF859400A92AD2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 8D7DCD581BAF859400A92AD2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 8D7DCD5A1BAF859400A92AD2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A45248AA1BB629A000638D83 /* APIManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APIManager.h; sourceTree = ""; }; + A45248AB1BB629A000638D83 /* APIManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APIManager.m; sourceTree = ""; }; + A45248AD1BB629FE00638D83 /* FourSquareSearchResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FourSquareSearchResult.h; path = ../FourSquareSearchResult.h; sourceTree = ""; }; + A45248AE1BB629FE00638D83 /* FourSquareSearchResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FourSquareSearchResult.m; path = ../FourSquareSearchResult.m; sourceTree = ""; }; + A45248B01BB62C6700638D83 /* LocationFinderViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationFinderViewController.h; sourceTree = ""; }; + A45248B11BB62C6700638D83 /* LocationFinderViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationFinderViewController.m; sourceTree = ""; }; + A45248B31BB6302F00638D83 /* DetailViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DetailViewController.h; sourceTree = ""; }; + A45248B41BB6302F00638D83 /* DetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DetailViewController.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,8 +70,16 @@ 8D7DCD481BAF859400A92AD2 /* TalkinToTheNet */ = { isa = PBXGroup; children = ( + A45248AD1BB629FE00638D83 /* FourSquareSearchResult.h */, + A45248AE1BB629FE00638D83 /* FourSquareSearchResult.m */, + A45248B01BB62C6700638D83 /* LocationFinderViewController.h */, + A45248B11BB62C6700638D83 /* LocationFinderViewController.m */, + A45248B31BB6302F00638D83 /* DetailViewController.h */, + A45248B41BB6302F00638D83 /* DetailViewController.m */, 8D7DCD4C1BAF859400A92AD2 /* AppDelegate.h */, 8D7DCD4D1BAF859400A92AD2 /* AppDelegate.m */, + A45248AA1BB629A000638D83 /* APIManager.h */, + A45248AB1BB629A000638D83 /* APIManager.m */, 8D7DCD4F1BAF859400A92AD2 /* ViewController.h */, 8D7DCD501BAF859400A92AD2 /* ViewController.m */, 8D7DCD521BAF859400A92AD2 /* Main.storyboard */, @@ -149,9 +169,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + A45248AC1BB629A000638D83 /* APIManager.m in Sources */, + A45248B21BB62C6700638D83 /* LocationFinderViewController.m in Sources */, 8D7DCD511BAF859400A92AD2 /* ViewController.m in Sources */, 8D7DCD4E1BAF859400A92AD2 /* AppDelegate.m in Sources */, 8D7DCD4B1BAF859400A92AD2 /* main.m in Sources */, + A45248AF1BB629FE00638D83 /* FourSquareSearchResult.m in Sources */, + A45248B51BB6302F00638D83 /* DetailViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -300,6 +324,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..a59c4f4 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/APIManager.h @@ -0,0 +1,21 @@ +// +// APIManager.h +// LearnAPI +// +// Created by Kaisha Jones on 9/20/15. +// Copyright © 2015 Kaisha Jones. All rights reserved. +// + +#import + +@interface APIManager : NSObject + + +// this is a public method + ++ (void)GETRequestWithURL:(NSURL *)URL + completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error)) completionHandler; + +// completionHandler isEqualtoBlock I think, must adk mike ot google + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/APIManager.m b/TalkinToTheNet/TalkinToTheNet/APIManager.m new file mode 100644 index 0000000..af756cb --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/APIManager.m @@ -0,0 +1,41 @@ +// +// APIManager.m +// LearnAPI +// +// Created by Kaisha Jones on 9/20/15. +// Copyright © 2015 Kaisha Jones. All rights reserved. +// + +#import "APIManager.h" + +@implementation APIManager + + +// create a data task method. completely stateless, not going to maniupulate anything outside of it +// completion handler is the actual blokc that we are passing. + ++ (void)GETRequestWithURL:(NSURL *)URL +// makes it a class method, we don't have to alloc init it + completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error)) completionHandler { + +// this accesses the shared session + + NSURLSession *session = [NSURLSession sharedSession]; + + +// I think this creates a data task to execute a api request. By default this happens on a background thread + NSURLSessionDataTask *task = [session dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { + + NSLog(@"%@", data); +// this moves us back to the main thread + dispatch_async(dispatch_get_main_queue(), ^{ + completionHandler(data, response, error); + }); + }]; +// this begins the task :) + [task resume]; + +} + + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard b/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard index f56d2f3..191001a 100644 --- a/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard +++ b/TalkinToTheNet/TalkinToTheNet/Base.lproj/Main.storyboard @@ -1,25 +1,182 @@ - + - + + + - - + + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + diff --git a/TalkinToTheNet/TalkinToTheNet/DetailViewController.h b/TalkinToTheNet/TalkinToTheNet/DetailViewController.h new file mode 100644 index 0000000..4b5a7d8 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/DetailViewController.h @@ -0,0 +1,20 @@ +// +// DetailViewController.h +// TalkinToTheNet +// +// Created by Kaisha Jones on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + + +#import +#import "FourSquareSearchResult.h" +#import "APIManager.h" +#import "LocationFinderViewController.h" + +@interface DetailViewController : UIViewController + + + +@property (nonatomic) FourSquareSearchResult *dataTransfered; +@end diff --git a/TalkinToTheNet/TalkinToTheNet/DetailViewController.m b/TalkinToTheNet/TalkinToTheNet/DetailViewController.m new file mode 100644 index 0000000..eb1e607 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/DetailViewController.m @@ -0,0 +1,69 @@ +// +// DetailViewController.m +// TalkinToTheNet +// +// Created by Kaisha Jones on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "DetailViewController.h" + +@interface DetailViewController () + +@property (strong, nonatomic) IBOutlet UILabel *barNameLabel; +@property (strong, nonatomic) IBOutlet UILabel *addressLabel; +@property (strong, nonatomic) IBOutlet UILabel *detailsLabel; + +//@property (nonatomic) NSString *searchResults; +@property (nonatomic) IBOutlet UILabel *dataLabel; + +@end + +@implementation DetailViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + self.barNameLabel.text = self.dataTransfered.barsName; + self.addressLabel.text = self.dataTransfered.barsAddress; + self.detailsLabel.text = self.dataTransfered.barsDistance; + + [self makeNewInstagramAPIRequestWithSearchTerm: self.dataTransfered.barsSearchTerm callbackBlock:^{ + [self.dataLabel reloadInputViews]; + }]; + +} + +#pragma mark - Instagram API Request +- (void)makeNewInstagramAPIRequestWithSearchTerm: (NSString *)searchTerm // pass four square search term + callbackBlock:(void(^)())block { // call block + + // search terms via url + NSString *instagramURL = [NSString stringWithFormat:@"https://api.instagram.com/v1/tags/%@/media/recent?client_id=ac0ee52ebb154199bfabfb15b498c067", searchTerm]; + + NSString *encodedString = [instagramURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; // encode string so that it can pass more than one word + + // test + NSLog(@"my second api url: %@", encodedString); + + // convert to a url + NSURL *url = [NSURL URLWithString:encodedString]; + + // for some reason part fails :( + [APIManager GETRequestWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + + if (data != nil) { + NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + + NSArray *establishments = [[json objectForKey:@"data"] objectForKey:@"link"]; + + NSLog(@"%@", establishments); + + } + block(); + }]; +} + + + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/LocationFinderViewController.h b/TalkinToTheNet/TalkinToTheNet/LocationFinderViewController.h new file mode 100644 index 0000000..c072d89 --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/LocationFinderViewController.h @@ -0,0 +1,13 @@ +// +// LocationFinderViewController.h +// TalkinToTheNet +// +// Created by Kaisha Jones on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface LocationFinderViewController : UIViewController + +@end diff --git a/TalkinToTheNet/TalkinToTheNet/LocationFinderViewController.m b/TalkinToTheNet/TalkinToTheNet/LocationFinderViewController.m new file mode 100644 index 0000000..a469eee --- /dev/null +++ b/TalkinToTheNet/TalkinToTheNet/LocationFinderViewController.m @@ -0,0 +1,169 @@ +// +// LocationFinderViewController.m +// TalkinToTheNet +// +// Created by Kaisha Jones on 9/25/15. +// Copyright © 2015 Mike Kavouras. All rights reserved. +// + +#import "LocationFinderViewController.h" +#import "FourSquareSearchResult.h" +#import "DetailViewController.h" +#import "APIManager.h" + +@interface LocationFinderViewController () + +@property (weak, nonatomic) IBOutlet UITextField *searchTextField; +@property (weak, nonatomic) IBOutlet UITableView *listTableView; +@property (nonatomic) NSMutableArray *searchResults; +@property (nonatomic) NSString *searchWords; + +@end + +@implementation LocationFinderViewController + +#pragma mark - setup for the view + + +- (void)viewDidLoad { + [super viewDidLoad]; + + // this is where we connect the delegates + self.listTableView.delegate = self; + self.listTableView.dataSource = self; + self.searchTextField.delegate = self; +} + + +#pragma mark - FourSquare API Request +- (void)makeNewFourSquareAPIRequestWithSearchTerm: (NSString *)searchTerm // pass four square search term + callbackBlock:(void(^)())block { // call block + + // search terms via url + NSString *urlString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/establishments/venues/search?client_id=M1KDUWRS5OBWUNQCXHHF23TAUEG2YOB0RXGBSP0LBVRCX2XL&client_secret=FWTAPZOJ4UBPUXX2R5Q1D5F3X0HXMCSMERWL4DJFW3UA33YX&v=20150919&ll=40.7,-74&query=%@", searchTerm]; + + self.searchWords = searchTerm; + + //self.searchWords = searchTerm; + //NSLog(@"%@", self.searchWords); + + + // &ll=40.7,-74 = latitude/longitude + + // encode url strings (so you can search for more than one word with spaces!) + NSString *encodedString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + + // NSLog(@"%@", encodedString); // test it! + + // convert urlString to url + NSURL *url = [NSURL URLWithString:encodedString]; + + // make the request + [APIManager GETRequestWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + + if (data != nil) { + + NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data + options:0 + error:nil]; + + // NSLog(@"%@", json); and log dictionary to test + + NSArray *venues = [[json objectForKey:@"response"] objectForKey:@"venues"]; + + // NSLog(@"%@", venues); + + self.searchResults = [[NSMutableArray alloc]init]; // initialize storage for array + + for (NSDictionary *venue in venues) { // creating a loop + + NSString *establishmentName = [venue objectForKey:@"name"]; // grab info from dictionary + NSString *establishmentLocation = [venue objectForKey:@"location"]; + + NSString *address = [establishmentLocation valueForKey:@"address"]; + NSString *city = [establishmentLocation valueForKey:@"city"]; + NSString *state = [establishmentLocation valueForKey:@"state"]; + + NSString *distance = [establishmentLocation valueForKey:@"distance"]; // get the distance + + // convert string into double to calculate miles + double distanceConvertedToDouble = [distance doubleValue]; + double metersInAMile = 1000.00; + double distanceInMiles = distanceConvertedToDouble / metersInAMile; + // NSLog(@"%.2f", distanceInMiles); + + // then we convert it back to a string + NSString *stringInMiles = [NSString stringWithFormat:@"%.2f", distanceInMiles]; + + // NSLog(@"%@", stringInMiles); we test to see if its working + + // this should include all results even the ones missing from address + if (address == nil){ + address = @""; + } + if (city == nil){ + city = @""; + } + + FourSquareSearchResult *resultsObject = [[FourSquareSearchResult alloc]init]; + + resultsObject.barsName = establishmentName; + resultsObject.barsAddress = [NSString stringWithFormat:@"%@, %@, %@", address, city, state]; + resultsObject.barsDistance = [NSString stringWithFormat:@"distance: %@ miles", stringInMiles]; + resultsObject.barsSearchTerm = self.searchWords; + + [self.searchResults addObject:resultsObject]; + } + block(); + } + }]; +} + +#pragma mark - uitextField method + + +- (BOOL)textFieldShouldReturn:(UITextField *)textField { // when return button on keyboard is pressed..... + + [self.view endEditing:YES]; // end editing + dismiss keyboard + + [self makeNewFourSquareAPIRequestWithSearchTerm:textField.text callbackBlock:^{ //make an API request + + [self.listTableView reloadData]; // reload table data + }]; + return YES; +} + +#pragma mark - tableView setup methods + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return self.searchResults.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath]; + + FourSquareSearchResult *currentResult = self.searchResults[indexPath.row]; + + cell.textLabel.text = currentResult.barsName; + cell.detailTextLabel.text = currentResult.barsDistance; + + return cell; +} + +#pragma mark - prepareForSegue + +- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + + // if ([segue.identifier isEqualToString:@"showDetailViewControllerIdentifier"]) { // reference segue title + // where we pass the data + NSIndexPath *myIndexPath = [self.listTableView indexPathForSelectedRow]; + FourSquareSearchResult *dataTransfer = self.searchResults[myIndexPath.row]; + DetailViewController *dvc = segue.destinationViewController; // reference to detail view controller + dvc.dataTransfered = dataTransfer; +} + +@end