Application of WebView in Swift

Source: Internet
Author: User

WebView control is to do the Web application development of the most used control, directly within the WebView to specify a page address to access the Web page, but also to implement the Uiwebviewdelegate protocol to implement the corresponding method to control the loading and processing of content. The famous PhoneGap developed the application, its shell important component is also the WebView control.

Add the following code within the Viewdidload method to complete a simple Web page

var WebView = UIWebView (frame:cgrectmake (0, 0, 600, 600))

var url = nsurl (string: "http://www.imoneyfans.com")

Webview.loadrequest (Nsurlrequest (url:url!))

Self.view.addSubview (WebView)

Just like the Loadrequest method used to load the page above to load the page content set by the specified URL object. Method loadhtmlstring is used to load the specified HTML code fragment.

var html = "<section> "

Webview.loadhtmlstring (HTML, Baseurl:nil)

LoadData is also used to load the specified content, such as when a local HTML file is converted to a NSData object and displayed on WebView

var file = Nsbundle.mainbundle (). Pathforresource ("Test", OfType: "HTML")

var url = nsurl.fileurlwithpath (file!)

var data = NSData (contentsofurl:url!)

Webview.loaddata (data, MIMEType: "text/html", Textencodingname: "Utf-8", Baseurl:nil)

for detecting whether the content of a Web page is an interactive type, if you click on an address on the page to bring up the map, you can specify a link, address, phone number (phonenumber), etc. for the data type you want to probe. You can also specify that all types are probed for all, and the phone number can be clicked by default, and the following is the probe phone number

Webview.datadetectortypes = Uidatadetectortypes.phonenumber

There are many other ways, such as

Webview.reload ()

Webview.stoploading ()

Webview.goback ()

Webview.goforward ()

This protocol is implemented Uiwebviewdeleage if more advanced functionality is to be achieved.

Application of WebView in Swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.