在Xcode中的另一个ViewController中打开QR链接

时间:2017-06-19 13:48:15

标签: ios swift xcode qr-code

我试图制作QR应用程序,但我没有完成最后一步的工作。

今天,当我进行扫描时,我会收到结果警报。我希望将该链接直接发送到我的webview控制器,并在我的webView中读取地址,而不是显示警报。

ViewController.swift

func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
    reader.stopScanning()



    dismiss(animated: true) { [weak self] in
      let alert = UIAlertController(
        title: "Hittade",
        message: String (format:"%@", result.value, result.metadataType),
        preferredStyle: .alert


      )


      alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))

      self?.present(alert, animated: true, completion: nil)
    }
  }

webview.swift

 @IBOutlet var containerView: UIView? = nil
    var webView: WKWebView?

    override func viewDidLoad() {
        super.viewDidLoad()

  let myURL = URL(string: "MY RESULT URL HERE")
        let myRequest = URLRequest(url: myURL!)
        _ = webView?.load(myRequest)



    }

修改 感谢@sharon的帮助!

到目前为止我已经这样做了。 但是,有一些错误消息

请参阅下面的链接图片

webview.swift

ViewController.swift

ViewController.swift

 func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
    reader.stopScanning()



    dismiss(animated: true) { [weak self] in
      let alert = UIAlertController(
        title: "Hittade",
        message: String (format:"%@", result.value, result.metadataType),
        preferredStyle: .alert

      )
        var qrLink: String?
        qrLink = "\(result.value)\(result.metadataType)"
        performSegue(withIdentifier: "WebView", sender: self)

      alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))

      self?.present(alert, animated: true, completion: nil)
    }
  }

    func prepare(for segue: UIStoryboardSegue, sender: Any?){
        if segue.identifier == "WebView" {
            let webView = segue.destination as! webview
            webView.qrLink = "%@"
        }}

  func reader(_ reader: QRCodeReaderViewController, didSwitchCamera newCaptureDevice: AVCaptureDeviceInput) {
    if let cameraName = newCaptureDevice.device.localizedName {
      print("Switching capturing to: \(cameraName)")
    }
  }

  func readerDidCancel(_ reader: QRCodeReaderViewController) {
    reader.stopScanning()

    dismiss(animated: true, completion: nil)
  }

}

webview.swift

import AVFoundation
import UIKit
import WebKit

class WebView: UIViewController, QRCodeReaderViewControllerDelegate {




    @IBOutlet var containerView: UIView? = nil
    var webView: WKWebView?

    override func viewDidLoad() {
        super.viewDidLoad()



        setNeedsStatusBarAppearanceUpdate()

        view.backgroundColor = UIColor(red: 109/255, green: 167/255, blue: 215/255, alpha: 1)

        var qrLink: String?
        var webView: UIWebView?


            super.viewDidLoad()
            let myURL = URL(string: qrLink!)
            let myRequest = URLRequest(url: myURL!)
            webView = UIWebView(frame: CGRect(x: 0, y: 0, width:
                UIScreen.main.bounds.width, height:
                UIScreen.main.bounds.height))
            webView?.loadRequest(myRequest)
            view.addSubview(webView!)




        }

    }

1 个答案:

答案 0 :(得分:2)

Ok, first take a look at this answer to add your webView to the view programmatically (I think it's best practice to pull one from the object library in the storyboard): https://stackoverflow.com/a/31651004/4301118

A simple tutorial that might help:
https://medium.com/@felicity.johnson.mail/web-view-tutorial-swift-3-0-4a5f4f6858d3

Next in your storyboard, pull a UIViewController and link it to the webview.swift class (in case you haven't done it already).

Control-drag from the ViewController to the new one you just pulled and name the segue anything you like (I'll refer it as "webview"). Create a property var qrLink: String? qrLink will use to store the reader result. Now in the reader function at ViewController.swift place the result in the qrLink you just added and trigger the segue by adding those lines:

qrLink = "\(result.value)\(result.metadataType)"
performSegue(withIdentifier: "webview", sender: self)

Declare a property to store the QR link in your webview as you did in the ViewController.

Next, add this function also in ViewController.swift:

func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == "webview" {
        let webView = segue.destination as! webview
        webView.qrLink = "YOUR READER RESULT"
}}

webview.swift

var qrLink: String?
var webView: UIWebView?

override func viewDidLoad(){
    super.viewDidLoad()
    let myURL = URL(string: qrLink!)
    let myRequest = URLRequest(url: myURL!)
    webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 
        UIScreen.main.bounds.width, height: 
        UIScreen.main.bounds.height))
    webView?.loadRequest(myRequest)
    view.addSubview(webView!)
}

Hope it helped.