You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程方式打开系统二维码扫描应用?

iOS 打开二维码扫描应用的方案

一、系统自带扫码功能的直接实现(推荐)

iOS系统自带扫码能力,无需依赖第三方应用的URL Scheme,通过AVFoundation框架即可直接集成,不受第三方应用安装情况限制。核心代码示例:

import AVFoundation

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupCaptureSession()
    }

    private func setupCaptureSession() {
        captureSession = AVCaptureSession()
        guard let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { return }
        
        let videoInput: AVCaptureDeviceInput
        do {
            videoInput = try AVCaptureDeviceInput(device: videoDevice)
        } catch {
            showUnavailableAlert()
            return
        }

        guard captureSession.canAddInput(videoInput) else {
            showUnavailableAlert()
            return
        }
        captureSession.addInput(videoInput)

        let metadataOutput = AVCaptureMetadataOutput()
        guard captureSession.canAddOutput(metadataOutput) else {
            showUnavailableAlert()
            return
        }
        captureSession.addOutput(metadataOutput)
        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        metadataOutput.metadataObjectTypes = [.qr]

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = view.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.addSublayer(previewLayer)
        
        captureSession.startRunning()
    }

    private func showUnavailableAlert() {
        let alert = UIAlertController(title: "扫码不可用", message: "当前设备不支持扫码功能,请更换设备或使用其他方式。", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true)
        captureSession = nil
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        captureSession.stopRunning()
        guard let metadata = metadataObjects.first as? AVMetadataMachineReadableCodeObject,
              let scanResult = metadata.stringValue else { return }
        
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        // 这里处理扫码得到的结果scanResult
        print(scanResult)
        dismiss(animated: true)
    }
}

二、第三方扫码应用的URL Scheme调用

若需唤起第三方扫码应用,不同应用的URL Scheme存在差异,且部分应用未公开Scheme或需配置白名单。以下是部分常见应用的已知Scheme:

  • 微信:weixin://scanqrcode
  • 支付宝:alipay://platformapi/startapp?saId=10000007
  • QQ:mqq://scanqrcode

调用示例:

if let scanUrl = URL(string: "weixin://scanqrcode"), UIApplication.shared.canOpenURL(scanUrl) {
    UIApplication.shared.open(scanUrl)
} else {
    // 应用未安装或Scheme不可用的处理逻辑
}

注意:需在Info.plist中添加LSApplicationQueriesSchemes数组,将需要调用的Scheme加入其中,否则canOpenURL会返回false。

三、其他替代方式

可使用UIActivityViewController提供分享选项,让用户手动选择扫码应用处理,但该方式无法直接唤起扫码界面,仅能提供选择入口。

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 12:27:12