Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Lend Kazazi
223 PointsSending a picture to all users
So my question is to send a picture directly to all recipients without showing the view of the friends list just like snapchats Story idea If somebody could help me out please leave a comment below I asked this a couple of times and i didn't get help at all so please take this in consideration.
Thanks :) Ben Jakuben
8 Answers
landonferrier
25,097 PointsLend, this is a very simple task! What you will need to do is get the query of friends and add them to an array of recipients that will be accessable from the PFObject.
Although this is slightly different than the tutorials code, the small change will come in handy for cleaner code. Here is how to get a list of friends:
- (void)retrieveFriendsWithTableViewUpdate:(BOOL)updateTableView
{
dispatch_async(dispatch_get_main_queue(), ^{
//Retreieve the friends relation for the current user. This will be used to display the friends in the table view.
self.friendsRelation = [[PFUser currentUser] relationForKey:@"friendsRelation"];
//Retrieve a copy of the query from the friendsRelation.s
PFQuery *query = [self.friendsRelation query];
//Order the query by the username of a user.
[query orderByAscending:@"username"];
//Finally, find the objects!
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
self.recipients = objects; //My guess for your code, change this to the array of recipients and you will be ready to go!
if (updateTableView)
{
//The friends array has changed so we will need to reload the table view so the new data is displayed for the user.
[self.tableView reloadData];
}
}
else
{
//If an error is returned, log it in Ben's special formatted and easy to read way!
NSLog(@"error: %@ %@", error, [error userInfo]);
}
}];
});
}
This method will retrieve a list of users on the iOS devices main queue for the current user.
If you have any questions, feel free to ask!
Lend Kazazi
223 PointsThanks a lot Landon, i actually found it a bit confusing though because i get some errors this is my code and if you could help please respond
import "NewRecipeViewController.h"
import "MBProgressHUD.h"
import
import
@interface NewRecipeViewController () - (IBAction)save:(id)sender; - (IBAction)cancel:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *recipeImageView; @property (weak, nonatomic) IBOutlet UITextField *nameTextField; @property (weak, nonatomic) IBOutlet UITextField *prepTimeTextField; @property (weak, nonatomic) IBOutlet UITextField *ingredientsTextField;
@end
@implementation NewRecipeViewController
(id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; }
(void)viewDidLoad { [super viewDidLoad];
_nameTextField.delegate = self; _prepTimeTextField.delegate = self; _ingredientsTextField.delegate = self;
}
(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
pragma mark - Table view delegate
(void)showPhotoLibary { if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)) { return; }
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init]; mediaUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// Displays saved pictures from the Camera Roll album. mediaUI.mediaTypes = @[(NSString*)kUTTypeImage];
// Hides the controls for moving & scaling pictures mediaUI.allowsEditing = NO;
mediaUI.delegate = self;
[self.navigationController presentModalViewController: mediaUI animated: YES]; }
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { [self showPhotoLibary]; } }
(void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage]; self.recipeImageView.image = originalImage;
[picker dismissViewControllerAnimated:YES completion:nil];
} - (IBAction)save:(id)sender { // Create PFObject with recipe information PFObject *recipe = [PFObject objectWithClassName:@"Posts"]; [recipe setObject:_nameTextField.text forKey:@"description"]; [recipe setObject:_prepTimeTextField.text forKey:@"time"];
NSArray *ingredients = [_ingredientsTextField.text componentsSeparatedByString: @","];
[recipe setObject:ingredients forKey:@"location"];
// Recipe image
NSData *imageData = UIImageJPEGRepresentation(_recipeImageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:@"%@.png", _nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[recipe setObject:imageFile forKey:@"imageFile"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Uploading";
[hud show:YES];
// Upload recipe to Parse
[recipe saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[hud hide:YES];
if (!error) {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Complete" message:@"Successfully saved the post" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
// Notify table view to reload the recipes from Parse cloud
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTable" object:self];
// Dismiss the controller
[self dismissViewControllerAnimated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
(IBAction)cancel:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; }
(void)viewDidUnload { [self setRecipeImageView:nil]; [self setNameTextField:nil]; [self setPrepTimeTextField:nil]; [self setIngredientsTextField:nil]; [super viewDidUnload]; }
pragma mark - Textfield delegate
(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }
@end
landonferrier
25,097 PointsLend, I am not able to find the error in this code; what lines are showing errors?
Lend Kazazi
223 PointsLandon Ferrier i am sorry for my mistake i wanted help into implementing your code into that class because i am new to this and i am stuck here so i would really appreciate your help
Thanks :)
landonferrier
25,097 PointsDo you want the class to query all the recipes for a user or object?
landonferrier
25,097 PointsHere is how the code flow works for this task: Create the PFObject recipe and give it all keys and values for use > Save to Parse. I can't give precise code for your app but here is my guess.
PFObject *recipe = [PFObject objectWithClassName:@"Recipes"];
recipe[@"creator"] = @"Lend Kazazi";
recipe[@"title"] = @"Scrambled Eggs";
recipe[@"indgredients"] = "Eggs";
recipe[@"recipients"] = //The array of recipients.
[recipe saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
//Hooray, the recipie has been successfully saved to Parse.
}
else
{
//Something bad happened like a internet request error, printing the error is always a smart idea if you have a reoccuring problem.
NSLog(@"Error: %@ %@", error, [error localizedDescription]);
}
}];
If you have any questions, feel free to ask!
Lend Kazazi
223 PointsLandon Ferrier so i want to send a object/recipie to all recipients by clicking the send button and also displaying them to a separate tableview
landonferrier
25,097 PointsAdd the code above into the send button's ibaction and this will save the message. What are you looking to do in a separate table view?
Lend Kazazi
223 PointsLandon Ferrier http://prntscr.com/720ssd this was the error i was talking about and i don't know how to fix that i would really appreciate your help
Thanks :)
landonferrier
25,097 PointsLend, it seems that these variables have not yet been declared, you will need to create these variables in the header file. Are you using the setup similar to the camera view controller?
Lend Kazazi
223 PointsLandon Ferrier yes
landonferrier
25,097 PointsLend, your error is that the friendsRelation variable is not existent, have you created this PFRelation variable in the header or class implementation?
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsHI, i dont know and i have no idea what so ever but, have you tried to look on people blog? maybe on youtubue? articles? articles seem very effective, far more then youtube videos.
And if you will search for it, type only key words to google.
Hope this helps.