0

Hi I use this function to create an UIImage of QRCode

func generateQRCode(string: String){
        let data = string.data(using: String.Encoding.ascii)

        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 3, y: 3)

            if let output = filter.outputImage?.transformed(by: transform) {
                imageQRCode.image = UIImage(ciImage: output)
                qrImage = UIImage(ciImage: output)
                self.tableView.reloadData()
            }
        }

    }

After I have generated the image I want to save or print it. I used this function

let shareText = NSLocalizedString("SHARE_QR_TITLE", comment: "")
            if let image = qrImage {
                let vc = UIActivityViewController(activityItems: [shareText, image], applicationActivities: [])
                present(vc, animated: true)
            }

but I can't share it. I received this error: "[ShareSheet] connection invalidate"

RFComp
  • 1
  • 2
  • It's hard to say because you didn't post enough code for me to actually duplicate it. Two thoughts. (1) I routinely use `UIActivityViewController` and your code for that *looks* fine. Could you post more complete code? Maybe try using a static - as opposed to a generated - `UIImage` instead? (2) Are you using an actual device or trying this in the simulator? I've actually never tried sharing something in the simulator. –  Jul 09 '20 at 09:50
  • Check this.. https://stackoverflow.com/a/35931947/6783598 – Ben Rockey Jul 09 '20 at 09:57
  • @Ben Rockey I just see this but not work – RFComp Jul 09 '20 at 10:03
  • @dfd I tried with device and simulator... Same result – RFComp Jul 09 '20 at 10:04
  • https://stackoverflow.com/questions/59433926/error-sharesheet-connection-invalidated-error-ios13-but-not-on-ios-11-4 ? – Larme Jul 09 '20 at 10:08
  • Tried but not works – RFComp Jul 09 '20 at 10:23
  • Last attempt to help. Breaking this down, I'm still looking at `UIActivityViewController` as the issue, because you seem to say that generating a QRCode is working fine. So (1) Could you post your *full* code for the share sheet, but *only* after being able to say that you've tried it with something other than a "generated QRCode" does work? Add *any* UIImage - hardcoded, asset, whatever. Try that with your code. If it doesn't work, post *that* code completely. I'm sure that we can figure it out. –  Jul 10 '20 at 04:46

1 Answers1

0

Try this it's work for me but not showing qrcode by sharing in whatsApp. Working fine with messages, mail, telegram..

import UIKit

class QRCodeGeneratorViewController: UIViewController {
@IBOutlet var qrImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func generateQRCode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.ascii)
    
    
    if let filter = CIFilter(name: "CIQRCodeGenerator") {
        filter.setValue(data, forKey: "inputMessage")
        let transform = CGAffineTransform(scaleX: 3, y: 3)
        
        if let output = filter.outputImage?.transformed(by: transform) {
            return UIImage(ciImage: output)
        }
    }
    
    return nil
}

@IBAction func generateQRAction(_ sender: Any) {
    let image = generateQRCode(from: "iOS Developer")
    qrImageView.image = image
}

@IBAction func btnShareClk(_ sender: Any) {
    let shareText = "Hello, world!"

    if let image = qrImageView.image {
        let vc = UIActivityViewController(activityItems: [shareText, image], applicationActivities: [])
        present(vc, animated: true)
        vc.popoverPresentationController?.sourceView = self.qrImageView
    }
}
Ben Rockey
  • 920
  • 6
  • 23