带附件的应用程序短信 - MFMessageComposeViewControllerDelegate 相关错误

In App SMS With Attachment - MFMessageComposeViewControllerDelegate Related Errors

我有以下代码可以在应用程序内截取我的屏幕的部分快照,然后打开一个 SMS 对话 window。

import UIKit
import MessageUI
import AVFoundation


class ViewController: UIViewController, MFMailComposeViewControllerDelegate
{


@IBAction func smsScreenShot(sender: AnyObject) {

    audioPlayer.play()

    // Declare the snapshot boundaries 
    let top: CGFloat = 100
    let bottom: CGFloat = 60

    // The size of the cropped image
    let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)

    // Start the context
    UIGraphicsBeginImageContext(size)
    CGContextTranslateCTM(context, 0, -top)

    // Draw the view into the context (this is the snapshot)
    view.layer.renderInContext(context)
    let snapshot = UIGraphicsGetImageFromCurrentImageContext()

    // End the context (this is required to not leak resources)
    UIGraphicsEndImageContext()

    // Composing the SMS

    if !MFMessageComposeViewController.canSendText() {
        print("SMS services are not available")
    }

    if (MFMessageComposeViewController.canSendText()) {

        let composeVC = MFMessageComposeViewController()

        composeVC.recipients = ["Enter tel no"]
        composeVC.body = "Have a look at this cool image!";

        // Attaching the image to the SMS.
        let image = snapshot
        let imageData = UIImagePNGRepresentation(image)
        composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myImage")

        self.presentViewController(composeVC, animated:true, completion:nil)

    }

}

我意识到我必须在 class 结构中添加 MFMessageComposeViewControllerDelegate 就在 'MFMailComposeViewControllerDelegate'.

之后

这样做时,我收到以下错误:

Type 'ViewController' does not conform to protocol 'MFMessageComposeViewControllerDelegate'

这是因为我没有关闭控制器。

为了关闭 MFMMessageComposeViewController,我添加了以下代码:

func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {

switch result.value {
case MessageComposeResultCancelled.value:
    NSLog("cancelled")
case MessageComposeResultFailed.value:
    NSLog("cancelled")
case MessageComposeResultSent.value:
    NSLog("cancelled")
default:
    NSLog("default...")
}
controller.dismissViewControllerAnimated(true, completion: nil)
}

然后我插入了这段代码:

self.presentViewController(composeVC, animated:true, completion:nil)

但现在我遇到了相同的 Type 'ViewController' does not conform to protocol 'MFMessageComposeViewControllerDelegate' 错误以及 switch 语句中的错误。

Switch 语句错误

首先,我在使用result.value时出错,所以我将其更改为result.rawValue。这似乎解决了那个错误。

但我现在在 case 语句中有错误:Value of type 'MessageComposeResult' has no member 'value'。我认为这是因为围绕 MessageComposeResult 的语法已过时或错误?

如何修改我的案例陈述?我想如果我修复了 case 语句,我所有的问题都会得到解决?

我花了几个小时在网上搜索类似的问题和 material,但我真的被难住了。

有人能告诉我哪里错了吗?以及如何修改我的代码?

非常感谢。

修改后的代码

import UIKit
import MessageUI
import AVFoundation


    class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate {

   // Composing the SMS

    if !MFMessageComposeViewController.canSendText() {
        print("SMS services are not available")
    }

    if (MFMessageComposeViewController.canSendText()) {


        let composeVC = MFMessageComposeViewController()


        composeVC.recipients = ["Enter tel no"]
        composeVC.body = "Have a look at this cool image!";
        composeVC.messageComposeDelegate = self;

        // Attaching the image to the SMS.

        let image = snapshot
        let imageData = UIImagePNGRepresentation(image)
        composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myImage")



        self.presentViewController(composeVC, animated:true, completion:nil)

        func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
            switch result {
            case MessageComposeResultCancelled:
                NSLog("Cancelled")

            case MessageComposeResultFailed:
                NSLog("Failed")

            case MessageComposeResultSent:
                NSLog("Sent")

            default:
                NSLog("Unknown result")

            }

        }

你的问题是你使用的是 MFMessageComposeViewController 但你说过你的 class 是 MFMailComposeViewControllerDelegate - Message vs 邮件。您已经为 MFMessageComposeViewController 实现了委托方法,它与您的 class 的声明不匹配,所以您得到了错误。

你想要

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate

然后

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
    switch result {
    case MessageComposeResultCancelled:
        NSLog("Cancelled")

    case MessageComposeResultFailed:
        NSLog("Failed")

    case MessageComposeResultSent:
        NSLog("Sent")

    default:
        NSLog("Unknown result")

    }

}

此外,您还没有将视图控制器设置为委托,因此不会调用委托方法:

let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate=self