在UIwebview swift iOS中加载qr代码链接

时间:2016-05-24 13:43:37

标签: ios xcode swift uiwebview qr-code

大家好我有一个webview VViewController.swift 和 QRCode扫描仪 QRRViewController.swift 如果它的网址是直接而非徒步旅行的网址,如何加载QR码?

VVCiewcontroller.swift

  import UIKit
class VViewController: UIViewController {



 @IBOutlet weak var WWebView: UIWebView!

 var QRLink:String!



 override func viewDidLoad() {
    super.viewDidLoad()



    var url = NSURL(string: QRLink)
    var request = NSURLRequest(URL: url!)

    WWebView.loadRequest(request)
  }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }
@IBAction func goBack(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
  }
}

QRRViewController.swift喜欢这个 此代码将Qr代码URL加载到safari而不是Web视图

.......

if metadataObj.type == AVMetadataObjectTypeQRCode {
let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject

qrCodeFrameView?.frame = barCodeObject.bounds;

if metadataObj.stringValue != nil {
  messageLabel.text = metadataObj.stringValue
    //if the result is url...
    UIApplication.sharedApplication().openURL(NSURL(string: metadataObj.stringValue)!)

  }
}

2 个答案:

答案 0 :(得分:0)

假设您从QR视图控制器移动到Web视图视图控制器,请使用prepare for segue方法将链接传递到下一个视图。

将其添加到QR视图控制器

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    // Create a variable that you want to send
    var QRLink = self.QRLink

    // Create a new variable to store the instance of PlayerTableViewController
    let destinationVC = segue.destinationViewController as VViewController
    destinationVC.QRLink = QRLink
}

别忘了,您还需要在webviews控制器中添加变量来存储QR链接。

VVCiewcontroller.swift

  import UIKit
class VViewController: UIViewController {

private var QRLink: String

 @IBOutlet weak var WWebView: UIWebView!

 var QRLink:String!



 override func viewDidLoad() {
    super.viewDidLoad()



    var url = NSURL(string: QRLink)
    var request = NSURLRequest(URL: url!)

    WWebView.loadRequest(request)
  }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }
@IBAction func goBack(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
  }
}

答案 1 :(得分:0)

我假设您能够从qr代码阅读器功能中检索字符串, ,还有一个webview,所以试试这个:

        let request = NSURLRequest(URL: "THE URL FROM QRCode")
        wvScreen.loadRequest(request)