2016-08-16 3 views
2

Kann jemand UIActivityView in iOS 10 verwenden? Nun, aus irgendeinem Grund in Swift 3.0 wird es kompilieren und bauen, aber wenn die Anwendung nach dem Drücken einer Share-Taste mit dem folgenden Code führt zu einem Absturz der Anwendung ... funktioniert es perfekt in iOS 9.3 und Swift 2.0.UIActivityViewController in iOS 10

Wie in den Kommentaren der Codezeile angegeben 6 oder let objectsToShare = [textToShare] as! AnyObject bewirkt eine Thread1: Signal SIGABRT und Absturz der Anwendung

@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 
    } 
} 

Jeder, der mir helfen kann ich es schätzen würde.

+1

Warum werfen Sie Array zu AnyObject. Das macht nicht viel Sinn. – Andy

+0

@Andy Ich bin mir nicht sicher, warum ich es tatsächlich getan habe. Aus irgendeinem Grund beschwert sich der Compiler, wenn ich diesen Text aus dem Code entferne. es macht Sinn und ist effizienter, die Besetzung nicht zu machen, aber ich bin nicht 100% ig darüber, wie ich es sonst ohne es machen könnte. – KSigWyatt

+0

Wenn ich raten müsste, wäre es, weil die Funktion 'UIActivityViewController (activityItems:' verwendet einen Cast in der Erklärung der Funktion hier 'objectsToShare as! [AnyObject]') – KSigWyatt

Antwort

0

Ich denke, ich habe meine eigene Frage beantwortet, aber wenn jemand überprüfen kann, würde ich es begrüßen. Die Kommentare im Code sind die verschiedenen Änderungen, die ich vorgenommen habe, um diesen Fix zum Laufen zu bringen. Funktioniert in iOS 10 in Simulator und ein echtes Gerät.

@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) 
} 
2
let objectsToShare = [textToShare] 

let activityVC = UIActivityViewController(activityItems: objectsToShare , applicationActivities: nil) 

Sollte die Art und Weise, es zu benutzen.

Verwandte Themen