WKWebview如何处理上传图片?
How to Handle Image uploading in WKWebview?
我已经使用 webkit 在我的应用程序上显示 url。
当我点击图像按钮( 中的绿色按钮)时,它显示
相机或照片库。
问题是,当单击 Use photo 时,webkit
自动刷新而不是显示在页面上
- 我已经在plist文件中赋予照片使用、媒体、相机权限
我的密码是
import UIKit
import WebKit
import Alamofire
class ContinueViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var activityView: UIActivityIndicatorView!
@IBOutlet weak var webview: WKWebView!
func getPostString(params:[String:Any]) -> String{
var data = [String]()
for(key, value) in params
{
data.append(key + "=\(value)")
}
return data.map { String([=10=]) }.joined(separator: "&")
}
var url: URL?
var traderCategoryId = 0, tradeSkillId = 0
override func viewDidLoad() {
super.viewDidLoad()
webview.navigationDelegate = self
self.navigationController?.navigationBar.isHidden = false
webview.uiDelegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activityView.startAnimating()
guard let url = self.url else { return }
var req = URLRequest(url: url)
let params = ["id" : id,"trader_skills" : tradeSkillId]
let postString = self.getPostString(params: params)
req.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
req.httpMethod = "POST"
req.httpBody = postString.data(using: .utf8)
self.webview.load(req)
}
}
extension ContinueViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityView.stopAnimating()
}
}
extension UIImagePickerController {
open override func viewDidLoad() {
super.viewDidLoad()
// self.modalPresentationStyle = .fullScreen
}
}
issue is, when click Use photo, the webkit refresh automatically instead showing on page
因为你self.webview.load(req)
在
override func viewWillAppear(_ animated: Bool) {
显示相机或照片库,然后return到此页面
func viewWillAppear
又打来电话
self.webview.load(req)
再次打来电话
简单点,
将self.webview.load(req)
相关内容放入
override func viewDidLoad() {
我已经使用 webkit 在我的应用程序上显示 url。
当我点击图像按钮(
- 我已经在plist文件中赋予照片使用、媒体、相机权限
我的密码是
import UIKit
import WebKit
import Alamofire
class ContinueViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var activityView: UIActivityIndicatorView!
@IBOutlet weak var webview: WKWebView!
func getPostString(params:[String:Any]) -> String{
var data = [String]()
for(key, value) in params
{
data.append(key + "=\(value)")
}
return data.map { String([=10=]) }.joined(separator: "&")
}
var url: URL?
var traderCategoryId = 0, tradeSkillId = 0
override func viewDidLoad() {
super.viewDidLoad()
webview.navigationDelegate = self
self.navigationController?.navigationBar.isHidden = false
webview.uiDelegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activityView.startAnimating()
guard let url = self.url else { return }
var req = URLRequest(url: url)
let params = ["id" : id,"trader_skills" : tradeSkillId]
let postString = self.getPostString(params: params)
req.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
req.httpMethod = "POST"
req.httpBody = postString.data(using: .utf8)
self.webview.load(req)
}
}
extension ContinueViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityView.stopAnimating()
}
}
extension UIImagePickerController {
open override func viewDidLoad() {
super.viewDidLoad()
// self.modalPresentationStyle = .fullScreen
}
}
issue is, when click Use photo, the webkit refresh automatically instead showing on page
因为你self.webview.load(req)
在
override func viewWillAppear(_ animated: Bool) {
显示相机或照片库,然后return到此页面
func viewWillAppear
又打来电话
self.webview.load(req)
再次打来电话
简单点,
将self.webview.load(req)
相关内容放入
override func viewDidLoad() {