2017-05-03 4 views
1

로컬 HTML 파일을로드하는 데 문제가 있습니다. 여기 내 코드가있다. 도와주세요.UIWebView에서 로컬 HTML 파일로드

let URL = NSBundle.mainBundle().pathForResource("index2", ofType: "html") 
let request = NSURLRequest(URL: url!) 
Webview.loadRequest(request) 

다음 코드를 사용하십시오. html 파일을로드 할 수 있지만 CSS가로드되지 않습니다!

htmlFile = NSBundle.mainBundle(). pathForResource ("index1", ofType : "html") let html = try? 문자열 (contentsOfFile : htmlFile! 인코딩 : NSUTF8StringEncoding) GSFWebView.loadHTMLString (HTML! base을 : 무기 호)

+0

가능한 복제 (http://stackoverflow.com/questions/7063276 [있는 UIWebView에 로컬 HTML 파일로드 방법]을/how-to-load-local-html-file-into-uiwebview) – Malik

+0

문제에 대한 세부 정보가있는 [편집] 질문. 게시 한 코드는 컴파일되지 않습니다. 이러한 오류로 시작하십시오. – rmaddy

답변

0

은 단순히 당신로 교체, 내가 예를 들어이 페이지의 URL을 추가 한 당신의 viewDidLoad 내에서이 코드를 작성합니다. 스토리 보드에서 아무 것도 할 필요가 없습니다. 컨트롤러에 웹 사이트 컨테이너를 추가 한 상태에서 추가보기를 추가 한 경우 내 코드의 self.view을보기로 바꿉니다.

let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) 
    //add your url on line below 
    myWebView.loadRequest(URLRequest(url: URL(string: "https://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift")!)) 
    self.view.addSubview(myWebView) 

로컬 파일에서 페이지를로드하려면이 방법으로 URL을 정의하면됩니다.

스위프트 2

let url = NSBundle.mainBundle().URLForResource("webFileName", withExtension:"html") 

스위프트 3

let url = Bundle.main.url(forResource: "webFileName", withExtension: "html") 
관련 문제