iOS通过代码制作WIFI二维码,实现手机扫码加入WIFI
1.获取二维码内容
如何获取当前连接WIFI的相关信息请跳转
☞iOS获取手机当前连接的WIFI信息
static func getQRCodeText(ssid: String, password: String) -> String {
return "WIFI:T:WPA;S:\(ssid);P:\(password);;"
}
2.代码可以通过以下方法实现详见☞字符串生成二维码,也可以拷贝二维码内容使用草料二维码生成二维码;
static func createQRImage(with qrCodeStr: String, size: CGFloat) -> UIImage? {
let strData = qrCodeStr.data(using: .utf8, allowLossyConversion: false)
// 创建一个二维码的滤镜
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
qrFilter.setValue(strData, forKey: "inputMessage")
qrFilter.setValue(size <= 150 ? "L" : "H", forKey: "inputCorrectionLevel")
let qrCIImage = qrFilter.outputImage
// 创建一个颜色滤镜,黑白色
guard let colorFilter = CIFilter(name: "CIFalseColor") else { return nil }
colorFilter.setDefaults()
colorFilter.setValue(qrCIImage, forKey: "inputImage")
colorFilter.setValue(CIColor.black, forKey: "inputColor0")
colorFilter.setValue(CIColor.white, forKey: "inputColor1")
guard let outputImage = colorFilter.outputImage else { return nil }
let scale = size / outputImage.extent.size.width
let image_tr = outputImage.transformed(by: CGAffineTransform(scaleX: scale, y: scale))
let qrImage = UIImage(ciImage: image_tr)
return qrImage
}
3.把二维码分享出去,拿出手机扫一扫加入即可。