Author Archives: dragos

WKWebView and WKCookieStore in iOS 11

Today I am going to talk about the new WebkitView in iOS 11 and about our love/hate relationship.

The project I worked on was a simple iOS application which should provide a native component for login/signup, navigation, and use a web view to display his website and all the other features which were provided via the web page.

At first this task seems trivial and most probably there are many of you who already did it. However, not many of us implemented this on the iOS 11, using the latest tools provided by Apple and here I’m referring to the WKWebView with all its additional properties.

What I’m going to expose here are some least know features, or not clear from Apple’s documentation, or things we’ve struggled with while implementing this project.

The task is simple, set cookies on a webpage (so we know that the user is logged in), do some CSS manipulation on all pages (basically hide the navigation bar of the site and some other components) and add some custom JS so we know when the user navigates within an iframe.

So lets get started with it 💻


The first task is to add some cookies to the WKWebView so when we fire any request to have those in place. Previous to iOS 11, the cookies used by the app would be shared and stored in the NSHTTPCookieStorage . UIWebView would use cookies from this cookie storage (parent app cookie storage).

All of those has changed with the introduction of WKHTTPCookieStore. Starting with iOS 8 we have a new component for displaying web content in our iOS apps, namely WKWebView.

iOS 11 came with some changes and added some extra features to the WKWebView, basically each WKWebView now has its own cookie storage and its not share by the whole app and all other web views like in the case of (UIWebView).

To add cookies to a WKWebview we can use the cookie store WKHTTPCookieStore which has a set of methods for cookie manipulation, setCookie, getAllCookies and delete cookie.

All of those methods work with a completion handler which gets called after the operation was made. All those operations are async operations and SHOULD BE CALLED FROM THE MAIN THREAD. The documentation for this is kind of thin and we haven’t found it to be written anywhere.

All those methods work with objects of HTTPCookie objects. The initialisation of those objects is pretty straight forward and we won’t cover it.

Another interesting thing we found is that those calls don’t always succeed, and what I mean by that is that the completion handler is not always called, event if those methods are called from the Main thread and this is not blocked by any operation.

We found this behaviour to be pretty odd and we couldn’t find any documentation for it as well. This approach worked for when we first displayed the WKWebView, but when we would display it the second or third time, the setting of the cookies would fail.

Initially this broke our code as we relied on the completion handler of the setCookie to be called at some point, but it didn’t so after a few hours of trying different approaches and scrapping the web for answers we have found a StackOverflow hint.

The solution was to use a shared WKProcessPool object between all the WKWebViews used in the app. WKProcessPool states where the cookies should be saved, so by using the same pool, we have found out that the get, save and delete methods on the shared cookie storage work properly.

Hope this will save you some time. We found out that Apple’s documentation in are of WebKit is pretty scarce and thats is the main reason why we have a love/hate relationship.

CSS manipulation

In the app we build the target was from start iOS 11, which meant we could use all the new 💎 things (WKWebView and WKHTTPCookieStore).

First of we have to initialise the WKWebView before we start using it, in order to do that we would need an instance of WKWebViewConfiguration, which is a collection of properties used for configuring the WKWebView. This configuration is responsible for determining how soon a page is rendered and many other options.

This configuration object will be used for CSS manipulation. We want some CSS to be applied to the web pages before they are displayed on the screen, we want the header of our website stripped hidden in the web view presented in the app.

There are many properties on this configuration object, but the one we are most interested in for fulfilling the above stated task is the .userContentController property. WKUserContentController is an object which allows the user to inject JavaScript code in the web view.

The whole idea is the following, write some JS script that will append a CSS to the current HTML document structure and execute that when the web view loads.

The JS script for achieving this is the following:


We create a new style tag, we populate it with our custom CSS, in this example “header { display: none;}” for hiding the header element. You can place here any CSS code, in order to fulfil your desired behaviour. The last line adds the tag as a child tag to the current HTML document.

Now that we have all the elements, lets stick them together, we need to initialise a WKWebView, add the script and load a page.

The following code, does exactly that:


After calling the “configureWebView” method, the we can start loading pages using the load method of the WKWebView.

JS callbacks in Swift

The next step it is to add the a script which would notify us when a page on an iframe changes. Unfortunately the .navigationDelegate of the WKWebView doesn’t notify us regarding the changes in the iframes of a web page.

To achieve this behaviour we will use once again JavaScript, the process is similar to the one used before, but also has a little twist. We want a Swift callback every time the web page navigates to a new link, previously we have seen how we inject JavaScript into a web page (Swift -> JavaScript), but we haven’t talked about the backwards communication, JavaScript -> Swift.

In order to receive callbacks from JavaScript we need to implement the WKScriptMessageHandler protocol which has only one method

func userContentController(WKUserContentController, didReceive: WKScriptMessage)

This will be called every time our JS code posts a message back to the Swift code.

So how do we use it?

The process is very similar to the one used before

  1. Create a .js file for the JS code and populate it with the custom code which will notify the Swift code base when a certain event occurs


The previous code does the following, every time the window has change function is called from the HTML we post a message to the webkit with the identifier “notification_name” and some message, in this example “pageHasChanged”,

2. Add the JS and the delegate


3. Implement the delegate method


As you probably saw, its important to use the same identifier in both JS and Swift code for a certain notification, in this current example that identifier is “notification_name”.

That’s it, if you are interested in other cool features of the WKWebView I strongly encourage you to watch the following WWDC video.

Happy coding!📱

UIWindow, rootViewController, visual artifacts and leaks.

Hello 👋

Today I am going to talk about UIWindow, rootViewController and some of its quirks, namely how you can create leaks in your app by incorrectly using them.

My relationship with UIWindow is not that serious as the one with the WKWebKit, nevertheless we have some issues.

The task we tried to complete was straightforward, refactor the navigation between screens in an old application. Our approach was to start use Coordinators Pattern, and split the old codebase into multiple flows, each one responsible for a certain part of the application (Login, Feedback, Profile, etc.).

All this looked great on 📝 , so we started to implement it, needless to say that the codebase was poorly written and the navigation was splattered all over the view controllers in all sorts of bizarre extensions.

One common thing the old developers did, was manipulating the rootViewController of the UIWindow, and this one started creating us problems.

We all know that the refactoring process of an application is something which is time consuming and that the business doesn’t necessarily see the value in doing that, but thats another topic of discussion maybe for another post. What we tried to do was to implement the Coordinators Patterns only on certain flows of the application while leaving the rest of them to work as they did previously, meaning that we would have to make our flows work with the perviously existing UIWindow manipulation in place.

The problem wasn’t visible till we started presenting a view controller which had an alpha value of 0.5 which meant that it was transparent, and the view behind it was visible. That view controller was presented by replacing the root view controller of the current window.

What we have observed is the although the root view controller of the current window was changed, and the top view controller was the correct one (the transparent one) the old one was still visible under it.

Opening the UI view inspector did not help either, as both views were presented there as well. Our next step was trying to see who has a reference to that view controller and where is it displayed. We have started up the memory graph debug tool and we have found out that there are memory leaks, we had objects which weren’t deallocated as we expected them.

So, what happens?

We have tracked the issue to the way UIViewControllers were presented. If the previous rootViewController (or any of its children, or any other contained UIViewController if the root was a UINavigationController) had any UIViewControllers presented while changing the root of the window, those would not be deallocated and will still be visible.

What we end up doing in our project due to alpha on some view controllers and to they way screens were presented, was replacing the whole window with a new one and dismissing all the view controllers from the old one.

Dismissing the calling dismiss on the root view controller will dismiss “its immediate child view controller and all view controllers above that child on the stack.

And on that bombshell, see you next time and till then make sure to call dismiss on the presented view controllers so that other developers who come after you won’t have to waste their time trying to comprehand what happens.


Objective-C – Categories

Every developer, good or bad, at least those who have followed the programming courses at any university remembers some design patterns, MVC, Singleton and some of the also say Decorator.

In the iOS framework, Apple has used, and has guided the developers to use certain patterns, if we talk about the general architecture we most of the times find Model View Controller, if we want to use the notification system, we find out they used Observer pattern, and the list could go on.

Objective-C  is a hybrid language, which took, various parts for other languages, and evolved a lot in its over 32 years of existence. It has many great features, which helps you get your work done (eq. blocks), but it also has its lacks, you can NOT set the visibility of a a method as being private or protected because of the dynamism of the language.


In the beginning of this post, I’ve mentioned the Decorator pattern, and that is because this post should be about categories, Objective-C categories, and those two are somehow related.

With the Decorator pattern, you would add some new functionality to an existing class, that is the main purpose of Objective-C categories but with some constraints.

If you have started working in iOS you probably did already use categories, without knowing they are called like that, but enough talk lets jump to examples.

@interface HelloWorldObjC : NSObject

@property (strong, nonatomic) NSString *greeting;

- (void)saySomething;


We declared an interface for a class which has one method, and one property, a greeting which is a string which will be printed by the say something method.

@interface HelloWorldObjC()

@property NSString *beforeGreetingString;


@implementation HelloWorldObjC

#pragma mark - Lifecycle

- (id)init {

self = [super init];

if (self) {

self.beforeGreetingString = [NSStringstringWithFormat:@"%p", self];

self.greeting = @"Hello there!";




#pragma mark - Public methods

- (void)saySomething {

NSLog(@"%@ says: %@", self.beforeGreetingString, self.greeting);



As for the .m file, the implementation, as you can see there is another place where you could declare variables, making them “private”, that section @interface ClassName() is a special type of category called extensions. That is why I said in the beginning of this post that you might have used categories without knowing how they are called if you are new to ObjC. You can define an extension in the .m file of a certain class, where the @implementation of that class resides and create new properties which your class will be able to use. One thing to mention about this properties is the fact that they are synthesised, which means you get a getter, a setter and a backing ivar for your property. Moreover, you can also declare methods in the extension. Long story short, everything you declare here, can only be used in the @implementation section, if another class inherits or someone makes a category over this class, you will not be able to access anything defined inside the extension, and you will only have access to the things defined in the .h (@interface) file (without using some tricks at least.).

The initializer sets the greeting to a default one, in case someone forgets to set it when using the object and also sets the value of the interface defined property to the memory address of the current object.

The saySomething method only displays a string composed of the value of the address of the object in memory, and the defined greeting.

A real category

This is supposed to be about categories, but where are them? Ok lets see what a proper category looks like.

@interface HelloWorldObjC(Smile)

- (void)addSmileToGreeting;


In another file usually named “HelloWorldObjC+Smile.h” we can add a new category, as you can see, the declaration is pretty straight forward, you declare it like any other class, without the inheritance and with a unique name in between parenthesis. What can we declare here? Well there are a few rules, you can define new methods and even properties, the compiler won’t complain if you provide the getters and setters in the implementation section your program won’t even crash. Basically what you can do here is add some new methods, some new functionalities to your original class, but you can not alloc new memory for it, that is why you can not add new properties backed by an ivar, because the categories are not new objects, they are the same object as the original class. To make it easier for you to understand, you can do an  category for every class you want (YES event for classes from Foundation, UIKit and so on), and you can use the methods you add to those classes using a category wherever you implement the header with the definition of the category. Now picture the following scenario, you are working with NSStrings all over the app, I come along and make a category over NSString and add new properties to it, what is suppose the system to do? Add those variables to all the objects, of NSStrings, wherever I import my header? The answer is NO, categories are not allowed to modify the size of the object in memory by adding some new values to it. That being said, you can not add properties, and ivars to a category. Another restriction is the fact that you can not overwrite methods on the base class, for obvious reasons.

@implementation HelloWorldObjC(WithSmile)

- (void)addSmileToGreeting {

[self.greeting stringByAppendingString:[NSStringstringWithFormat:@"%C", 0xe04f]];



As for the implementation of the category in the “HelloWorldObjC+Smile.m” file, we only add an emoji to the current greeting message.

Well this doesn’t look like much, what can I use them for? Well there are many usages, for example you can use them to explicitly mark for the people in your team which methods are “private” or any other sort of example where you use a category in order to make your code more readable. But by far the most common usage is by adding some sort of functionality to a certain class, in my projects i usually make a category over UIColor so that i can initialize a colour from a hex string, you can put there all sorts of helpers on any class for the app you’re building.

Xcode run script

There always comes a time in the life of a iOS developer when it has to create a for various reasons a script to automate a certain task. For example, you can create a script on your production target that every time you make an production build your localisable files will be stripped of unused keys, this was a simple example but you can create more complex functionalities with this feature of Xcode.

How do you create an run script?














Thats it, simple as that.


Now lets talk about what can we write in here.

What sort of scripting language can we use, we can use basically any type of scripting language that comes installed on macOS, a few example would be:


Another great thing, which I found out from an Stackoverflow post, is the fact that we can specify input and output files. Basically we can tell Xcode which file we are going to read in our script so that the build script is run ONLY if one of those files has changed, if the files haven’t changed Xcode caches the result, so it won’t affect the build time. Output files are the files out script writes to, we specify those so that if they haven’t changed, Xcode can cache the build.

One specifically thing worth mentioning, is the fact that the order of the build phases, matters, so if you want your script to be run before the compile phase, you can do that by dragging it before the compile phase.

If you follow the 3 images in this post you will have a project which when you’ll run it it will produce a file named myFile.txt in the root of your project with the content “Hello world”, while this is useful, sometimes when you are writing your script, you need to check the output of the commands. If you modify the script by deleting “> myFile.txt”, the result will be just ‘echo “HelloWorld”‘, if we delete the initial file and run the project again, no file will be generated and we can no longer see the output. In order for us to see the output of the script, we need to access the Report navigator in Xcode and select the build phase.






Last but not least, you can use many path variables and even git commands to fetch any details about your project repo.

The heart of the app

Today I’m going to talk a bit about the AppDelegate. In every demo project, and most of the times (I currently don’t know any examples where it does not) in every app, there is an class named AppDelegate.


The app delegate is the entry point in an iOS application, here the app we are developing will receive different notifications regarding its state, push notifications and here is also the place where the app initialisation takes place.

Every iOS application has a lifecycle composed of 5 different states.

  1. Non-running – the application does not run, has NOT been launched
  2. Inactive – the application is in foreground but does not receive events, this is a transition state, and we usually find the app in an inactive state when launching the application or when the application runs and we receive an phone call
  3. Active – the application is in foreground and receives events (the application is open, we can interact with it)
  4. Background – the application runs in background and executes code (for eq. in an navigation app, fetching the current location of the user, and alert the user that its gonna need to change direction)
  5. Inactive – the application is in background and does not execute code (the application can become active if we tap the icon again, or press on the home button for all the apps to be displayed and select it from there, the application is loaded in memory, to not be confused with the non-running state)



The app delegate is and singleton and is baiscally an object that comforms to the UIApplicationDelegate protocol. Besides the AppDelegate, every project has an singleton object of UIApplication type, which actually does all the hard work, and it just passes its events to the AppDelegate class of our application.

This means that the AppDelegate is an object that can be accessed during the whole time an iOS application is active, and keeps its state (is a singleton we don’t have multiple instances).

One common pattern when beginning to develop apps for iOS is to have a fat AppDelegate and multiple links with other classes from your app, as it keeps its state and is accessible from all over the app for beginner is the perfect place to keep different informations. The app delegate should be lightweight, should only handle the state changes of the app, and the initialisation of it, everything else, different properties or variables most of the times they should NOT be there.



What I do for my apps in order to have a light app delegate is create classes for various classes of notifications. For eq. I have a class that handles all the push notification flow, basically the app delegate only tells my NotificationClass, that there is a new notification, the handling (the parsing of the notification, decide how to act on it) is done by my NotificationClass object. Respecting this rule, you’ll end up with a very light weight app delegate, and classes that are focused on only one job (as they should be, by respecting the “Single responsibility” from the OOP principles), which in the long term is easier to maintain and modify.

I’ve seen app delegates which had over 2000 lines of code, and I tell you now, thats some ugly stuff. We have to look at the app delegate like a phone company, its job is to link various people (the app delegate job would be to link an event to a handler), it should not care about how it can handle an event, it should only care about sending it to the right object for handling it.





Gain new knowledges in the areas you are interested in and share your experience with others. FIVE helps you connect and interact with people who share the same passion, because you are the sum of the FIVE people you spend the most time with. 

The application finds nearby people that could help you improve your skills in various domains such as mathematics, computer science, arts and even parenting if you struggle with that, but it also offers you the chance to give something back to the world and share your expertise in the areas where you confident that you can help others.

In order for you to optimise your learning time, and not struggle with finding a right place in the calendar for meeting with a person who could help you with your hobbies or even more serious problems you are facing with bigger projects, FIVE suggest you places for you two to have lunch. Why lunch? Because we usually spend this time with people we know talking about unimportant stuff, and we at FIVE strongly believe that we can spend this time to nourish our body as well as our minds.




So you wanna learn iOS …

Hi there, this is my first post from many (hopefully) to follow in which I try to explain things I struggled with or things I found interesting while learning how to make apps for iPhone.

One of the first things someone who is willing to learn iOS development has to do is get his tools. Easier done then said if you are on a Mac, all you need to do is download Xcode and you are good to go. However, if you are on a Windows or Linux machine, you need to install a virtual machine which has macOS, which is not legal, but it can be achieved, or install macOS directly in you computer, if your hardware supports it. Otherwise, if you want to get you hands dirty, you can always build yourself a hackintosh, but in my humble opinion is if your can afford it, you should by a Mac, it will save you of a tons of “troubles” and you will love it.

Assuming you’ve installed Xcode, on any of the setups described above, we can move to the next step and explain a bit what is Xcode and why we are using it. Xcode is one of the most powerful and widely used IDE on Mac for developing C, C++, ObjectiveC and Swift apps. It’s highly optimised for building iOS, tvOS, watchOS and macOS apps, but people also use it to develop other types of apps.

Why use Xcode?

Apple has tried (successful in my opinion) to make the developers life as easy as possible. What do I mean by that? Basically all you need to develop, test and lunch your client side app is build in this IDE. It has a lot of powerful tools for debug (see Instruments), development, and deploying your app, and you don’t need to struggle with other things, such as different command line tools, or multiple programs or IDE’s just to achieve your purpose (creating an iOS app). Most of the developers I know, experts or beginners use it for development, but the development can be done in other environments as well for eq. Facebook developed Nuclide.

Xcode is definitely a perfect tool, but most of the people I know, in fact all the developers I know personally, get along with it pretty well and are contempt with its performance. Of course depending on the project, Xcode might not be the right tool for you but, if you are just starting developing apps for Apple platforms, I strongly suggest you to use it.

Other tools

What other tools? Like I said Xcode is all you need! Next time we’ll start to talk about actual development as its pretty straight forward to get a simple project up and running.

Start Xcode -> “Create new Xcode project” -> iOS Application -> Single View Application -> Enter a product name such as “My demo app” -> Next -> Choose a location on the disk and press create!

Now just press the “play” icon on the right of the macOS close, minimise and maximise buttons and your first iOS application will start on the selected simulator.

Thats it, you new shiny demo app is running.

See you next time!


Hello World

As one of the first programs you write in a new language (except Assembly and Lisp and Prolog and … ), I’ve chosen to make it as well as my first blog post. If you arrived here it means you were interested in my blog and read all the articles (which is unlikely) or this is my only article so far.

Now for the body of our program, we have our environment settled, so its time for me to say what I’m gonna do with this, the basic idea is that I’m going to write things that I found interesting, mostly programming and geeky stuff, but there might also be some more personal and cultural ones.

The main purpose of an “Hello World” program is to see that your environment is working and to get a glimpse at the way the new language works, another important aspect of it is the fact that is usually short that being said, return 0; and see you around.


It is an an app that allows you to control your OS X device from your iPhone or iPad. I can not be the only one who is too lazy when I watch a movie to get up and adjust the volume.

This app will only work if you are having an OS X device and an iPhone or iPad (iOS 7 or later) connected to the same WiFi network.

With this app you can:

  • Adjust the volume.
  • Mute/Unmute
  • Put your computer to sleep, or shutdown ( YOU WILL LOSE ANY UNSAVED PROGRESS ).
  • Play/Pause
  • Next/Forward
  • See all your opened apps
  • Bring an app to the front (in front of all your other apps)
  • Close any opened app
  • Enter in the full screen mode for any app
  • Open any installed app on your computer.





This window will pop, this is useful in case you want to manually connect your iOS device with your computer (this should be the case when you are using a LAN connection as well as an WiFi one).

You can close the window (pressing the CLOSE button), this will not exit the application, the application is still running, you can see an icon in the top bar.

In case you closed accidentally the window and you forgot to enter the code, the code is still displayed here.

To exit the application just click on “Exit” from the menu. (You should not do that if you still want to remotely control your computer)



You can automatically connect, that should be the case when you are using a WiFi router and you have you computer connected at the same network as your iOS device  and your computer IS NOT USING A LAN CONNECTION (only the WiFi).

The name of your computer should automatically appear in the picker list.

You can use the refresh button (right upper corner) to reinitialise the search.

Otherwise you can click the “MANUAL” bottom bar.

Here you should enter the address provided by desktop application (the whole address exactly as displayed), in the example ca



From this view you can control the basic features as slew, shutdown, volume, play pause, next, previous. You can swipe on the screen to access the other features.

In this view you can see all you opened apps, and tap on them for actions. After you tap on an app name the fullscreen, close, and bring to front actions will be displayed and can be performed.

You can swipe left or right for the next or previous view.

You can pull the list for an updated list of applications, or you can press the refresh button, top right corner.

In this las view, you can enter the name of an application and press the Open button, which will open the app if this exists.

Swipe works here as well.





To make it work, you will have to follow the next steps:

  1. Download the app from App Store (iOS).
  2. Download the desktop application at the end of the page.
  3. Run the desktop app.
  4. Run the iOS app and connect.
  5. Control your computer.




If you are using Wifi and LAN on your computer you will probably need to connect manually (the iOS app won’t be able to find your computer).

If the desktop app shows :

No IPV4 address!

in the address section (and in the top bar), then there is something wrong with your connection, you are connected to multiple networks.


The address is composed from you local IP address and a free port usually is the 5001.



iOS application download







The classic game from your childhood on Android platform.




  • multiplayer
  • global highscores (multiplayer or single player)
  • all the words from Romanian language
  • word “Look up” (the meaning of an used word)



Google Play