나는 Xwebview
을 사용하여 iPhone 용 앱을 개발하고 있으며, 다운로드 한 페이지에서 자바 스크립트와 상호 작용하는 페이지를 다운로드 할 수 있습니다. 모두 작동하지만 인터넷 연결이 끊어지면 사용자에게 인터넷에 연결되어 있지 않음을 알리는 기본 로컬 페이지가로드됩니다. 이 페이지는 인터넷 연결을 누른 경우 인터넷 연결을 확인하는 재시도 단추를 표시합니다. 연결되면 응용 프로그램이 외부 페이지에 다시 연결하여 페이지를 웹보기에로드하려고 시도합니다. 이 코드는 작동하지 않습니다. 코드가 페이지를 다운로드하지만 (세션 데이터에서 볼 수 있음) 웹 페이지로 다시로드 할 수 없습니다. 당신이 어떤 업데이 트가 표시되지 않는 이유자바 클래스에서 webview swift 2로 페이지로드
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")
}
}
정보를 추가 할 수 있습니까? 버튼 누르기에서 요청까지 어떤 부분이 작동하지 않습니까? 무엇을 수정하고 디버그하려고 했습니까? 우리가 당신을 도울 수 없다면, 어떤 코드를 추가하는 것을 고려해보십시오. –
안녕하세요, 죄송합니다. 내 첫 번째 게시물입니다. 코드를 추가했습니다. 자바 스크립트가 연결을 확인한 다음 URL을 만들고 앱을 다시 실행하려고하는 것을 볼 수 있습니다. 사용자가 이미 사이트에 등록 된 경우 사용할 수있는 저장된 해시 코드가 있습니다. 인터넷 연결을 확인한 후 ViewController(). loadPagelive (url) 페이지를로드하려고하지만 작동하지 않습니다. –
아무 문제가 없습니다. ,하지만 질문을 작성하는 방법에 대한 StackOverflow 자습서를 읽는 것이 좋습니다. 그렇지 않으면 향후 게시물이 닫힐 수 있습니다. 코드를 보면 잘못 될 수있는 여러 가지 사항이 있습니다. 다시 시도 버튼을 누르면 콘솔에 표시됩니까? 어쨌든, 현재의 ViewController가 아닌 ViewController의 새로운 인스턴스에서 loadPageLive를 수행하려고 시도합니다. 그 이유는 업데이트가 표시되지 않는 이유입니다. –