gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:21:36 27 4
gpt4 key购买 nike

我正在尝试制作 QR 应用程序,但我没有完成最后一步。

今天,当我进行扫描时,我会收到有关结果的警报。我希望将该链接直接发送到我的 webview Controller 并在我的 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!)




}

}

最佳答案

好的,首先看一下这个答案,以编程方式将您的 webView 添加到 View 中(我认为最好从 Storyboard 中的对象库中提取一个): https://stackoverflow.com/a/31651004/4301118

可能有用的简单教程:
https://medium.com/@felicity.johnson.mail/web-view-tutorial-swift-3-0-4a5f4f6858d3

接下来在您的 Storyboard中,拉出一个 UIViewController 并将其链接到 webview.swift 类(以防您还没有这样做)。

按住 Control 键从 ViewController 拖动到您刚刚拉出的新 View Controller ,并为 segue 命名任何您喜欢的名称(我将其称为“webview”)。创建属性 var qrLink: String?qrLink 将用于存储阅读器结果。现在,在 ViewController.swift 的阅读器函数中,将结果放入您刚刚添加的 qrLink 中,并通过添加这些行来触发 segue:

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

像在 ViewController 中所做的那样,在您的 webview 中声明一个属性来存储 QR 链接。

接下来,也在 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!)
}

希望对您有所帮助。

关于ios - 在 Xcode 的另一个 ViewController 中打开 QR 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44632377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com