UIActivityViewController在iOS 10中
问题描述:
任何人都知道如何在iOS 10中使用UIActivityView?现在,由于某种原因,在Swift 3.0中它将编译和构建,但是当应用程序在按下共享按钮后使用以下代码导致应用程序崩溃时运行时......它在iOS 9.3和Swift 2.0中完美运行。UIActivityViewController在iOS 10中
如上代码行6或let objectsToShare = [textToShare] as! AnyObject
的评论指定导致线程1:信号SIGABRT和应用程序崩溃
@IBOutlet weak var detailDescriptionLabel: UITextView!
@IBAction func share(_ sender: AnyObject) {
let textToShare = detailDescriptionLabel.attributedText
let objectsToShare = [textToShare] as! AnyObject
// line above causes app crash in iOS 10 - compiled and built
// error is "Thread1: signal SIGABRT"
let activityVC = UIActivityViewController(activityItems: objectsToShare as! [AnyObject], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = (sender as! UIView)
self.present(activityVC, animated: true, completion: nil)
}
class ActivityForNotesViewController: UIActivityViewController {
internal func _shouldExcludeActivityType(_ activity: UIActivity) -> Bool {
let activityTypesToExclude = [
//insert UIActivity here
]
if let actType = activity.activityType {
if activityTypesToExclude.contains(actType) {
return true
}
else if super.excludedActivityTypes != nil {
return super.excludedActivityTypes!.contains(actType)
}
}
return false
}
}
任何人都可以帮助我,我将不胜感激。
答
我想我可能已经回答了我自己的问题,但如果有人可以仔细检查我会很感激。代码中的注释是我为了使此修复工作而做出的各种更改。适用于模拟器中的iOS 10和真实设备。
@IBAction func share(_ sender: AnyObject) {
// Changed attributedText to text!
let textToShare = detailDescriptionLabel.text!
// Removed Cast to AnyObject
let objectsToShare = [textToShare]
//Removed cast to AnyObject in the Function Call to get rid of error from removing it above
let activityVC = UIActivityViewController(activityItems: objectsToShare , applicationActivities: nil)
//Moved cast for as! UIView outside the perantheses of sender so
//that the as! can be used more efficiently. But most importantly
// I changed the as! to a as? instead thinking that might catch an error and it did... so this works.
activityVC.popoverPresentationController?.sourceView = (sender) as? UIView
self.present(activityVC, animated: true, completion: nil)
}
答
let objectsToShare = [textToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare , applicationActivities: nil)
应该使用它的方式。
为什么要将数组投射到AnyObject。这没有什么意义。 – Andy
@Andy我不太确定为什么我最终真的做到了。由于某种原因,虽然当我从代码中删除该文本编译器抱怨。它确实有意义,并且更有效率地不做演员,但我不是100%关于如何在没有演员的情况下使其工作。 – KSigWyatt
如果我不得不猜测这是因为函数'UIActivityViewController(activityItems:'在此函数的decleration中使用了一个强制转换'objectsToShare as![AnyObject]' – KSigWyatt