objective-cios7uiimagemfmessagecomposeviewcontrollermessageui

Can't Attach Image to MFMessageComposeViewController


I'm doing the following, but when logging, it always returns that the image could not be attached. What's wrong here?

- (void)showInvitation {

if (![MFMessageComposeViewController canSendText]) {

    UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [warningAlert show];
    return;
}

NSString *message = [NSString stringWithFormat:@"Download this game!"];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setBody:message];

if ([MFMessageComposeViewController canSendAttachments]) {
    NSLog(@"Attachments Can Be Sent.");
    NSData *imgData = [NSData dataWithContentsOfFile:@"water"];
    BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:@"image.png"];

    if (didAttachImage) {
        NSLog(@"Image Attached.");

    } else {
        NSLog(@"Image Could Not Be Attached.");
    }
}

[self presentViewController:messageController animated:YES completion:nil];
}

Solution

  • As discussed in the comments, use addAttachmentURL:withAlternateFilename:. My guess is, the NSData object you provide does not fit the kUTTypePNG type and adding the attachment fails.