Category Archives: iOS

How to create semi-transparent outside border for UIImageView

Just in case if somebody looks for an UIImage with transparent outside border. If you simply set a layer border, you will get a transparent border, but you will see inside image behind that border, not outside image. I managed to create an ImageView with transparent outside border.

It looks like this:
cool image

The idea is simple. I save UIImage from UIImageView. Then I remove UIImage and set initial layer as border layer. Then I put a new smaller sublayer above it and set a saved UIImage as contents of it.

The same approach can be used for UIButton or UIView.

Что нового в iOS 9

Новинки в iOS 9:

1. Новое встроенное приложение News, улучшения Notes, Maps, Wallet, CarPlay.

2. На iPad появится Split View, можно будет пользоваться несколькими приложениями одновременно, например, Safari и Twitter.

3. Siri станет умнее. Например, можно будет попросить Siri напомнить о чем-то, когда ты будешь дома. Она сама создает уведомление с привязкой к локализации.

4. Улучшения производительности. Батарея на 1 час дольше будет держаться. Встроенные приложения начнут использовать Metal, то есть по сути графический процессор. Для обновления системы достаточно будет не 4, а 1 гб свободной памяти.

http://www.apple.com/ios/ios9-preview/

#iOS9, #Apple

How to clean all targets in XCode 6 including Pods

When you clean a project, you actually clean only main target. If you want to clean all targets, for example, Pods after installing new versions of pods, you should use another option.

To clean all targets in XCode 6 including Pods use Shift+Cmd+Alt+K button. It calls “Clean Build Folder…” command, which is not visible from menus.

Probably, I don’t know something, because this way of cleaning seems to be not so obvious.

My visit to a YaC 2014 Conference (Mobile section)

YaC 2014 – Yet Another Conference was organized by Yandex in Moscow in October 2014. It is a largest developer conference in Russia. I have participated only in a Mobile section.

There was a presentation from Orta Therox – the creator of CocoaPods, called “Tools, Testing and new New Team Members. The Story of Post 1.0”. Here is his presentation.

A presentation from Ashley Nelson-Hornstein from Dropbox called “Building Quality Code That Lasts: A Dropbox Story”

and some other presentations from Russian developers.

All materials are published now and available for free.

What I did get from all that is something new about unit and integration testing – using mocks to speed up network connection related tests, information about some new instruments and team development, something new about View stacks and some understanding of possibilities of using JavaScript in a new iOS WebView – WKWebView. Found a new friend – another mobile developer.

Here are some of my own photos:

How to safely work with NSDictionary that may have empty values or may not have a key

Dictionary_2x

NSDictionary sometimes can contain a key with empty values or even may not contain a key, that we expect to have. This is a very common problem, when you work with server APIs and parse network JSON responses to a NSDictionary. In this post I will describe how to safely work with NSDictionary that may have empty values or may not have a key.

The method objectForKey will return nil, if there is no a key, a key doesn’t exist in a dictionary.

You can’t add nil to a NSDictionary. NSDictionary will throw an NSInvalidArgumentException if you attempt to insert a nil value. You would have to use [NSNull null] instead. NSNull is something called nothing, while nil is simply nothing.

Checks that value for key is not empty.

But if a dic doesn’t contain a key, you will enter to the body of a condition dic[@”key”] != (id)[NSNull null].

For example, before taking integerValue from obj, you must be sure, that it is not [NSNull null]:

Checks that the key exists, but value for it can still be empty.

So the optimal check while reading NSDictionary is this, where key is “key”

You can’t add nil to a NSDictionary. So you have to check, that your object is not nil before adding:

But you can add [NSNull null]:

Version of code, where you check if dictionary entry exists and not empty:

I have created a NSDictionary category that has a method to check if a key exists and value is not empty:

This is an example, how to use it:

iOS : Creating Random E-mail and Phone Number

roulette_lg

This is a sample code how to create random e-mail and phone number. I used it in my unit tests of authorization.

Solution for iOS Developer Technical Interview Problem

I have found an interesting iOS Developer Technical Interview Problem and solved it.

“I also have the ultimate iOS Developer technical test you can assign a potential hire. It should take 1-3 hours. It is easy to communicate, allows a lot of freedom of implementation so you can really get a better picture into how a developer thinks, and will make sure this developer knows the absolute fundamentals. Ready for it?

Calculate the and display each Fibonacci number from 1 -> max N possible on an iPhone with unsigned integers, and display each F(n) in a table view. The UITableView scrolling MUST remain smooth.

That’s it. You’ll be amazed at how profoundly simple this task sounds and yet how much iOS knowledge can be demonstrated. Not just what they know, but how they structure their work. You can assess their APIs, their separation of concerns when designing classes, the considerations they’ve made for performance, and their knowledge of concurrency. (Not to mention their knowledge of recursive functions.) It is ok to give them the formula, and allow them to use Google. F(n) = F(n-1) + F(n-2).”

This is the result:
IMG_0051

GitHub repository with a solution is here.

I used recursion and concurrency. I added a little delay of 0.1 s to make a delay visible but scrolling is still smooth. This is my solution:

It scrolls without lags on iPhone 6 Plus. I do not calculate twice a fibonacci number if already have calculated it, I store a new fibonacci number in a NSMutableArray.

Using dispatch group to wait until multiple operations executed – including completion block (AFNetworking)

Sorting objects in Objective-C in Alphabet Order by Name

It is a very easy but a common task. There is no need to create any custom NSSortDescriptors. Let’s say, we have an unsorted array of RRRegion objects, that have a name property. This is how we can sort this array:

SQLite FMDB Batch / Bulk Inserts

SQLite
I use FMDB framework to work with SQLite database on iOS. Sometimes you need to insert many rows, many objects that come from a server to your local database. It can take some time. When I was inserting rows one by one, the method took 50 seconds to finish inserting about 1300 rows. So I decided to find a faster solution.

And found it here. To insert many rows faster you should use transactions. It is very easy, you shouldn’t use complex approaches, like preparing huge SQL statements. I got 0.5 seconds as a result on iPhone 3GS. Here is more sophisticated approach to optimize insert of millions of rows, but it’s not my case, I’m satistfied with 0.5 second result.

_72483466_clockthinkstock

This is a sample code:

I have a class RRRegion, which objects I am inserting. I open database, then begin a transaction, execute as many updates as I have objects and commit a transaction, close database.

I should mention, that when I was using caching of statements by FMDB, I got strange leak statement errors, so I don’t cache them. I don’t have this line:

This is how I measured the time it takes: