1

I have message send functionality in my app and implemented the same using MFMessageComposeViewController. I am able to attach photos with the message in iOS9 but not in iOS 10? Is there anyone having the same issue?

JamesDon
  • 65
  • 1
  • 9
  • Did you ever find a solution to this? It appears that apple is not allowing the user to attach an image from the modal. It still shows the icon but nothing happens when I select it. – Douglas Cobb Nov 09 '16 at 21:34
  • No...Not Yet. I have raised this concern in apple forums too and not yet received any reply for that. Did apple provides any explanation for this? – JamesDon Nov 10 '16 at 13:59
  • Hi @JamesDon, same issue here. Did you find any solution ? – raphael Nov 23 '16 at 10:09
  • 1
    I am seeing the same issue here. Pressing the > button to the left of message, shows 3 more buttons - all of which are disabled. – David Jul 17 '17 at 23:22

3 Answers3

5

Swift 5.0 version: Call the below method named displayMessageInterface:

- Important to note:

  • composeViewController.addAttachmentData(dataImage!, typeIdentifier: "image/png", filename: "ImageData.png")

    In the above line, filename must be of type abc.png in my case or abc.jpeg if you are using a jpeg image data and typeIdentifier must follow image/png and image/jpeg respectively. I struggled a lot to find out this. The reason I write this answer even when other answers are enough already.

For more information on typeIdentifiers, use this link: https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

fileprivate func displayMessageInterface() {
    if MFMessageComposeViewController.canSendText() {
        let composeViewController = MFMessageComposeViewController()
        composeViewController.messageComposeDelegate = self
        composeViewController.body = "Enter your text body here."

        if MFMessageComposeViewController.canSendAttachments() {
            let image = UIImage(named: "image-name")!
            let dataImage =  image.pngData()
            guard dataImage != nil else {
                return
            }
            composeViewController.addAttachmentData(dataImage!, typeIdentifier: "image/png", filename: "ImageData.png")
        }
        self.present(composeViewController, animated: true)
    } else {
        print("Can't send messages.")
    }
}

Since I have mentioned the delegate in the above method, you can use it this way in case of a UIViewController:

extension UIViewController: MFMessageComposeViewControllerDelegate {
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        if result == .failed {
            print("could not send message")
        }
        self.dismiss(animated: true)
    } 
}
Community
  • 1
  • 1
Vinay Kharb
  • 171
  • 2
  • 4
2

Please find the below code as image attachment and I had successfully run on iOS 10.

- (void)sendImgAttachment {

    if([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init]; // Create message VC
        messageController.messageComposeDelegate = self; // Set delegate to current instance

        NSMutableArray *recipients = [[NSMutableArray alloc] init]; // Create an array to hold the recipients
        [recipients addObject:@"555-555-5555"]; // Append example phone number to array
        messageController.recipients = recipients; // Set the recipients of the message to the created array

        messageController.body = @"Example message"; // Set initial text to example message

        NSData *dataImg = UIImagePNGRepresentation([UIImage imageNamed:@"logoApple"]);//Add the image as attachment
        [messageController addAttachmentData:dataImg typeIdentifier:@"public.data" filename:@"Image.png"];

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

Please find the screenshot for the same.

Scrrenshot

Hope it works for you!!!

Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
  • Hi Ram,Thank you for your response. My scenario is, If I am adding the attachment through code it works for me. But I need to attach the photo from photo gallery by clicking on the camera icon. All the icons including camera is disabled in the message view controller window in iOS 10. I am unable to tap on that. The same code works for me in iOS9. Did you come across this scenario? – JamesDon Nov 08 '16 at 11:58
  • Hi JamesDon, could you please share the screenshot for iOS 10 where the camera button is disabled so that I can review it. – Ramkrishna Sharma Nov 09 '16 at 06:17
  • @JamesDon Did you find any solution to enable those buttons? – Arpit Dongre Jul 28 '17 at 08:49
2

i found this solution for me :

if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: "sms:123456789")!, options: [:], completionHandler: nil)
        } else {
            // Fallback on earlier versions
            if MFMessageComposeViewController.canSendText() {
                if MFMessageComposeViewController.canSendAttachments() {
                    print("canSendAttachments")
                }
                let messageVC = MFMessageComposeViewController()
                messageVC.body = "Enter a message";
                messageVC.recipients = ["123456789"]
                messageVC.messageComposeDelegate = self
               messageVC.accessibilityActivate()
                self.present(messageVC, animated: false, completion: nil)
            } else {
                print("Cant send sms")
            }
        }
Phung Du
  • 101
  • 5
  • Using the line with `accessibilityActivate()` causes the debugger to spit out a lot of `AX Exchange error: Error Domain=Accessibility Code=0 "Remote service does not respond to _accessibilityMachPort" UserInfo={NSLocalizedDescription=Remote service does not respond to _accessibilityMachPort}` – David Jul 17 '17 at 23:19