Category Archives: iOS

Using SFSafariViewController to open URL inside app without transitioning to Safari or using WebView

RxSwift/RxCocoa example:

Chaining example on RxSwift

This is one of the reasons I love RxSwift. It allows to chain requests beautifully.

Here I authorize user by getting auth token, then I request userInfo, then I request profile.

Что нового в iOS 12

ARKit 2
Multiuser and Persistent AR. – сохранение данных из AR сессии в приложении, многопользовательский режим AR.
Object Detection. – распознавание объектов реального мира и добавление их в AR приложения.

Siri Shortcuts
Siri предсказывает быстрый доступ (shortcuts) для действий в приложении.

Health Records
Доступ к информации о здоровье в приложениях.

Effects
Показ стикеров и сообщений в iMessage в виде эффектов.

Interactive Controls in Notifications
Полная кастомизация уведомлений.

Authentication Services
Позволяет шарить логин-сессию между Safari и приложением, чтобы логинить пользователя проще.

CarPlay for Navigation Apps

Network Framework
Доступ к протоколам TLS, TCP, UDP.
URLSession, работающий с HTTP, построен поверх этого фреймворка.

Natural Language
Получение метаданных из текста.

Deprecation of OpenGL ES
OpenGL ES deprecated, необходимо использовать только Metal.

Получение строки, содержащей склонения дней дня день

Достаточно часто используемая штука, поэтому решил сохранить сниппет:

Использование:

Результат: 11 дней

Swift build failed. No errors.

This happened to me while I was writing in Swift 4.1 in XCode 9.3. I use RxSwift also. It never happened while I was writing in Objective C. There were no any logs in Report Navigator too. Project just compiled without errors, then failed. Couldn’t figure out why.

In my case what I did and it helped:

1. Commit changes in Git and then discard changes in XCode if it shows “M” near files
2. Clear derived data: Xcode -> Preferences -> location
3. Clean build folder: Shift + Cmd + Alt + K
4. Restart XCode

After this project tried to compile and showed errors. The error was here:

I passed wrong variable name in observer.onNext(timing). It should have been pTiming.

Using RACCommand and creating custom RACSignals in Objective C

To bind some action to the button click one can use just a custom RACSignal. But then you have to add three more things in every case:
1. Disable button, while action is performing;
2. Show, that action is in progress;
3. Disable button, if input is incorrect and button should not be able to be tapped.

Instead, if you use RACCommand, you get all these things in a box.

Let’s say, we want to bind RACCommand verifyPhoneCommand to a button. We can create a command in a lazy property in a ViewModel:

Here phoneValidSignal is this signal, that is formed by observing a phone variable:

We pass a RACSignal verifyPhone to the RACCommand to do a job:

This is how we bind a RACCommand to a button:

To track execution of a RACCommand and show an activity indicator we can use executing property:

If errors happen during RACCommand execution we can process those errors:

Asynchronous requests chaining using FlatMap operator in RxSwift

One of the main features of using RxSwift and RxCocoa or FRP is the possibility to chain several asynchronous requests and avoid callback hell. It can be achieved by using FlatMap operator and creating your own Observables from your asynchronous jobs like network requests or heavy calculations. But sometimes it is not simple to understand, how to do it. In this post I would like to show an example from one of my projects. The same can be applied to Objective C + ReactiveCocoa, the idea is the same.

First of all we should create Observables from different asynchronous requests. Then we can chain them using FlatMap operator. My example concerns a classic case – login and profile fetching. In many apps after logging in a user you have to request his profile in a separate request.

Usually you use MVVM with RxSwift. In a ViewModel you can create observables. In my case login() method creates an Observable from Alamofire request. In loadProfile() method Observable is created from ProfileService.

This is how you chain login and loadProfile requests. When they finish, you can do some job with a result profile. In this case I check a trial period and navigate to one of the views depending on a result.

Note that error for both requests is handled in one place using SCLAlertView. FlatMap operator maps a result of a first request to a second request and also flattens Observables. If you use just Map instead of FlatMap, the result of chaining will be an Observable of Observables.

Loading a TableFooterView that uses Autolayout from XIB

You can use Autolayout and Xibs to create a footer view. But you have to put your custom view to the container view, that you assign to tableFooterView. If you use Autolayout and load Header or Footer View from xib, you should add constraints.

Some useful features of a CocoaPods podfile

You can specify build settings for all pods or only for one pod. Here is an example:

You can create a podspec for a git repo, that is not a pod. Add this podspec to you project root and make a link for an added pod to specify a podspec. This is a better way to create pods for projects that do not have a pod, than creating a fork with a podspec, since you get latest updates more easily.

Handling Swift JSONDecoder typeMismatch error while using Codable protocol

If you have a problematic JSON, which can contain number or string for some keys, you can decode object without that property and manually set that property after decoding.

For example, I have a Vehicle class inside HistoryItem. In Vehicle model_year can be empty String or non empty Int. Here I am decoding modelYear manually using NSDictionary and trying to get Int. Swift 4 cannot do it automatically.

This is my Vehicle class that is contained inside HistoryItem:

As you see, there is no model_year key in CodingKeys.