Pergunta

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];
}
Foi útil?

Solução

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.

Outras dicas

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)
            })


        }
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top