Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
- //get string from searchBar textfield
- NSString* searched = self.searchBar.text;
- //format the API call to search for the "searched" item (%@ after 'search/')
- NSString* formattedURL = [NSString stringWithFormat:@"https://api.nutritionix.com/v1_1/search/%@?results=0:100&fields=item_name,nf_total_fat,nf_protein,nf_total_carbohydrate&appId=f35a80a7&appKey=9263d4b1c216becb04681b1cd04d1815",searched];
- dispatch_async(dispatch_get_main_queue(), ^{
- [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:formattedURL] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
- NSDictionary* foodsFoundDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
- //array holds an array of all the foods found from the API call
- NSArray* array;
- array = [foodsFoundDict valueForKey:@"hits"];
- //since the info needed is inside the dictionary @"fields" of each array element, loop through array and add each dictionary to the global searchedFoodsArray
- for(NSDictionary* dict in array){
- [self.searchedFoodsArray addObject:dict[@"fields"]];
- }
- }]resume];
- [self.tableView reloadData];
- });
Add Comment
Please, Sign In to add comment