2016-03-11 10 views
52

Ich begann meine Suche mit dem Wunsch zu wissen, wie ich mit anderen Apps in iOS teilen könnte. Ich entdeckte, dass zwei wichtige WegeGrundlegendes Beispiel für die Freigabe von Text oder Bild mit UIActivityViewController in Swift

  • UIActivityViewController
  • UIDocumentInteractionController

Diese und andere Verfahren sind im Vergleich in this SO answer sind.

Wenn ich ein neues Konzept lerne, sehe ich oft ein einfaches Beispiel, um mich zu beginnen. Sobald ich etwas Grundlegendes eingerichtet habe, kann ich es modifizieren, wie es mir später gefällt.

Es gibt viele SO Fragen im Zusammenhang mit UIActivityViewController, aber ich konnte keine finden, die nur für ein einfaches Beispiel bitten. Da ich gerade gelernt habe, wie das geht, werde ich unten meine eigene Antwort geben. Fühlen Sie sich frei, eine bessere (oder eine Objective-C-Version) hinzuzufügen.

+0

https://iosdevcenters.blogspot.com/2017/08/how-to-share-content-with.html –

Antwort

163

UIActivityViewController Beispielprojekt

Storyboard mit zwei Tasten einrichten und sie zu Ihren View-Controller anschließen (siehe Code unten).

enter image description here

ein Bild zu Ihrem Assets.xcassets hinzufügen. Ich habe meinen "Löwen" genannt.

enter image description here

-Code

import UIKit 
class ViewController: UIViewController { 

    // share text 
    @IBAction func shareTextButton(_ sender: UIButton) { 

     // text to share 
     let text = "This is some text that I want to share." 

     // set up activity view controller 
     let textToShare = [ text ] 
     let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil) 
     activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash 

     // exclude some activity types from the list (optional) 
     activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ] 

     // present the view controller 
     self.present(activityViewController, animated: true, completion: nil) 

    } 

    // share image 
    @IBAction func shareImageButton(_ sender: UIButton) { 

     // image to share 
     let image = UIImage(named: "Image") 

     // set up activity view controller 
     let imageToShare = [ image! ] 
     let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil) 
     activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash 

     // exclude some activity types from the list (optional) 
     activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ] 

     // present the view controller 
     self.present(activityViewController, animated: true, completion: nil) 
    } 

} 

Ergebnis

einem Klick gibt auf der linken Ergebnis "einen Text teilen" und rechts "Teile ein Bild" gibt das Ergebnis klicken.

enter image description here

Hinweise

  • ich erneut getestet dies mit iOS 11 und Swift 4. Ich hatte es ein paar Mal im Simulator laufen, bevor es funktionierte, weil es wurde eine Zeitüberschreitung. Dies kann daran liegen, dass mein Computer langsam ist.
  • Wenn Sie einige dieser Optionen ausblenden möchten, können Sie dies mit excludedActivityTypes tun, wie im obigen Code gezeigt.
  • Ohne die Zeile popoverPresentationController?.sourceView wird Ihre App beim Ausführen auf einem iPad abstürzen.
  • Dadurch können Sie keinen Text oder Bilder mit anderen Apps teilen. Sie wollen wahrscheinlich UIDocumentInteractionController dafür.

Siehe auch

+1

Warum einige Beispiele Array von 1 Artikel zeigen, und einige Shows 2? Angenommen, ein Bild wird geteilt. –

5

Nur als Anmerkung Sie können dies auch für iPads verwenden:

activityViewController.popoverPresentationController?.sourceView = sender 

So die popover Pops vom Sender (die Schaltfläche in diesem Fall).

+0

activityViewController.popoverPresentationController? .sourceView = Absender als? UIView - für mich bearbeitet. :) Vielen Dank – Brian

3

Ich fand das einwandfrei funktioniert, wenn Sie den gesamten Bildschirm teilen möchten.

@IBAction func shareButton(_ sender: Any) { 

    let bounds = UIScreen.main.bounds 
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0) 
    self.view.drawHierarchy(in: bounds, afterScreenUpdates: false) 
    let img = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil) 
    activityViewController.popoverPresentationController?.sourceView = self.view 
    self.present(activityViewController, animated: true, completion: nil) 
} 
1

Sie können die folgenden Funktionen verwenden, die ich in einer meiner Hilfsklassen in einem Projekt geschrieben habe.

rufen Sie einfach

showShareActivity(msg:"message", image: nil, url: nil, sourceRect: nil) 

und es wird sowohl für iPhone und iPad arbeiten. Wenn Sie den CGRect-Wert einer Ansicht durch sourceRect übergeben, wird auch ein kleiner Pfeil auf dem iPad angezeigt.

func topViewController()-> UIViewController{ 
    var topViewController:UIViewController = UIApplication.shared.keyWindow!.rootViewController! 

    while ((topViewController.presentedViewController) != nil) { 
     topViewController = topViewController.presentedViewController!; 
    } 

    return topViewController 
} 

func showShareActivity(msg:String?, image:UIImage?, url:String?, sourceRect:CGRect?){ 
    var objectsToShare = [AnyObject]() 

    if let url = url { 
     objectsToShare = [url as AnyObject] 
    } 

    if let image = image { 
     objectsToShare = [image as AnyObject] 
    } 

    if let msg = msg { 
     objectsToShare = [msg as AnyObject] 
    } 

    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 
    activityVC.modalPresentationStyle = .popover 
    activityVC.popoverPresentationController?.sourceView = topViewController().view 
    if let sourceRect = sourceRect { 
     activityVC.popoverPresentationController?.sourceRect = sourceRect 
    } 

    topViewController().present(activityVC, animated: true, completion: nil) 
} 
0

Teilen: Text

@IBAction func shareOnlyText(_ sender: UIButton) { 
    let text = "This is the text....." 
    let textShare = [ text ] 
    let activityViewController = UIActivityViewController(activityItems: textShare , applicationActivities: nil) 
    activityViewController.popoverPresentationController?.sourceView = self.view 
    self.present(activityViewController, animated: true, completion: nil) 
} 
} 

Teilen: Bild

@IBAction func shareOnlyImage(_ sender: UIButton) { 
    let image = UIImage(named: "Product") 
    let imageShare = [ image! ] 
    let activityViewController = UIActivityViewController(activityItems: imageShare , applicationActivities: nil) 
    activityViewController.popoverPresentationController?.sourceView = self.view 
    self.present(activityViewController, animated: true, completion: nil) 
} 

Teilen: Text - Bild - URL

@IBAction func shareAll(_ sender: UIButton) { 
    let text = "This is the text...." 
    let image = UIImage(named: "Product") 
    let myWebsite = NSURL(string:"https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile") 
    let shareAll= [text , image! , myWebsite] 
    let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil) 
    activityViewController.popoverPresentationController?.sourceView = self.view 
    self.present(activityViewController, animated: true, completion: nil) 
    } 

enter image description here

Verwandte Themen