Swift resources

There are a tons of very useful resources out there for Swift and I have barely begun to scratch the surface. But below are some the resources that I have found invaluable in my few months of Swift coding.

  • http://objc.io is a website with 24 online magazine issues dealing with different iOS concepts in detail. Inspite of most of the articles written in Objective C, most of the concepts explained are iOS related which are relevant irrespective of the language used ( Swift or Objective C)
  • http://www.raywenderlich.com/ – Has very detailed text and video tutorials
  • https://www.udemy.com/the-complete-ios-9-developer-course/learn/#/ – Udemy course by Rob Percival. I am 30% there and I have found this course to start from Swift basics to important advanced features
  • Stack overflow: Most of the questions that have arisen for me have been asked here. I did have trouble in coming up with optimal search query that will get me to the answer.

I have used other resources while searching, but these have been the locations that I have referred to the most often.

Swift resources

How to create UITableViewCell with different UITableViewCellStyle?

Before I describe the issue I faced and how I solved it, here are a few resources regarding UITableViewCell style.

  • UITableView is the built-in control of iOS to create tables with a comprehensive set of delegate methods giving developers the option to customize.
  • UITableViewCell represents each row in a UITableView in iOS.
  • UITableViewCellStyle represents the different styles a row can be initialized in. The default value for this is , surprise, .Default. More details on what each of them indicate is here

There are 2 ways in which you can specify the type of UITableViewCell that you want to use for your UITableView:

  1. If you have only one type of cell in uitableview then you can use the following code in the uitableviewdelegate:cellForRowAtIndexPath

    With this method it is pretty straightforward to change the style of the uitableview cell to the desired value.
  2. The second, and more commonly used method, involves 2 steps:
    1. First, register the cell types that you are going to use in your uitableview in the viewDidLoad() method of you uitableviewcontroller
    2. Second, in uitableviewdelegate:cellForRowAtIndexPath, add the below method to use the registered cell class

      In this second method there is no straightforward way to change the style of the UITableViewCell as it was in previous method.

So when I was faced with this issue, I figured, this seems to be a pretty simple thing, Swift should have provided me a way with changing the cell style. I searched far and wide in stackoverflow.com and the only option is to create a new custom cell derived from UITableViewCell and override the constructor to change style if you are following the second method of initialization

So below is the sample custom cell to create a uitableview cell with .SubTitle style

Hopefully this will save you some time in trying to figure out if a simpler way exists

How to create UITableViewCell with different UITableViewCellStyle?

iOS autolayout tutorials

I solved some autolayout issues without much priori knowledge. Though it took long, it peaked my interest in reading more about autolayout. Below are few links ( mostly google results) about autolayout that I am hoping to read more thoroughly and write some blog posts on

iOS apple autolayout guide

Ray Wenderlich beginning autolayout tutorial part 1

Ray Wenderlich beginning autolayout tutorial part 2 

Mobile OOP autolayout advanced tutorial

Debugging autolayout issues

iOS autolayout tutorials

WKWebView javascript quirks

WKWebView is the more performant webview control introduced by Apple for iOS and OSX apps. You can read more about this in my previous post.  But as the post mentions there are many features missing that are present in the UIWebView control. One such quirk is related to how WKWebView handles javascript actions that result in an alert, prompt (or) request to open a new url in a new window(tab). If we dont correctly handle, all of these clicks will result in no-op in WKWebView.

  • WKWebView does not open any <a> links that have target=”_blank”. ( For eg: From inside a WKWebview, if you click on the trailers in http://www.tomcruise.com website , nothing happens).  In desktop browser, if target is “_blank”, a new tab is opened. In the in-app browser, we usually want the page to load on top of the current one. In order to achieve that in WKWebView, the following steps need to be followed.
    • Make your view controller that handles the webView callbacks to implement WKUIDelegate
    • In the view controller add the following code. This code verifies if the navigationAction’s target frame is nil(happens when target=”_blank”) and calls loadRequest on the navigationAction.request using current webview. The method ends with returning nil as we did not create a webView and used the existing one instead

  • WKWebView does not display the following Javascript prompts that are requested by web page.
    • Javascript prompt that shows a message that user can dismiss by a button pressWKWebView does not render any of the following prompts that are initiated by javascript code inside the WKWebView
    • Javascript prompt with OK and Cancel buttons, asking for user to make a choice of further action
    • Javascript prompt that asks for text input from user with an OK and Cancel buttons

To ensure all these three different javascript prompts show up, ensure that you have implemented the following three WKUIDelegate methods. The delegate methods give us access to the completion handler and which type of Javascript prompt to create. We need to create an iOS alert in the delegate method and render it modally for users to interact with.

I dont know why Apple’s Webkit could not have included a default implementation for these delegate methods with the creation and displaying alert controller

WKWebView javascript quirks