在我的iOS
应用程序中,我正在生成一个二维码,其中的基本功能只是将用户移动到应用程序的一个功能中。
我想在同样的二维码中包含一个深度链接URL。
用例将是一个拥有应用程序的人向另一个人显示二维码。该用户将打开他们的本地相机应用程序或选择的二维码扫描器&它会找到这个URL,并被导航到Safari / app Store。
下面是我创建二维码的方法:
func generateQRCode(from string: String) -> UIImage? {
var jsonDict = [String: Any]()
let url = "https://mydeeplinkurlhere"
jsonDict.updateValue(url, forKey: "url")
jsonDict.updateValue(string, forKey: "xyz")
guard let jsonData = try? JSONSerialization.data(withJSONObject: jsonDict, options: [.prettyPrinted]) else {
return nil
}
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
// Input the data
qrFilter.setValue(jsonData, forKey: "inputMessage")
// Get the output image
guard let qrImage = qrFilter.outputImage else { return nil}
// Scale the image
let transform = CGAffineTransform(scaleX: 12, y: 12)
let scaledQrImage = qrImage.transformed(by: transform)
// Do some processing to get the UIImage
let context = CIContext()
guard let cgImage = context.createCGImage(scaledQrImage, from: scaledQrImage.extent) else { return nil }
let processedImage = UIImage(cgImage: cgImage)
return processedImage
}
更新:
上面的代码让我更接近我想要的结果。
如果在应用程序中扫描二维码,我可以将其拆分,并使用我需要的。
我现在的问题是:
如果用户使用他们的相机应用程序进行扫描,它会显示出JSON是什么。
因此,它们将同时看到这两个键值对。我不想让他们看到这一切。在这种情况下,我只需要它们来查看URL和/或自定义消息。
该部分可以编辑吗?也就是说,扫描二维码时会显示什么?
或者,网站可以自动打开而不是看到通知吗?
发布于 2020-10-16 04:06:46
你可以在二维码中存储任何类型的对象,在你的情况下,你可以在你的二维码中按字典或数组类型发送几个信息,这是代码:
func generateQRCode(from dictionary: [String: String]) -> UIImage? {
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: false) else { return nil }
qrFilter.setValue(data, forKey: "inputMessage")
guard let qrImage = qrFilter.outputImage else { return nil}
let transform = CGAffineTransform(scaleX: 10, y: 10)
let scaledQrImage = qrImage.transformed(by: transform)
let context = CIContext()
guard let cgImage = context.createCGImage(scaledQrImage, from: scaledQrImage.extent) else { return nil }
let processedImage = UIImage(cgImage: cgImage)
return processedImage
}
这是你的模型,你如何调用这个方法:
let dictionary: [String: String] = [
"message" : "some message string here",
"link" : "https://google.com"
]
YOUR_IMAGE_VIEW.image = generateQRCode(from: dictionary)
您可以完全自由地更改数据模型,但扫描二维码则是另一回事,请查看:https://www.hackingwithswift.com/example-code/media/how-to-scan-a-qr-code
我希望这将是有用的,快乐的编码?
https://stackoverflow.com/questions/64373972
复制相似问题