Reputation: 4755
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];
}
Upvotes: 0
Views: 3081
Reputation: 49
Please try this code. It is working fine for me.
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Solution of broken image in composer while sending through MFMessageComposserViewController "
controller.messageComposeDelegate = self
if image.imageAsset != nil {
let imageData = UIImageJPEGRepresentation(self.fixOrientation(img: image), 1) //! as NSData
controller.addAttachmentData(imageData! , typeIdentifier: "image/.jpeg", filename: "image.jpeg")
}
self.present(controller, animated: true, completion: {
completion(true)
})
}
}
Upvotes: 0
Reputation: 57040
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.
Upvotes: 2