2014-10-30 6 views
49

का उपयोग करके UIWebView में स्थानीय HTML लोड करें यह एक सुबह मुझे पागल कर रहा है। मैं एक वेब दृश्य में कुछ स्थानीय एचटीएमएल लोड करना चाहते हैं:स्विफ्ट

class PrivacyController: UIViewController { 

    @IBOutlet weak var webView:UIWebView! 

    override func viewDidLoad() { 
     let url = NSURL(fileURLWithPath: "privacy.html") 
     let request = NSURLRequest(URL: url!) 
     webView.loadRequest(request) 
    } 
} 

html फ़ाइल अपने प्रोजेक्ट की जड़ फ़ोल्डर में स्थित लेकिन एक समूह के अंदर है। वेबव्यू मेरे लिए खाली है। कोई विचार क्या गलत है? मैं xcode 6.1 पर हूं और अपने आईफोन 6 पर इस उदाहरण को चला रहा हूं।

+0

यह आपको मदद मिल सकती है http://stackoverflow.com/questions/24591926/is-this-a-swift-bug-opening-a-file-in -ए-वेबव्यू – Jhondoe

उत्तर

54

एप्लिकेशन संसाधनों के लिए यूआरएल पुनर्प्राप्त करने के लिए, आपको URLForResourceNSBundle कक्षा का उपयोग करना चाहिए।

स्विफ्ट 2

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

स्विफ्ट 3

let url = Bundle.main.url(forResource: "privacy", withExtension: "html") 
+0

आकर्षक बिंदु यह है कि इसे "गोपनीयता" होना चाहिए और "privacy.html" नहीं होना चाहिए। यही कारण है कि यह मेरे लिए काम नहीं कर रहा था। –

6

स्थानीय html फ़ाइल आपके प्रोजेक्ट नाम में जोड़े यह home.html के रूप में, तो NSURLRequest NSURL ऑब्जेक्ट का उपयोग बनाएँगे, तब वेबव्यू के अनुरोध को पास करने से यह अनुरोधित यूआरएल को वेबव्यू में लोड करेगा और यदि आप स्टोरीबोर्ड का उपयोग नहीं कर रहे हैं तो नीचे कोड की तरह व्यू कंट्रोलर व्यू में uiwebview जोड़ें।

override func viewDidLoad() { 
        super.viewDidLoad() 
        // Do any additional setup after loading the view, typically from a nib. 
        let localfilePath = NSBundle.mainBundle().URLForResource("home", withExtension: "html"); 
        let myRequest = NSURLRequest(URL: localfilePath!); 
        myWebView.loadRequest(myRequest); 
     self.view.addSubview(myWebView) 
    } 

अधिक संदर्भ के लिए इस http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/

15
// Point UIWebView 
@IBOutlet weak var webView: UIWebView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //load a file 
    var testHTML = NSBundle.mainBundle().pathForResource("privacy", ofType: "html") 
    var contents = NSString(contentsOfFile: testHTML!, encoding: NSUTF8StringEncoding, error: nil) 
    var baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file 

    webView.loadHTMLString(contents, baseURL: baseUrl) 

} 
6

स्विफ्ट संस्करण देखें 2.1

इस मामले भी एन्कोडिंग

// load HTML String with Encoding 
let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html") 
do { 
    let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding) 
    webView.loadHTMLString(fileHtml as String, baseURL: nil) 
} 
catch { 

} 
4

यह मेरे लिए काम किया है में शामिल हैं:

@IBOutlet weak var mWebView: UIWebView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    mWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("fineName", ofType: "html")!))) 

} 

जोड़ा गया App Transport Security SettingsInfo.plist फ़ाइल में Dictionary प्रकार के साथ। ऐप ट्रांसपोर्ट सुरक्षा सेटिंग्स टाइप Boolean और YES के साथ उप कुंजी Allow Arbitrary Loads भी जोड़ा गया।

Here ट्यूटोरियल है।

संपादित

स्विफ्ट 3 के लिए (Xcode 8)

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!))) 
1

आप UIWebView में एचटीएमएल स्ट्रिंग या स्थानीय html फ़ाइल लोड कर सकते हैं।

एचटीएमएल स्ट्रिंग:

func loadHtmlCode() { 
    let htmlCode = "<html><head><title>Wonderful web</title></head> <body><p>wonderful web. loading html code in <strong>UIWebView</strong></></body>" 
    webView.loadHTMLString(htmlCode, baseURL: nil) 
} 

HTML फ़ाइल:

func loadHtmlFile() { 
    let url = NSBundle.mainBundle().URLForResource("contactus", withExtension:"html") 
    let request = NSURLRequest(URL: url!) 
    webView.loadRequest(request) 
} 

विवरण यहां पाया जा सकता: http://webindream.com/load-html-uiwebview-using-swift/

+0

loadHtmlCode() क्योंकि मैं कोशिश की, लेकिन छवि खाली दिखा @farhad रुबेल –

+0

मैं इस जाने htmlCode = " अद्भुत वेब<शरीर bgcolor = नारंगी>

अद्भुत वेब। लोड हो रहा है एचटीएमएल कोड की तरह करने की कोशिश की है में छवि दिखाने के लिए कैसे UIWebView

" –

0

यह मेरे लिए काम किया (Xcode 8, स्विफ्ट 3)

@IBOutlet कमजोर var webViewer: UIWebView!

override func viewDidLoad() { 
    super.viewDidLoad() 

    let localfilePath = Bundle.main.url(forResource: "homeInfo", withExtension: "html"); 
    let myRequest = NSURLRequest(url: localfilePath!); 
    webViewer.loadRequest(myRequest as URLRequest); 
    self.view.addSubview(webViewer) 
} 
0

तेज 3 उपयोग के लिए यह:

do 
    { 
     let testHTML = Bundle.main.path(forResource: "about", ofType: "html") 
     let contents = try NSString(contentsOfFile: testHTML!, encoding: String.Encoding.utf8.rawValue) 
     let baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file 

     mWebView.loadHTMLString(contents as String, baseURL: baseUrl as URL) 
    } 
    catch 
    { 

    } 
10

स्विफ्ट 3: प्रकार सुरक्षित

@IBOutlet weak var webView: UIWebView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Adding webView content 
    do { 
     guard let filePath = Bundle.main.path(forResource: "myFile", ofType: "html") 
      else { 
       // File Error 
       print ("File reading error") 
       return 
     } 

     let contents = try String(contentsOfFile: filePath, encoding: .utf8) 
     let baseUrl = URL(fileURLWithPath: filePath) 
     webView.loadHTMLString(contents as String, baseURL: baseUrl) 
    } 
    catch { 
     print ("File HTML error") 
    } 
} 

ध्यान रखें: एन एस = स्विफ्ट नहीं:]

+0

धन्यवाद आप @Peter Kreinz – iLandes

+0

इस जवाब स्वीकार किया जाना चाहिए, एक आकर्षण की तरह काम करता है! – godblessstrawberry

3

स्विफ्ट के साथ 3 लाइनों :)

if let url = Bundle.main.url(forResource: "privacy", withExtension: "html") { 
    webview.loadRequest(URLRequest(url: url)) 
}