Search Your Question

Download task when app is inactive

 https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background


For the background download, there is beginBackgroundTaskWithExpirationHandler: that is specifically designed to do that. When you use it, you will get few more minutes to execute whatever you need (after that limit, your application will get terminated no matter what).

You can write following methods:

func beginBackgroundTask() -> UIBackgroundTaskIdentifier {
    return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}

func endBackgroundTask(taskID: UIBackgroundTaskIdentifier) {
    UIApplication.sharedApplication().endBackgroundTask(taskID)
}

When you want to use it, you just simple begin / end the task when starting / finishing the download call:

// Start task
let task = self.beginBackgroundTask()

// Do whatever you need
self.someBackgroundTask()

// End task
self.endBackgroundTask(task)

Hope it helps!

What is SSL Pinning and How to implement it?

SSL stands for Secure Socket Layer, which is a protocol for creating an encrypted connection between client and server. It ensures that all data pass in network will be private and integral.

How SSL works? When client establishes the connection with server (called SSL handshake):

  1. Client connects to server and requests server identify itself.
  2. Server sends certificate to client (include public key)
  3. Client checks if that certificate is valid. If it is, client creates a symmetric key (session key), encrypts with public key, then sends back to server
  4. Server receives encrypted symmetric key, decrypts by its private key, then sends acknowledge packet to client
  5. Client receives ACK and starts the session

Using SSL, the client will allow the connection only from trusted sources that have the valid certificate. And it looks good for most cases. But what if someone stands between client and server, and acts like they are the real server? Let's call client is C, server is S and the attacker is A.

In step 1, instead of sending packet to S, A can catch the packet and pretend it as S. What if instead of receiving certificate from S, client C will receive fake certificate from A and believe it's valid. A can make C think that it is communicating with S, but actually all connection flows will be directed to attacker A.

Hence, SSL pinning can be the solution to prevent Man-In-The-Middle (MITM) attack. SSL pinning will ensure that client connect with designated server. The main key of SSL pinning that server certificate will be saved in app bundle. Then, when client receives certificate from server, it then compares 2 certificates to make sure that they are the same before establishing the connection.

URLSession

For NSURLSession, the main method to handle SSL pinning is URLSession:didReceiveChallenge:completionHandler:delegate. Set your class to conform URLSessionDelegate and paste this function to your class:


func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        if let serverTrust = challenge.protectionSpace.serverTrust {
            var secresult = SecTrustResultType.invalid
            let status = SecTrustEvaluate(serverTrust, &secresult)
            
            if (errSecSuccess == status) {
                if let serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0) {
                    let serverCertificateData = SecCertificateCopyData(serverCertificate)
                    let data = CFDataGetBytePtr(serverCertificateData);
                    let size = CFDataGetLength(serverCertificateData);
                    let cert1 = NSData(bytes: data, length: size)
                    let file_der = Bundle.main.path(forResource: "name-of-cert-file", ofType: "cer")
                    
                    if let file = file_der {
                        if let cert2 = NSData(contentsOfFile: file) {
                            if cert1.isEqual(to: cert2 as Data) {
                                completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust:serverTrust))
                                return
                            }
                        }
                    }
                }
            }
        }
    }
    
    // Pinning failed
    completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
}

This function will “requests credentials from the delegate in response to an authentication request from the remote server.” We will compare the certificate from server with the one that saved in app bundle. If 2 certificates are identical, the authentication will let it pass and client can connect to server.

Types of SSL Pinning(What to Pin)?

Pin the certificate: You can download the server’s certificate and put this in your app bundle. At runtime, the app compares the server’s certificate to the one you’ve embedded.

One disadvantage of SSL pinning is that you have to save the certificate in the app. Whenever the certificate is updated, we need to release new version of app. But this also leads to another problem: what we do with older version?
  • Maintain the old certificate for a time, until we make sure all users have downloaded new version already.

Pin the public key: You can retrieve the certificate’s public key and include it in your code as a string. At runtime, the app compares the certificate’s public key to the one hard-coded hash string in your code.

Explain NSOPERATION Priority.

Ans : 

All operations may not be equally important. Setting the queuePriority property will promote or defer an operation in an NSOperationQueue according to the following rankings:

public enum NSOperationQueuePriority : Int {
    case VeryLow
    case Low
    case Normal
    case High
    case VeryHigh

}

The operations with high priority will be executed first.

let backgroundOperation = NSOperation()
backgroundOperation.queuePriority = .Low

let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperation(backgroundOperation)

Swift Initializers

Ans :

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

Initializers : Initializers, are like special methods that can be called to create a new instance of a particular type.

init() {
// perform some initialization here
}

Customizing Initialization :

struct Human {
    var gender:Gender
    var age:Int = 10
    
    init(age:Int) { // initializer 1
        self.age = age
        self.gender = .unknown
    }
    
    init(age:Int, gender:Gender) { // initializer 2
        self.age = age
        self.gender = gender
    }
}
//------------------------------

let human2 = Human(age: 20)
let human3 = Human(age: 40, gender: .male)

We can have more than one initializers based on our requirement, with different parameter names, types. The swift compiler will decide which init method to call based on the argument label. 

Note that it is not possible to call these initializers without using argument labels. Argument labels must always be used in an initializer if they are defined, and omitting them is a compile-time error:

let human4 = Human() // error :cannot invoke initializer for type ‘Human’ with no arguments
let human5 = Human(40,.male) //error:error: missing argument labels 'age:gender:' in call

Default Initialisers :

Swift provides a default initializer for any structure or class that provides default values for all of its properties and does not provide at least one initializer itself.

class ShoppingListItem {
var nameString?
var quantity = 1
var purchased = false
}
var item = ShoppingListItem()

Memberwise Initializers for Structure Types :

Structure types automatically receive a memberwise initializer if they do not define any of their own custom initializers. 

struct Size {
var width, height :Double // stored properties without default values
}
-------- or --------------
struct Size {
var width = 10.0, height = 30.0 // stored properties with default values
}

In above both Size struct, there is no any init method defined. Still Size struct can be instantiated by 

let twoByTwo = Size(width: 2.0, height: 2.0)

If we provide custom initialisation, then memberwise initializer instantiation will be failed. See following :  

struct Size {
var width, height :Double
init(){
self.width = 10.0
self.height = 30.0
}
}
let sizeObj1 = Size(width: 2.0, height: 2.0)// error. argument passed to call that takes no arguments
let sizeObj2 = Size() // success.

Initializer Delegation for Value Types :

Initializers can call other initializers to perform part of an instance’s initialization. This process, known as initializer delegation.

struct Size {
 var width = 0.0, height = 0.0
}
struct Point {
 var x = 0.0, y = 0.0
}

struct Rect {
 var origin = Point()
 var size = Size()
 init() {}
 init(origin: Point, size: Size) {
   self.origin = origin
   self.size = size
 }

init(center: Point, size: Size) {
  let originX = center.x - (size.width / 2)
  let originY = center.y - (size.height / 2)
  self.init(origin: Point(x: originX, y: originY), size: size)
 }
}

Designated Initializers and Convenience Initializers :

Swift defines two kinds of initializers for class types to help ensure all stored properties receive an initial value.

Designated initializers
Convenience initializers

Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.
Every class should have at least one designated initializer.

Designated initializers for classes are written in the same way as simple initializers for value types:

Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer’s parameters set to default values. You can also define a convenience initializer to create an instance of that class for a specific use case or input value type.

class HumanBeing {
var name: String
init(name: String) {
self.name = name
}
convenience init() {
self.init(name: “not set”)
// Convenience init call the designated init method
}
}
let humanBeingObj1 = HumanBeing() // calls convenience init
let humanBeingObj2 = HumanBeing(name: “abhilash”) // calls designated init


Failable Initializers :

We cannot always assume that the initialization will always succeed for a struct, enum or class. It can fail for several reasons. It is sometimes useful to define a class, structure, or enumeration for which initialization can fail.

You write a failable initializer by placing a question mark after the initkeyword (init?).

You cannot define a failable and a nonfailable initializer with the same parameter types and names.

 In swift, the initializers won’t return anything. But objective -C does. In swift, You write return nil to trigger an initialization failure, you do not use the return keyword to indicate initialization success.

struct Animal {
let species: String
init?(species: String) {
if species.isEmpty { return nil }
self.species = species
}

}

Check :

let someCreature = Animal(species: "Giraffe")
// someCreature is of type Animal?, not Animal
if let giraffe = someCreature {
print("An animal was initialized with a species of \(giraffe.species)")
}

// Prints "An animal was initialized with a species of Giraffe"


Failable Initializers for Enumerations :

You can use a failable initializer to select an appropriate enumeration case based on one or more parameters. The initializer can then fail if the provided parameters do not match an appropriate enumeration case.

enum TemperatureUnit {
case kelvin, celsius, fahrenheit
init?(symbol: Character) {
switch symbol {
case "K":
self = .kelvin
case "C":
self = .celsius
case "F":
self = .fahrenheit
default:
return nil
}
}

guard let fahrenheitUnit = TemperatureUnit(symbol: "X") else {
print("This is not a defined temperature unit, so initialization failed.")
}

// Prints "This is not a defined temperature unit, so initialization failed."

Failable Initializers for Enumerations with Raw Values :

Enumerations with raw values automatically receive a failable initializer, init?(rawValue:), that takes a parameter called rawValue of the appropriate raw-value type and selects a matching enumeration case if one is found, or triggers an initialization failure if no matching value exists.

enum TemperatureUnit: Character {
case kelvin = "K", celsius = "C", fahrenheit = "F"
}
let fahrenheitUnit = TemperatureUnit(rawValue: "F")
if fahrenheitUnit != nil {
print("This is a defined temperature unit, so initialization succeeded.")
}
// Prints "This is a defined temperature unit, so initialization succeeded."


Propagation of Initialization Failure :

If your subclass is having a failable initializer which in turn call its superclass failable designated initializer, then if either of the initialization failed means the entire initialization process fails immediately, and no further initialization code is executed.

class Product {
let name: String
init?(name: String) {
if name.isEmpty { return nil }
self.name = name
}
}
class CartItem: Product {
let quantity: Int
init?(name: String, quantity: Int) {
if quantity < 1 { return nil }
self.quantity = quantity
super.init(name: name)
}
}

if let zeroShirts = CartItem(name: "shirt", quantity: 0) { // fails
print("Item: \(zeroShirts.name), quantity: \(zeroShirts.quantity)")
} else {
print("Unable to initialize zero shirts")
}
// Prints "Unable to initialize zero shirts"

if let oneUnnamed = CartItem(name: "", quantity: 1) { // fails
print("Item: \(oneUnnamed.name), quantity: \(oneUnnamed.quantity)")
} else {
print("Unable to initialize one unnamed product")
}
// Prints "Unable to initialize one unnamed product"


Required Initializers in swift :

Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.

You must also write the required modifier before every subclass implementation of a required initializer, to indicate that the initializer requirement applies to further subclasses in the chain. You do not write the override modifier when overriding a required designated initializer:

//required init
class classA {
required init() {
var a = 10
print(a)
}
}
class classB: classA {
required init() {
var b = 30
print(b)
}
}
//______________________
let objA = classA()
let objB = classB()
prints:
10
30
10

//______________________

class classC: classA { }
//______________________
let objC = classC() // prints 10 ..superclass init method gets called.


Note: You do not have to provide an explicit implementation of a required initializer if you can satisfy the requirement with an inherited initializer. In the above class classC , since we don’t have any stored properties unintialized at the time of initialization, we dont have to declare the required init explicitly.

When required init method is mandatory? What is that?

Ans :


Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.
You must also write the required modifier before every subclass implementation of a required initializer, to indicate that the initializer requirement applies to further subclasses in the chain. You do not write the override modifier when overriding a required designated initializer.
“The use of the required modifier ensures that you provide an explicit or inherited implementation of the initializer requirement on all subclasses of the conforming class, such that they also conform to the protocol.”

How we stop notification to be sent to device who is uninstalled?

Ans :

The push message service has a feedback channel which reports error messages when the application has been removed or the device no longer accepts the push messages.

"The Apple Push Notification service includes a feedback service to give you information about failed remote notifications. When a remote notification cannot be delivered because the intended app does not exist on the device, the feedback service adds that device’s token to its list. Remote notifications that expire before being delivered are not considered a failed delivery and don’t impact the feedback service. By using this information to stop sending remote notifications that will fail to be delivered, you reduce unnecessary message overhead and improve overall system performance."

How to iterate over all enum cases?

Ans : 

Swift 4.2's CaseIterable can help us avoid bugs and make our code more consistent when defining enum-keyed dictionaries. We'll start by making TextType conform to CaseIterable. Just like with Codable, Equatable and Hashable, we don't need to write any additional code ourselves - instead CaseIterable kind of acts as a marker for the compiler, telling it to synthesize an allCases collection for us:


enum TextType: CaseIterable {
    case title
    case subtitle
    case sectionTitle
    case body
    case comment
}


With the above change, we can now use TextType.allCases to access a collection of all cases in order, which we can then use to build up our fonts dictionary without the risk of missing a case, like this:


var fonts = [TextType : UIFont]()

for type in TextType.allCases {
    switch type {
    case .title:
        fonts[type] = .preferredFont(forTextStyle: .headline)
    case .subtitle:
        fonts[type] = .preferredFont(forTextStyle: .subheadline)
    case .sectionTitle:
        fonts[type] = .preferredFont(forTextStyle: .title2)
    case .body:
        fonts[type] = .preferredFont(forTextStyle: .body)
    case .comment:
        fonts[type] = .preferredFont(forTextStyle: .footnote)
    }
}


Definitely an improvement, since if we now add a new text type without a matching font definition - we'll get a compiler error 👍.