This Content is from Stack Overflow. Question asked by Gralex I’m trying to restore For serialization I need serialize at least one So how to serialize history of So for now I see only 1 option: make custom navigation with serialization support and forgot about If you’re targeting iOS 15 or above, you should look
into It’s erased to This Question was asked in StackOverflow by Gralex and Answered by Pyry Jahkola It is licensed under the terms of
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0. people found this article helpful. What about you? Skip to main content This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
WKWebView.GoTo(WKBackForwardListItem) Method
DefinitionIn this articleNavigates to an item from the back-forward list and sets it as the current item.
Parametersitem WKBackForwardListItem The item to which to navigate. Must be one of the items in the web view's back-forward list. ReturnsWKNavigationA new navigation to the requested item, or null if it is already the current item or is not part of the web view's back-forward list. AttributesRemarksApplies toGet WebKit into your app the easy way
In this article I’ve put together 15 of the most common use cases for Sponsor Hacking with Swift and reach the world's largest Swift community! 1. Making a web view fill the screenSometimes you’ll see folks add code to A simpler approach is to add a property to your view controller like this:
Then overriding the
Having a
dedicated 2. Loading remote contentGiven that one of the major uses for
If you intend to load URLs frequently, you might find it easier to wrap that behavior inside an extension:
Now you can load a website just by running 3. Loading local content
For example, if you wanted to load a file called "help.html" you might use code like this:
That
4. Loading HTML fragmentsYou can generate HTML in code and feed that directly to
Note the
5. Controlling which sites can be visitedBy default First, make something conform to
Second, make that object the navigation delegate of your web view. If you were using your view controller, you’d write this:
Finally, implement the
6. Opening a link in the external browserIt’s common to want to handle some links inside your app and others externally, and this doesn’t take much work thanks to the First, make an object conform to the protocol – it might be your view controller, but it doesn’t need to be. For example:
Second, set that object as your web view’s navigation delegate. If you were using your view controller you would write this:
Finally, implement the As an example, this implementation will load all links inside the web view as long as they don’t go to the Apple homepage:
7. Monitoring page loadsLoading one web page means fetching some HTML, downloading any JavaScript and CSS it uses, downloading any images, and so on. To help keep your users informed, it’s a good idea to monitor page loads and show some sort of user interface updates so they know something is happening. This can be done by
observing the
You should now implement the
8. Reading a web page’s title as it changesYou can use To do that, first register to receive notifications when the title changes:
Now implement the To get you started, this code just prints the title whenever it changes:
9. Reading pages the user has visitedIf you’re serious about building a useful browser experience, you’ll probably want to read the list of sites the
user has visited. This is all available inside the 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:
10. Injecting JavaScript into a pageOnce your web view has loaded some content, you can execute any JavaScript you like
inside that rendered page by using the As an example, if you had a page that contained
11. Reading and deleting cookiesYou can read through the complete list of cookies associated with a website by using the As an example, this code loops over all cookies, and when it finds one called “authentication” deletes it – all other cookies are just printed out:
12. Providing a custom user agentUser 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. If you’re reading pages from your own server, you can adjust the user agent to your own string so that you can identify users of your app. For example:
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. 13. Showing custom UI
Fortunately, you can change that using the First, make some object such as your view controller conform to it:
Second, assign your view controller to the
Finally, implement as many of the optional methods of
There’s also Note: You must call the completion handler when you’re finished. JavaScript’s alerts are blocking, which means JavaScript execution will not continue until the alert finishes. As a result, WebKit will complain if you don’t let it know when you’re done. 14. Snapshot part of the pageAlthough you can use the regular For example, this will produce a 150x50 image from the top-left of the web view:
If you don’t want a cropped image - i.e. you want the whole thing – just use 15. Detecting dataWeb views have built-in data detector support, which means they can make things like phone numbers, calendar events, and flight numbers into tappable links. These are all disabled so the default behavior is to render web pages as they were designed, but it’s trivial to override – just create your web view using a custom
For example, this instructs the web view to detect all possible types of data:
Sponsor Hacking with Swift and reach the world's largest Swift community! |