iOS 10中的UIActivityViewController
有人知道如何在iOS 10中使用UIActivityView吗?由于某些原因,在Swift 3.0中,它将编译和构建,但当应用程序在按下共享按钮后运行时,使用以下代码会导致应用程序崩溃。。。它在iOS 9.3和Swift 2.0中运行得非常好 如代码行6的注释中所指定,或iOS 10中的UIActivityViewController,ios,swift,uiactivityviewcontroller,ios10,Ios,Swift,Uiactivityviewcontroller,Ios10,有人知道如何在iOS 10中使用UIActivityView吗?由于某些原因,在Swift 3.0中,它将编译和构建,但当应用程序在按下共享按钮后运行时,使用以下代码会导致应用程序崩溃。。。它在iOS 9.3和Swift 2.0中运行得非常好 如代码行6的注释中所指定,或将objectsToShare=[textToShare]设为!任何对象都会导致Thread1:信号SIGABRT和应用程序崩溃 @IBOutlet weak var detailDescriptionLabel: UIText
将objectsToShare=[textToShare]设为!任何对象
都会导致Thread1:信号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)
}
应该是使用它的方法。为什么要将数组强制转换为任何对象。“这没什么意义。”安迪,我不太清楚为什么我最终真的这么做了。但出于某种原因,当我从代码中删除该文本时,编译器会抱怨。不进行强制转换是有意义的,而且效率更高,但我不知道没有强制转换如何工作。如果我不得不猜测,这可能是因为函数
UIActivityViewController(activityItems:
在这里使用强制转换来取消对函数的限制objectsToShare as![AnyObject]
let objectsToShare = [textToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare , applicationActivities: nil)