Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Friday, August 28, 2015

Whats new in iOS 9

In this article I will cover some of the major features and functionalities of iOS 9 and what are the main aspects we should keep in mind so as to make our apps iOS 9 compatible one.

Existing apps will function normally in iOS 9, Ideally Apple will take care of it; which is nothing but “Forward Compatibility”, as it was managing this before in its earlier version releases.

If you want to release an update of your existing app or you are going to release a brand new iOS app, in which you wish to make use of iOS 9 features and make your app compatible with iOS 9 then you need to take care of few stuffs, 

Prerequisites for iOS 9: Xcode 7; iOS 9 SDK is compatible with Xcode 7, So the first and foremost thing any one wants to do is download Xcode 7 from developer.apple.com and start using iOS 9.0 SDK.

Before going in to details here is a quick snapshot on crucial points on iOS 9.0,

- Multitasking Enhancements for iPad
- App Transport Security(Adopt/Migrate your web-services to HTTPS asap)
- App Thinning
- Search
- Support for Right-to-Left Languages
- Contacts and Contacts UI
- UI Testing in Xcode 7

1) Multitasking Enhancements for iPad:
iOS 9 enhances the user’s multitasking experience on iPad with Slide Over, Split View, and Picture in Picture

- The Slide Over(below is the reference image) feature lets users pick a secondary app and quickly interact with it.


- The Split View(below is the reference image) feature gives users the ability to use two apps side by side on iPad, both the apps were responsive for user interaction and will run in parallel. 



- The Picture in Picture feature (also known as PiP - below is the reference image) lets users watch video in a window that floats above other onscreen apps.
Picture in Picture feature is for apps whose primary role is video playback.


Apple recommends for existing apps to adopt multitasking enhancements of iOS9 on iPad,If you are releasing any new iPad app or releasing an update for the existing app, Its better that if you adapt your apps for multitasking since users will be expecting all apps with Slide Over, Split View, and Picture in Picture features of iOS 9.

Note: For apps which you are planning to support multitasking enhancements make sure that you are supporting all the interface orientation.” You need to explicitly add the  UIRequiresFullScreen key to our Xcode project’s Info.plist file and apply the Boolean value YES from next update/new release.”

- SplitView functionality will work only from iPad Air 2 and above(with upcoming iPad devices).
- SlideOver and Picture in Picture functionality will work on iPad Mini 2,iPad Mini 3,iPad Air,iPad Air 2 (and with upcoming iPad devices).

Reference for Implementation:

2) App Transport Security

App Transport Security is a feature that requires secure connections between an app and web services. 

Here is a note from Apple mentioned on developer site: “You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.”

“If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app's Info.plist file.”

Reference for Implementation:

So go ahead instantly and migrate your web-services to HTTPS, if you are developing new services then by default opt for HTTPS services.

For a temporary fix or to bypass App Transport Security in your app, refer http://just-works.blogspot.in/2015/08/how-to-make-http-services-work-on-ios-9.html


3) App Thinning

The App Store and operating system optimize the installation of iOS apps by tailoring app delivery to the capabilities of the user’s particular device, with minimal footprint. This optimization, called app thinning.

If you adopt App thinning within your apps, then apps will occupy minimum disk space, faster downloads from App store.

App Thinning includes three components: slicing, bitcode, and on-demand resources.

- In general Slicing is recommended as a best practice for every app as-well as developer,
Since Slicing will check the user’s iOS device and based on that specific device it will send a specific installation package(unnecessary resource files will not be included in the package during download process(.ipa))
Below is a reference image(pictorial representation) on how App Thinning will work,



- Bitcode is an intermediate representation of a compiled program. Apps you upload to iTunes Connect that contain bitcode will be compiled and linked on the App Store. Including bitcode will allow Apple to re-optimize your app binary in the future without the need to submit a new version of your app to the store.

Note: For iOS apps, bitcode is the default, but optional. If you provide bitcode, all apps and frameworks in the app bundle need to include bitcode. 

- On-demand resources are resources—such as images and sounds—that you can tag with keywords and request in groups, by tag. The App Store hosts the resources on Apple servers and manages the downloads for you. On-demand resources enable faster downloads and smaller app sizes, improving the first-time launch experience. For example, a game app may divide resources into game levels and request the next level of resources only when the app anticipates that the user will move to that level. Similarly, the app can request In-App Purchase resources only when the user buys the corresponding in-app purchase.
Below is a reference image(pictorial representation) on how On-demand resources will work,


Reference for Implementation:

4) Search

Search in iOS 9 gives users great new ways to access information inside of your app, even when it isn’t installed. 
If you adopt iOS 9 Search feature in your apps, it helps you to increase the usage of your app and improve its discoverability by displaying the app content(description of the app) when users search across the system and on the web; even when your app is not installed in users device.

Reference for Implementation:
iOS 9 introduces the following APIs to adopt Search:



5) Support for Right-to-Left Languages

iOS 9 brings comprehensive support for right-to-left languages, which makes it easier for you to provide a flipped user interface.

Arabic, Persian, Urdu are few of the examples for Right to Left languages.

Reference for Implementation:

6) Contacts and Contacts UI

iOS 9 introduces the Contacts and Contacts UI frameworks (Contacts.framework and ContactsUI.framework), which provide modern object-oriented replacements for the Address Book and Address Book UI frameworks.

Reference for Implementation:


7) UI Testing in Xcode 7

Find and interact with UI elements, Validate the UI properties and state, UI recording.
- Xcode 7 introduces UI testing as a major new feature of the existing XCTest framework.
- Now from Xcode 7 and iOS 9 their is a native support for both Unit testing and UI testing.

Reference for Implementation:

Go ahead and explore and make use of this amazing native UI Automation testing support for your apps from Apples Xcode.

For more information & additional new features of iOS 9 refer Apple's official documentation here, https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS9.html

Hope this post is helpful, any comments or suggestions are acceptable and appreciated.

Tuesday, December 31, 2013

UDID equivalent in iOS

From iOS 5 and above Apple deprecated uniqueIdentifier (accessing UDID of iOS device) method, I hope for some sort of security reasons. In iOS 7, Apple removed uniqueIdentifier property itself. So apps that requires a user specific id so as to run needs an alternative, here is one alternative way to get a unique identifier called GUID.

GUID is an acronym for 'Globally Unique Identifier'. GUID is also known as UUIDs(Universally Unique Identifiers). It is a 128-bit integer number used to identify resources.

Below code snippet (method) will generate a GUID in objective C - ARC version, which can run in any of iOS deployment target

+ (NSString *)generateGUID
{
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
    CFRelease(uuid);
    return (__bridge NSString *) uuidStr;
}

Additional available alternative properties:

From iOS 6.0 and above, identifierForVendor property is available. 

Pros and Cons:

Pros:
GUID’s can be generated in offline.

Cons:
       Each time when we try to invoke this method it will return a different unique key, which is based on some specific point of time.

For more information on UIDevice properties you can visit Apples developer document in the following url https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/identifierForVendor


Friday, November 22, 2013

What is @synchronized directive in Objective C?

In a multithreaded environment if more than one thread tries to access same memory address may cause a Race Condition”, to avoid such kind of conditions you should use “Mutex Lock(Mutual Exclusion)” nothing but blocking or restricting or locking n number of threads to access same memory address or content at a same point of time and allowing only one thread at an instance of time.
This can be achieved in Objective C by using @synchronized directive.

Example:
Generally while implementing Singleton design pattern or class you will see some kind of code snippet like below in any iOS projects,

+(id)getSingletonInstance
{
    @synchronized(self)
    {
        if (singletonObj == nil)
        {
            singletonObj = [[self alloc] init];
        }
        return singletonObj;
    }
}

In the above example if suppose at a particular instance of time, if two different class tries to call getSingletonInstance method then if you don’t manage thread safety then a Race Condition will occur, so as to avoid such kind of exceptional conditions you need to use @synchronized directive.
Here whenever a thread reaches this line of code, @synchronized directive will check whether any other thread is already accessing self, if some thread is already accessing it will just block the current thread until and unless the processing thread will finishes its task.

At this point you might be thinking that why do we need to use threads? & why we want to manage threads?
Here is the answer,
- Basically a thread is an instance of a Process, A process may have n number of threads (we can say threads are children’s of a process)

why do we need to use threads?
In Software Programming we need to take care of CPU resources optimizations so that the performance of our application can be increased. If we consider any iOS application basically there will be a main thread for each application where exactly the app starts and all the UI manipulations has been taken care by this main thread.

In this kind of environment suppose say an example, if you are downloading an image from a remote server through a web-service (ex: REST) call, then if you perform this task on main thread then until and unless you get the response from the server your UI will become hang, so always this kind of asynchronous tasks should be handled by separate threads so as to utilize CPU, system resources properly and to increase your apps performance.

why we want to manage threads?
- Every thread of a process has equal access to your application resources to fetch, modify or manipulate.

- Each thread will not make sure that how long it will run and when it will finishes its task.
So it’s a programmer's responsibility or the one who creates a thread its his/her responsibility to manage threads.

Advantages of using multiple threads
- Use of multiple threads will reduce CPU waiting time.

For additional information on synchronization you can refer Apple’s official documentation in following URL https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW1


Hope this is helpful, any comments or suggestions are acceptable.

Thursday, November 21, 2013

iOS App life cycle

Today will see the iOS App life cycle, mainly on
1) The App Launch Cycle
2) The View Controller Life Cycle

1) The App Launch Cycle

Lets see as soon as you tap on any application icon to open any iOS app in your device springboard what will happen,

- As part of the launch cycle, the iOS system creates a process and main thread for your app and calls your app’s main function on that main thread.

- The default main function that comes with your Xcode project promptly hands control over to the UIKit framework, it will initialize your app and prepares it to run.

Below flowchart (diagram from Apple’s developer docs: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/Art/app_launch_fg_2x.png) will show the sequence of events that occurs when an app is launched into the foreground


The main function’s main job is to hand control to the UIKit framework.

2) The View Controller Life Cycle

Now lets see once your app is launched then say your first screen (Root/Home View Controller), how it will load in to the screen in other words the view controller life cycle hierarchy,
If you are creating your UI or View programmatically then the first method which gets called is loadView, followed by viewDidLoad -> viewWillAppear -> viewDidAppear  and while moving to the next screen or view Controller the hierarchy of methods are like viewWillDisappear -> viewDidDisappear -> viewDidUnload  

Note: In iOS 6 and later, viewDidUnload method is deprecated since views are no longer purged under low-memory conditions.

viewDidLoadCalled only once during the initial load of interface builder(.xib) file. Initial set up of view controller can be taken care here.

viewWillAppear – This method gets called just before your view appears or renders on screen every time when you navigate or switch between different views. Suppose if you want to update your view different while switching back and forth based on some requirement for that kind of dynamic changes to the views can be done here.

viewDidAppear – Once your view loads completely on screen this method gets called and here you can handle further UI or functionalities w.r.t your view controller like database call or whatever you want to do after loading your view.

viewWillDisappear - This method is called before the view is actually removed and before any animations are configured. Notifies the view controller that its view is about to be removed from a view hierarchy.

viewDidDisappear - This method notifies the view controller that its view was removed from a view hierarchy, here you can perform additional tasks associated with dismissing or hiding the view.

viewDidUnload - Called when the controller’s view is released from memory. In iOS 6 and later, clearing references to views and other objects in your view controller is unnecessary. In iOS 6 and later, viewDidUnload method is deprecated.


 Hope this post is helpful,any comments or suggestions is acceptable.



Tuesday, November 19, 2013

The most important design patterns in iOS

Today lets see the most important basic design patterns in iOS,

design pattern solves a common software engineering problem. Patterns are abstract designs, not code. When you adopt a design, you adapt its general pattern to your specific needs. No matter what type of app you’re creating, Understanding design patterns helps you use frameworks more effectively and allows you to write apps that are more reusable, more extensible, and easier to change.

The most important design patterns you must know are
1) MVC (Model View Controller)
2) Target Action
3) Delegation
4) Blocks

1) MVC (Model View Controller)
In this pattern, model keep track of your app’s data, views display your user interface and make up the content of an app, and controllers manage your views. 
Example:
In an iOS app, your Interface builder(.xib) files are views, your interface(.h) and implementation(.m) file is a Controller and your database (CoreData or SQLite or SQL Server) is nothing as a Model.

Advantages:
This pattern separates the data objects in the model from the views used to present that data. This separation promotes code reuse by making it possible to swap out your views as needed and is especially useful when creating universal apps—that is, apps that can run on both iPad and iPhone.

2) Target Action
Target-action is a conceptually simple design in which one object sends a message to another object when a specific event occurs.

Example:
Consider an UIButton in a view say Login button on a UIView, on TouchUpInside event you are calling a selector method where you actually perform login action, here the Target is generally self or whichever the controller(XYZViewController) is handling the action and Action is Login in this example.

3) Delegation
Delegation is a simple and powerful pattern in which one object in an app acts on behalf of, or in coordination with, another object. 

The delegate of a framework class is automatically registered as an observer of notifications posted by the delegating object.The delegate need only implement a notification method declared by the framework class to receive a particular notification message.

Example:
Consider UITableView class where didSelectRowAtIndexPath: is a delegate method of UITableView,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method tells the delegate that the specified row is now selected. The delegate handles selections in this method

Attribute tableView: A table-view object informing the delegate about the new row selection.

Attribute indexPath:An index path locating the new selected row in tableView.

By the help of this delegate method of UITableView, you will come to know which row of your table user has selected if your table is adopting and confirming to UITableView protocol, i.e; <UITableViewDelegate> in your interface(.h) file and yourTableViewObj.delegate = self; in your implementation(.m) file.

Advantage of this delegate method is No pain for a programmer to get the user selected row index.

4) Blocks
Blocks are a language-level feature, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary. They also have the ability to capture values from the enclosing scope,

Example:
The syntax to define a block literal uses the caret symbol (^), like this:
1)^{
NSLog(@”This is a block”);
    };
In the above example, the block doesn’t return any value, and doesn’t take any arguments.

2) use the operator to declare a block variable and to indicate the beginning of a block literal. The body of the block itself is contained within {}, as shown in this example
int multiplier = 4;
int (^myBlock)(int) = ^(int num) {
    return num * multiplier;
};

the block is able to make use of variables from the same scope in which it was defined.
In the above example myBlock is a block name, which returns an integer type, it takes an single argument of type int, num is an argument name and stuffs inside flower braces’{}’ is the body of the block and a block always ends with a semicolon(;).
Advantages:
- A block is commonly used in place of a call back function.
- It saves you having to create your own threads and maintain the state of each thread, manage locks & etc.
- Blocks can be used by multiple threads.


In addition to the above mentioned design patterns iOS is having even more design patterns like Abstract Factory,Adapter,Chain of Responsibility,Command,Composite,Decorator,Facade,Iterator,Mediator,Memento,Observer,Proxy,Receptionist,Singleton to know about these additional design patterns you can go through this Apples Official documentation https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW6

Hope this post was helpful, any comments or suggestions is acceptable.