在ios10中禁用使用MFMessageComposeViewController发送照片

时间:2016-09-28 11:09:23

标签: ios10 mfmessagecomposeview

我的应用程序中有消息发送功能,并使用MFMessageComposeViewController实现了相同功能。我可以在iOS9中附上照片但在iOS 10中没有吗?有没有人有同样的问题?

3 个答案:

答案 0 :(得分:2)

请将以下代码作为图片附件找到,并且我已成功运行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];
    }
}

请找到相同的屏幕截图。

Scrrenshot

希望它适合你!!!

答案 1 :(得分:2)

我找到了这个解决方案:

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

答案 2 :(得分:0)

Swift 5.0版本: 调用以下名为displayMessageInterface的方法:

-重要说明:

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

    在上一行中,对于我来说,文件名的类型必须为abc.png,如果使用的是jpeg图片数据,则文件名必须为abc.jpeg,并且typeIdentifier必须遵循image/pngimage/jpeg分别。 我很难找到答案。即使其他答案已经足够,我也写这个答案的原因。

有关typeIdentifiers的更多信息,请使用以下链接: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.")
    }
}

由于我在上面的方法中提到了委托,因此在UIViewController的情况下可以使用这种方式:

extension UIViewController: MFMessageComposeViewControllerDelegate {
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        if result == .failed {
            print("could not send message")
        }
        self.dismiss(animated: true)
    } 
}