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

iOS

Swift: Sending text

So as i was working on Builiding a Self-Distructing Message app i came up with an idea of sending text instead of images/videos and making it a texting app. Could anybody help me with that :)?

4 Answers

can i seeth e code then abe i can fighure something out

can i seeth e code then abe i can fighure something out

can i see the basic code then maybe i can see somthing out ps my sisters got to the answers above sorry :{}

haydensteed

So this is the coding i needed to upload the images to the backend

pragma mark - Helper methods

  • (void)uploadMessage { NSData *fileData; NSString *fileName; NSString *fileType;

    if (self.image != nil) { UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHeight:480.0f]; fileData = UIImagePNGRepresentation(newImage); fileName = @"image.png"; fileType = @"image"; } else { fileData = [NSData dataWithContentsOfFile:self.videoFilePath]; fileName = @"video.mov"; fileType = @"video"; }

    PFFile *file = [PFFile fileWithName:fileName data:fileData]; [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!" message:@"Please try sending your message again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else { PFObject *message = [PFObject objectWithClassName:@"Messages"]; [message setObject:file forKey:@"file"]; [message setObject:fileType forKey:@"fileType"]; [message setObject:self.recipients forKey:@"recipientIds"]; [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"]; [message setObject:[[PFUser currentUser] username] forKey:@"senderName"]; [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!" message:@"Please try sending your message again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else { // Everything was successful! [self reset]; } }]; } }]; }

  • (void)reset { self.image = nil; self.videoFilePath = nil; [self.recipients removeAllObjects]; }

  • (UIImage *)resizeImage:(UIImage *)image toWidth:(float)width andHeight:(float)height { CGSize newSize = CGSizeMake(width, height); CGRect newRectangle = CGRectMake(0, 0, width, height); UIGraphicsBeginImageContext(newSize); [self.image drawInRect:newRectangle]; UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

    return resizedImage; }

@end

And this is for displaying the images

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [self.messages count]; }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject *message = [self.messages objectAtIndex:indexPath.row]; cell.textLabel.text = [message objectForKey:@"senderName"];

    NSString *fileType = [message objectForKey:@"fileType"]; if ([fileType isEqualToString:@"image"]) { cell.imageView.image = [UIImage imageNamed:@"icon_image"]; } else { cell.imageView.image = [UIImage imageNamed:@"icon_video"]; }

    return cell; }

pragma mark - Table view delegate

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.selectedMessage = [self.messages objectAtIndex:indexPath.row]; NSString *fileType = [self.selectedMessage objectForKey:@"fileType"]; if ([fileType isEqualToString:@"image"]) { [self performSegueWithIdentifier:@"showImage" sender:self]; } else { // File type is video PFFile *videoFile = [self.selectedMessage objectForKey:@"file"]; NSURL *fileUrl = [NSURL URLWithString:videoFile.url]; self.moviePlayer.contentURL = fileUrl; [self.moviePlayer prepareToPlay]; [self.moviePlayer thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];

    // Add it to the view controller so we can see it
    [self.view addSubview:self.moviePlayer.view];
    [self.moviePlayer setFullscreen:YES animated:YES];
    

    }

    // Delete it! NSMutableArray *recipientIds = [NSMutableArray arrayWithArray:[self.selectedMessage objectForKey:@"recipientIds"]]; NSLog(@"Recipients: %@", recipientIds);

    if ([recipientIds count] == 1) { // Last recipient - delete! [self.selectedMessage deleteInBackground]; } else { // Remove the recipient and save [recipientIds removeObject:[[PFUser currentUser] objectId]]; [self.selectedMessage setObject:recipientIds forKey:@"recipientIds"]; [self.selectedMessage saveInBackground]; }

}

Thanks :)