By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Till Swift 1. Apple has updated the declarations and they now require non-nil values. They have adding reality to the declarations for the benefit of Swift and ObjC. Learn more.

Author:Zolocage Dur
Country:El Salvador
Language:English (Spanish)
Published (Last):22 August 2014
PDF File Size:2.17 Mb
ePub File Size:14.98 Mb
Price:Free* [*Free Regsitration Required]

WKWebView is a powerhouse on iOS, providing high-performance web rendering wherever and whenever you need. So, if you want to solve a specific problem, or if you just want to see what WebKit is capable of, read on! All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community. Start your free trial! Sponsor Hacking with Swift and reach the world's largest Swift community!

This is inefficient, and also far harder than it needs to be. Having a dedicated webView property is helpful so that you can reference its properties and methods more easily later on. If you intend to load URLs frequently, you might find it easier to wrap that behavior inside an extension:. For example, if you wanted to load a file called "help. That url. For example, this displays a heading message:. If you are referencing assets in your bundle such as images or CSS, you should specify Bundle.

For example:. Second, make that object the navigation delegate of your web view. Finally, implement the decidePolicyFor method, adding any logic you want to decide whether the page should be loaded. As an example, this implementation allows the user to visit the Apple homepage and nothing else:. If you were using your view controller you would write this:. Finally, implement the decidePolicyFor method with whatever logic should decide whether the page is loaded internally or externally.

For internal loads make sure you call the decisionHandler closure with. This can be done by observing the estimatedProgress property, like this:. You should now implement the observeValue forKeyPath: method, which will pass you a string saying what changed. You can use webView. Now implement the observeValue forKeyPath: method.

This is all available inside the backForwardList property of web views, which itself contains the array backList and forwardList. Inside each of those arrays you can read the URL for each page that was visited, along with the title that was used.

For example, you can print out a list of all sites the user has already visited using this loop:. Once your web view has loaded some content, you can execute any JavaScript you like inside that rendered page by using the evaluateJavaScript method. You can read through the complete list of cookies associated with a website by using the httpCookieStore property of your web view.

This is buried under the configuration. User agents let your web servers identify the type of web browser that is accessing a page, and are commonly used to enable or limit which features are available. Note: There's nothing stopping you from changing the user agent when accessing other resources, but keep in mind that some sites might read the user agent string and get confused if it doesn't match what they expect. Fortunately, you can change that using the WKUIDelegate protocol: set an object to be the UI delegate of your web view and you can show custom alerts, manage your own tabs, and more.

For example, you can make WKWebView show a custom alert controller when any web page uses the alert JavaScript function:. Although you can use the regular drawHierarchy method for converting a view to an image, WebKit comes with its own takeSnapshot method that lets you crop and size the image as needed.

Web views have built-in data detector support, which means they can make things like phone numbers, calendar events, and flight numbers into tappable links. Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News , the maintainer of the Swift Knowledge Base , and a speaker at Swift events around the world.

If you're curious you can learn more here. RSS feed. Articles Learn Start Here. Start Here. About Hacking with Swift. About the author Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. More articles. New book: Understanding Swift. Spot the Swifty. How to build neumorphic designs with SwiftUI. Was this page useful? Let us know! Link copied to your pasteboard.


How to load a HTML string into a WKWebView or UIWebView: loadHTMLString()



Working with UIWebView – loading contents in different ways


Related Articles