How to create a WebView in iOS/iPhone?

iOSiPhone/iPadMobile Development

To create a web view in iOS we'll use Webkit framework of iOS. Previously UIWebView was used to create web views but that has been deprecated now.

We'll use WebKit View in this project.

  • Create a new project and from object library drag and drop webKit View to the ViewController.

  • Give constraints as per your requirement.

  • Make an outlet connection to the webKit in ViewController.

  • We'll open facebook in this example using the code below.

let url = URL(string: "https://www.facebook.com")
override func viewDidLoad() {
   super.viewDidLoad()
   let request = URLRequest.init(url: self.url!)
   self.wbView.load(request)
}

Now finally we need to add a key App Transport Security Settings in info.plist.

Open your info.plist file as source code and paste the following code −

<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Note − In devices running iOS 10.0 or later we also need to add Photo library usage description and Camera usage description in the info.plist

NSPhotoLibraryUsageDescription
NSCameraUsageDescription

This is how the app looks when running on a simulator.

raja
Published on 22-Jan-2019 17:10:40
Advertisements