Tuesday, 29 September 2015

Some Basic Terminology

Important Concept Need to Know

                             
Property:

Properties ensure that the instance variable of the class can be accessed outside the class. Properties begin with @property, which is a keyword 
=> Properties is followed with access specifiers, which are nonatomic or atomic, readwrite or readonly and strong, unsafe_unretained or weak. 
=> This varies based on the type of the variable. For any pointer type, we can use strong, unsafe_unretained or weak. Similarly for other types we can use readwrite or readonly. 

What is Keyword Atomic and NonAtomic in Objective-C ? 

Atomic Keyword will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. 
=> Properties are atomic by default . 
=> Atomic Keyword is Thread Safe. 
=> That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -- an autoreleased object, most likely -- will be returned to the caller in A. 

In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than "atomic". NonAtomic Keyword does not guarantees about thread safety. 
=> If thread A is calling the getter simultaneously with thread B and C calling the setter with different values, thread A may get any one of the three values returned -- the one prior to any setters being called or either of the values passed into the setters in B and C. Likewise, the object may end up with the value from B or C, no way to tell. 

What is NSSet ? 
An NSSet object represents a static, unordered collection of distinct objects. NSSet is immutable, so you cannot add or remove elements from a set after it’s been created. You can, however, alter mutable objects that are contained in the set. 

An NSSet can be created through the setWithObjects: class method, which accepts a nil-terminated list of objects. 

An NSSet instance can record any kind of Objective-C object, and it does not have to be homogeneous. 

What is NSArray and NSMutableArray ? 
NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. 

What is Synchronization ? 
Synchronization is defined as a mechanism which ensures that two or more concurrent processes or threads do not simultaneously execute some particular program segment known as mutual exclusion. When one thread starts executing the critical section (serialized segment of the program) the other thread should wait until the first thread finishes. 

If proper synchronization techniques are not applied, it may cause a race condition where, the values of variables may be unpredictable and vary depending on the timings of context switches of the processes or threads. 


Define NSThread ? 

An NSThread object controls a thread of execution.
Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the 

execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application, which handles user interface and event-related actions. Threads can also be used to divide a large job into several smaller jobs, which can lead to performance increases on multi- core computers. 

What is Retain Count or Reference Counting? 

Retain Count is the term which is related to the Manual Memory Management 

=> When we create a object, Objective C doesn't really care about the object we have created , the area of memory is claimed for this object and retain count is increased by one.
And whenever we release that object the retain count does go down by one . 


=>When the retain count of object reaches zero, Objective C blows that object ( memory occupied by object will be free ) and it will be ready to reclaim that memory for another Object. 

What you mean by Asynchronous and GCD in Objective-C ? 

Grand Central Dispatch, or GCD for short, is a C API that makes it exceptionally easy to perform asynchronous operations in iOS. 

Asynchronous operations are a fundamental part of every iOS app when you want to perform long operations without freezing or blocking the user interface. You can image that if your entire app froze without warning then your users would get quite irritated. 

With GCD, you can line up blocks of code in a queue for the system to execute as necessary. These blocks or operations, will be dispatched in the queue to another thread, leaving your main UI thread to continue its tasks. 

It’s important to know that GCD will use separate threads for it’s operations but which thread it isn’t important to you. All that matters is that your long running process in a separate thread outside the 
main thread where your UI and gestures are running. 

What are Blocks : 

=> Block is a chunk of code that can be executed at some future time. 

=> Blocks are first-class functions, which is a fancy way of saying that Blocks are regular Objective-C objects. Since they’re objects, they can be passed as parameters, returned from methods and functions, and assigned to variables. 

=> Blocks are Objective-C objects. Block objects are one type of C-level syntactic and runtime features. Blocks, in addition to executable code in standard C functions, they may also comprise variable bindings to managed (heap) or automatic (stack) memory. Therefore, a block can preserve a set of data that it can use to affect behaviour when executed. They can be added to collections like NSArray or NSDictionary. 

Override Getter and Setter Method : 

In Objective-C, we use @property to provide managed access to the state of a class. The system provides basic management rules like thread locking and reference retainment. To provide custom access logic,we need to write your own getter and setter methods. 
Let’s say that we have a Customer class and its name must always be in upper case. First, we declare the property in the interface: 

@interface Customer
{
@property (nonatomic) NSString* name;

}
 @end 

Next, we do the implementation. 
@implementation 

Customer @synthesize name = _name; //Must do this 

//Setter method
- (void) setName:(NSString *)n { 

NSLog(@"Setting name to: %@", n); 
_name =n; 


//Getter method
- (NSString*) name { 
NSLog(@"Returning name: %@", _name); 
return _name;
 } 

- (void) test {
self.name = @“Prashant ”; NSLog(@"Name is %@", self.name); 

}

 @end 

Here, we are declaring a member variable called _name that is mapped to the name property. The setter method is called setName. The getter method is just name. 

What is a .dSYM and How can we use it?

dSYM files store the debug symbols for your app
Services like crashlytics uses it to replace the symbols in the crash logs with the appropriate methods names so it will be readable and will make sense.
the benefit of using the dSYM is that you don't need to ship your App with it symbols making it harder to reverse engineer it and also reduce your binary size
in order to use it in order to symbolicate the crash log you need to drag the crash log into the device's device logs in the organizer of the machine that compiled the app binary (a machine that stores the dSYM)

Friday, 21 August 2015

IOS Design Pattern


 Design Pattern is an important Aspect of IOS Development, for writing an efficient and user friendly code we must follow an effective design within our program.
It not only increase code reusability but also enhance its efficiency.

In Short Lets start with what is Design Pattern ? 

Design patterns are reusable solutions to common problems in software design. 
They’re templates designed to help you write code that’s easy to understand and reuse. They also help you create loosely coupled code so that you can change or replace components in your code without too much of a hassle.

The most common Cocoa design patterns:

1. Creational: Singleton and Abstract Factory.
2. Structural: MVC, Decorator, Adapter, Facade and Composite.
3. Behavioral: Observer, Memento, Chain of Responsibility and Command.

Lets have a sort introduction about some major Design that we need to adopt while developing Application.


# Model View Controller (MVC):

 Model View Controller (MVC) is one of the building blocks of Cocoa and is undoubtedly the most-used design pattern of all. It classifies objects according to their general role in your application and encourages clean separation of code based on role.
The three roles are:

Model: The object that holds your application data and defines how to manipulate it. For example, in your application the Model is your Album class.

View: The objects that are in charge of the visual representation of the Model and the controls the user can interact with; basically, all the UIViews and their subclasses. In your application the View is represented by your AlbumView class.

Controller: The controller is the mediator that coordinates all the work. It accesses the data from the model and displays it with the views, listens to events and manipulates the data as necessary.

 Can you guess which class is your controller? 
That’s right: ViewController.

# The Singleton Pattern

The Singleton design pattern ensures that only one instance exists for a given class and that there’s a global access point to that instance. It usually uses lazy loading to create the single instance when it’s needed the first time.
Apple uses this approach a lot.

 For example: 
[NSUserDefaults standardUserDefaults]
[UIApplication sharedApplication]
[UIScreen mainScreen] 
[NSFileManager defaultManager]
all return a Singleton object.

# The Decorator Design Pattern
The Decorator pattern dynamically adds behaviors and responsibilities to an object without modifying its code. It’s an alternative to subclassing where you modify a class’ behavior by wrapping it with another object.

In Objective-C there are two very common implementations of this pattern: 

Category and Delegation.

Category
Category is an extremely powerful mechanism that allows you to add methods to existing classes without subclassing. The new methods are added at compile time and can be executed like normal methods of the extended class. It’s slightly different from the classic definition of a decorator, because a Category doesn’t hold an instance of the class it extends.

Delegation
The other Decorator design pattern, Delegation, is a mechanism in which one object acts on behalf of, or in coordination with, another object. For example, when you use a UITableView, one of the methods you must implement is tableView:numberOfRowsInSection:.
You can’t expect the UITableView to know how many rows you want to have in each section, as this is application-specific. Therefore, the task of calculating the amount of rows in each section is passed on to the UITableView delegate. This allows the UITableView class to be independent of the data it displays.

# The Observer Pattern
In the Observer pattern, one object notifies other objects of any state changes. The objects involved don’t need to know about one another – thus encouraging a decoupled design. This pattern’s most often used to notify interested objects when a property has changed.
The usual implementation requires that an observer registers interest in the state of another object. When the state changes, all the observing objects are notified of the change. Apple’s Push Notification service is a global example of this.
If you want to stick to the MVC concept (hint: you do), you need to allow Model objects to communicate with View objects, but without direct references between them. And that’s where the Observer pattern comes in.

Cocoa implements the observer pattern in two familiar ways:
Notifications and Key-Value Observing (KVO).

Notification :
Notifications are based on a subscribe-and-publish model that allows an object (the publisher) to send messages to other objects (subscribers/listeners). The publisher never needs to know anything about the subscribers.
Notifications are heavily used by Apple. 
For example, when the keyboard is shown/hidden the system sends a UIKeyboardWillShowNotification/UIKeyboardWillHideNotification, respectively.
 When your app goes to the background, the system sends a UIApplicationDidEnterBackgroundNotificationnotification.

Key-Value Observing (KVO)

In KVO, an object can ask to be notified of any changes to a specific property; either its own or that of another object.



Encryption and Decryption: 

Encryption is the process of translating plain text data (plaintext) into something that appears to be random and meaningless (ciphertext).

Decryption is the process of converting ciphertext back to plaintext. 

To encrypt more than a small amount of data, symmetric encryption is used.
A symmetric key is used during both the encryption and decryption processes. To decrypt a particular piece of ciphertext, the key that was used to encrypt the data must be used.

The goal of every encryption algorithm is to make it as difficult as possible to decrypt the generated ciphertext without using the key. If a really good encryption algorithm is used, there is no technique significantly better than methodically trying every possible key. For such an algorithm, the longer the key, the more difficult it is to decrypt a piece of ciphertext without possessing the key. 
OS X and iOS provide a number of encryption technologies. Of these, three APIs are available on both iOS and OS X: 
Keychain Services API—provides secure storage for passwords, keys, and so on
Cryptographic Message Syntax—provides (nonstreaming) symmetric and asymmetric encryption and 

decryption
Certificate, Key, and Trust Services—provides cryptographic support services and trust validation 


What is dispatch_once ? 

The behavior of dispatch_once is in the name. It does something once and only once. 
It takes two parameters. The first is a predicate that tracks the "once". The second is a block to execute on the first call. Calls look like this: 
static dispatch_once_t predicate; dispatch_once(&predicate, ^{ 
// some one-time task 
});
It's a great API for lazily initializing shared state, whether it's a global dictionary, a singleton instance, a cache, or anything else that needs a bit of setup the first time through. 

Wednesday, 13 May 2015

Apple InAppPurchase

In App Purchase in IOS :

Types of In App Purchase
There are three types of the products you can create:
# Consumable
# Non-Consumable
# Subscription

Consumable
Consumables are In-App Purchases that must be purchased each time the user needs that item. Consumable In-App Purchases are available for both iOS and OS X apps. 
Some examples of Consumable purchase types are: 
• Supplies in a game (ammunition, health points, cheats, extra lives, etc) 
• Accelerators used to decrease advancement time within an app 

Non-Consumable. 
Non-Consumables are In-App Purchases that only need to be purchased once by the user and are available to all devices registered to a user. This purchase type is used for services that do not expire. Non-Consumable In-App Purchases are available for both iOS and OS X apps. 
Non-Consumable content may be bundled in your app’s binary when you submit to the app store, or it may be downloaded after the user makes the purchase. 
Purchase can be done only once. This type of the purchase is used to unlock new themes, additional levels and so on.

Subscription : There are different type of subscription Used such as 

# Auto-Renewable Subscriptions : Auto-Renewable Subscriptions allow the user to purchase episodic content or access to dynamic digital content for a set duration time. At the end of each duration, the subscription will renew itself, until a user opts out. The Auto-Renewable Subscription In-App Purchase type is available for both iOS and OS X apps. 

Examples of Auto-Renewable Subscription purchase types are: 
• Recurring delivery of newspapers or magazine issues 
• Monthly subscription to audio or video streaming feed February 3, 2014 
• Weekly membership to a dating service
 • Business app providing cloud storage services 

# Free Subscriptions : Free Subscriptions are an extension of Auto-Renewable Subscriptions that permit the delivery of free subscription content to Newsstand-enabled applications. The Free Subscription In-App Purchase type is implemented in the same way as an Auto-Renewable Subscription, just without any charges to the user. Free Subscriptions do not have expirations, but the user can turn off the subscription at any time. 

# Non-Renewing Subscriptions: Non-Renewing Subscription allow the sale of services with a limited duration. Non-Renewing Subscriptions must be used for In-App Purchases that offer time-based access to static content and are only available for both iOS and OS X apps. 

Examples of Non-Renewing Subscription purchase types are: 
• One week subscription to voice guidance feature within a navigation app 

• Annual subscription to online catalog of archived video or audio