gpt4 book ai didi

swift - 从java类将页面加载到webview swift 2中

转载 作者:行者123 更新时间:2023-11-30 11:57:33 24 4
gpt4 key购买 nike

我正在使用 Xwebview 为 iPhone 开发一个应用程序,它使我能够下载页面,然后与下载页面上的 javascript 进行交互。一切正常,但如果互联网连接断开,则会加载默认的本地页面,通知用户没有互联网连接。该页面显示一个重试按钮,按下该按钮会检查互联网连接:如果建立连接,应用程序会尝试再次连接到外部页面并将页面加载到 Web View 中。我无法让它工作:代码下载页面(我可以在 session 数据中看到这一点),但我无法将该页面加载回 WebView 中。

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

func login()
{
// *********** Get stored hashkey **************
let hashcode = getHashcode()

// ********** Check network connection *********
let netConnection = Connection.isConnectedToNetwork()
print("net connection: ", netConnection)

if netConnection == true
{
if hashcode != "00000"
{

print("local key found", hashcode)
// We dont have local key
let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
//webview.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
view.addSubview(webview)
webview.loadPlugin(jsapi(), namespace: "jsapi")


let url:NSURL = NSURL(string: serverLocation + onlineLoginApi)!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)

request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

let paramString = "/?username=username&password=password"
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)

let task = session.downloadTaskWithRequest(request) {
(
let location, let response, let error) in

guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("error")
return
}

let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)

guard let _:NSString = urlContents else {
print("error")
return
}

print(urlContents)

}

task.resume()

// you must tell webview to load response
webview.loadRequest(request)

}
else{

print("local key found", hashcode)
// ********* Found local key go to site pass key over ************

let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
view.addSubview(webview)
webview.loadPlugin(jsapi(), namespace: "jsapi")

let req = NSMutableURLRequest(URL: NSURL(string:serverLocation + onlineLoginApi + "?hashcode=\(hashcode)")!)
req.HTTPMethod = "POST"
req.HTTPBody = "/?hashcode=\(hashcode)".dataUsingEncoding(NSUTF8StringEncoding)
NSURLSession.sharedSession().dataTaskWithRequest(req)
{ data, response, error in
if error != nil
{
//Your HTTP request failed.
print(error!.localizedDescription)
} else {
//Your HTTP request succeeded
print(String(data: data!, encoding: NSUTF8StringEncoding))
}
}.resume()
webview.loadRequest(req)

}

}
else{

// No connection to internet

let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
view.addSubview(webview)
webview.loadPlugin(jsapi(), namespace: "jsapi")

let root = NSBundle.mainBundle().resourceURL!
let url = root.URLByAppendingPathComponent("/www/error-no-connection.html")
webview.loadFileURL(url, allowingReadAccessToURL: root)
print("No internet connection")

}
}
class jsapi: NSObject {


// Reconnect button on interface
func retryConnection()
{
print("Reconnect clicked")
dispatch_async(dispatch_get_main_queue())
{
let netConnections = Connection.isConnectedToNetwork()

if netConnections == true {
let netalert = UIAlertView(title: "Internet on line", message: nil, delegate: nil, cancelButtonTitle: "OK")
netalert.show()

let url = self.serverLocation + self.onlineLoginApi
let hashcode = ViewController().getHashcode()

if(hashcode != "00000") {
let url = url + "?hashcode=\(hashcode)"
print("url: ", url)
}

ViewController().loadPagelive(url)

}


else{
let netalert = UIAlertView(title: "Internet off line", message: nil, delegate: nil, cancelButtonTitle: "OK")
netalert.show()
}
}
print("retryConnect end")
}
}

最佳答案

您尝试在 ViewController 的新实例上执行 loadPagelive(url),而不是在屏幕上显示的当前实例上执行,这就是您不这样做的原因查看任何更新。

您应该创建一个委托(delegate)或一个完成 block ,以便在屏幕上加载的 ViewController 实例上执行代码:每次执行 ViewController() 时,都会创建一个新对象。

您可以尝试使用委托(delegate)模式,实现起来很简单。我将尝试专注于重要部分并创建可以与您现有代码一起使用的东西:

class ViewController: UIViewController {
let jsapi = jsapi() // You can use only 1 instance

override func viewDidLoad() {
super.viewDidLoad()

// Set your ViewController as a delegate, so the jsapi can update it
jsapi.viewController = self
login()
}

func loadPagelive(_ url: URL) {
// Load page, probably you already have it
}
}

class jsapi: NSObject {
weak var viewController: ViewController?

func retryConnection() {
// We check if the delegate is set, otherwise it won't work
guard viewController = viewController else {
print("Error: delegate not available")
}

[... your code ...]

// We call the original (and hopefully unique) instance of ViewController
viewController.loadPagelive(url)
}
}

关于swift - 从java类将页面加载到webview swift 2中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47634315/

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