WebView Tutorial in iOS 8 With Swift

The UIWebView class is use to embed web content in an application. It can be done by simply creating a UIWebView object and attaching it to a window and then sending it a request to load web content.

Open Xcode and create a new Single View Application. Fill out the Product Name, Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.




Go to the Storyboard and On the bottom of the Interface Builder you’ll see something that says “w Any”, “h Any”.  Change the width to compact and height to regular to change the view to an iPhone in portrait mode.



 Add a Web View to the main view. The ViewController should look like this.


 Select the assistant editor and open the ViewController.swift file. Ctrl and drag from the button to the class section and create the following outlet.



We will be needing a property to hold the url of the web view we want to show, so add a constant property. 

let url = "https://developer.apple.com/swift/"


Next, change the viewDidLoad to


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

let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL)
webView.loadRequest(request)
}


And that's it. Build and Run the project and wait for a couples of second and you should be able to view the website displayed into the web view.

0 nhận xét:

Đăng nhận xét