Sending photo using MFMessageComposeViewController is disabled in ios10

让人想犯罪 __ 提交于 2021-01-06 07:22:01

问题


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?


回答1:


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



回答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.

Hope it works for you!!!




回答3:


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


来源:https://stackoverflow.com/questions/39745761/sending-photo-using-mfmessagecomposeviewcontroller-is-disabled-in-ios10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!