我在iOS 10上使用Swift 3.我使用的是带有UIActivity子类的UIActivityViewController,其活动类型设置为mail。当调用当前的UIActivityViewController时,我在调试器中遇到错误:
2017-05-26 14:09:27.583 TrialActivityViewControllerMail3[798:21017] -[TrialActivityViewControllerMail3.TSSActivityMail setSourceIsManaged:]: unrecognized selector sent to instance 0x60800026c580
2017-05-26 14:09:27.605 TrialActivityViewControllerMail3[798:21017] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TrialActivityViewControllerMail3.TSSActivityMail setSourceIsManaged:]: unrecognized selector sent to instance 0x60800026c580'
关键消息是无法识别的选择器,发送到选择器setSourceIsManaged的实例。这涉及我无法控制的多线程系统。
如何解决此问题或解决问题或了解有关此问题的更多信息?
除了将我的代码放在这里之外,我还将整个项目放在GitHub上的git存储库中。它是一个小项目,仅用于尝试使用UIActivityViewController进行邮件活动的代码。
这是网址:
https://github.com/Shinehah-Gnolaum/TrialActivityViewControllerMail3.git
这是我的代码:
let mailItemProvider = TSSMailItemProvider(placeholderItem: textView.text as Any)
let activityItems = [mailItemProvider]
let activityMail = TSSActivityMail()
let applicationActivities = [activityMail]
let activityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: applicationActivities)
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) {
activityViewController.modalPresentationStyle = UIModalPresentationStyle.popover
activityViewController.preferredContentSize = CGSize(width: 0, height: 0)
activityViewController.popoverPresentationController?.barButtonItem = barButtonItemMore
}
present(activityViewController, animated: true, completion: nil)
这是TSSActivityMail.swift:
import UIKit
import MessageUI
class TSSActivityMail: UIActivity, MFMailComposeViewControllerDelegate {
var activityItem: Any?
let mailComposeViewController = MFMailComposeViewController()
override init() {
super.init()
}
// Succeed when activityType is remarked out or when it returns nil.
override var activityType: UIActivityType? {
get {
return UIActivityType.mail
}
}
override var activityTitle: String? {
get {
return "Mail"
}
}
override var activityImage: UIImage? {
get {
return UIImage(imageLiteralResourceName: "email.png")
}
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
// !!! Perform checks.
return true
}
override func prepare(withActivityItems activityItems: [Any]) {
activityItem = activityItems[0] // $$$ as! String
mailComposeViewController.mailComposeDelegate = self
let filename = "file.txt"
mailComposeViewController.setSubject("subject")
mailComposeViewController.setMessageBody("Message body.", isHTML: false)
let content = activityItem as! String // $$$ ('as! String' was added)
let length = content.lengthOfBytes(using: String.Encoding.utf8)
let outputData = NSData(bytes:content, length: length) as Data
mailComposeViewController.addAttachmentData(outputData, mimeType: "plain", fileName: filename)
}
override class var activityCategory: UIActivityCategory {
get {
return UIActivityCategory.action
}
}
override var activityViewController: UIViewController? {
get {
return mailComposeViewController as UIViewController // $$$ ('as UIViewController' was added)
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
print("TSSActivityMail.mailComposeController(_:_:_:)")
// Check the result or perform other tasks.
print("result.rawValue = \(result.rawValue)")
if let error = error {
print("error.localizedDescription = \(error.localizedDescription)")
self.activityDidFinish(false)
}
else
{
if !(result == MFMailComposeResult.sent) {
self.activityDidFinish(false)
}
else
{
self.activityDidFinish(true)
}
}
}
}
这是TSSMailItemProvider.swift:
import UIKit
class TSSMailItemProvider: UIActivityItemProvider {
var placeholderItemAny: Any
var localActivityType: UIActivityType?
override init(placeholderItem: Any) {
placeholderItemAny = placeholderItem
super.init(placeholderItem: placeholderItem)
}
override var item: Any {
get {
let itemAny = NSString(string: placeholderItemAny as! String)
localActivityType = UIActivityType.mail
return itemAny
}
}
override var placeholderItem: Any? {
get {
return placeholderItemAny
}
}
override var activityType: UIActivityType? {
get {
return localActivityType
}
}
}