How do I invoke some Swift code from my HTML content?
Well the solution to this may feel a little bit “hacky” but it is a solution to achieve this. The following are the steps to achieve it:- Add a UIWebViewDelegate to your class definition: The UIWebViewDelegate is a protocol in Swift. You can think of a protocol like a Java interface, which means the method that implements or conforms to the protocol must implement the methods defined by that protocol.
- Implement the UIWebVIewDelegate protocol’s methods: which in this case would be as follows
optional func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
optional func webViewDidStartLoad(webView: UIWebView)
optional func webViewDidFinishLoad(webView: UIWebView)
optional func webView(webView: UIWebView, didFailLoadWithError error: NSError)
- The method of interest here is the one with the shouldStartLoadWithRequest parameter: So every time we send a request to load a url from a webpage in a webView, this method is invoked.
- As you can see the method has a boolean return type: So if this method returns true, it means we load the url and if false, means we do not load the url.
- Great, so what do we do with all this info? To tie this all up, one way to trigger execution of Swift code from some HTML content in a webview is
- Have a javascript function in your html page, which on being called modifies window.location
- This will invoke the webView method with the shouldStartLoadWithRequest parameter. At this point we can execute any Swift code that we would like to in our Swift class which in this case I suppose would be a ViewController.
- Now use the shouldStartLoadWithRequest.path property to determine the request for the path to be loaded.
- So if it is just a request to trigger some Swift code then we just return false from that method.
Getting data in Swift code from the HTML?
One way to accomplish this is using the UIWebView method stringByEvaluatingJavaScriptFromString that fetches us an optional String. So say we have a DOM element in our HTML file called “test”, we can store some data in it and get it in our Swift code. Something like this,var fromHtml:String? = view?.stringByEvaluatingJavaScriptFromString("document.getElementById('test').value")
Other useful tutorials
In addition to this, I have written several iOS tutorials with useful code samples.Building a native iOS Currency converter app
In this post, I will talk about my approach when building a new iOS Currency Converter. I focus on how I start by defining a simple user story, understand what the user story is followed by how I can accomplish all that technically. This post focuses on the requirements specification to build an iOS mobile app. The concepts discussed here apply to both all mobile apps, this document is focused on iOS Currency Converter. The rest of this document is structured as follows- iOS Currency Converter app requirements as mentioned in the assessment document
- Define the user story for the app
- A description of what the app will do
- Technical requirements i.e. what libraries or frameworks can be used to achieve this
- UI design samples for the minimal UI
- Some backend architecture design
- Decisions to adopt this style
- Lastly, Questions i.e. if any aspect of building this is not clear
Tutorial on animating rows in UITableView
This post is a tutorial on how to add, delete and reload rows in UITableView with animation. Shown below is some code that I wrote for Xcode playground. This code programmatically constructs a UIButton, UITableView, adds them to a view and styles them using auto layout constraints. Then, it randomly generates a number between 1 and 3 and shows how to either,
- Animate inserting a new row (UITableViewCell) into a UITableView
- How to delete and animate removal of a row (UITableViewCell) from a UITableView
- How to animate reloading a section of a UITableViewRow
The code also has the necessary comments that explains some of the things. You can read the full post below.
Build your first iOS app: A Tip Calculator
Ignoring the COVID-19 world we live in right now, going out to eat is something we all love and while we don’t mind tipping the wait staff, we often don’t understand the tip calculation. Hence, I thought that helping you make a tip calculator would be the best way to get you started on making your first iOS app. Don’t worry if you haven’t built anything in iOS prior to this, with the right instructions, it’s not hard. If I can do it, so can you. The rest of this article is structured as follows,- We will talk a little bit about what the app is
- Talk about the starter project and Xcode environment
- Discuss some storyboard concepts and write some code
- Finally, conclude with what you can do next after building this app
0 Comments