Swift UIActivityViewController Image Share not working

前端 未结 3 1479
北荒
北荒 2021-02-13 19:23

I am using UIActivityViewController to share image. After the WhatsApp recent changes to allow share i am able to see WhatsApp in the share option. I am sharing an

3条回答
  •  独厮守ぢ
    2021-02-13 20:03

    To share Text || Image try this Swift.

    func share(shareText shareText:String?,shareImage:UIImage?){
    
        var objectsToShare = [AnyObject]()
    
        if let shareTextObj = shareText{
            objectsToShare.append(shareTextObj)
        }
    
        if let shareImageObj = shareImage{
            objectsToShare.append(shareImageObj)
        }
    
        if shareText != nil || shareImage != nil{
            let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view
    
            presentViewController(activityViewController, animated: true, completion: nil)
        }else{
            print("There is nothing to share")
        }
    }
    

    To share just call like this:

    let imageToShare = UIImage(named: "myImage")
    share(shareText: "Sharing this text", shareImage: imageToShare)
    

提交回复
热议问题