Saturday, 29 August 2015

Storyboard reference in iOS

Usage of Storyboard Reference


 1. First create 1 new storyboard name it anything you want(I named it `NewStoryboard`).
 2. Place your View controller(or any View Controller) in `NewStoryboard`.
 3. Give it a `Storyboard ID`(I have given `newVC`).

 4. Switch back to your main storyboard.
 5. Drag and drop `Storyboard Reference` control in this storyboard.
 6. Select the `Storyboard Reference` control and select `Attributes Inspector`.
 7. Select your new storyboard `NewStoryboard`.
 8. Provide Reference ID as `newVC`(which you used in `NewStoryboard` storyboard).
 9. Connect this `Storyboard Reference` via segue from ViewController. Now it will open this VC via segue without writing code. :)

 Limitation of Storyboard reference:-
1. Storyboard references deployed to iOS 9, OS X 10.10, and watchOS 1.
2. It requires Xcode 7.

More Info

Friday, 21 November 2014

UIScrollView with Autolayout

This is my second post in iOS tutorial series.

Yesterday I have to deal with UIScrollView in Autolayout, but I didn't found any good tutorial who will teach this, so I decided to write this tutorial.

Earlier without Autolayout, if UIscrollview contains controls with height more than UIScrollView then we have to set UIScrollview's contentSize to scroll the UIscrollView.

But in autolayout mode even if we set its contentSize the UIscrollView will not scroll. In Autolayout mode we don't have to set contentSize of UIScrollView and adopt a new way, so it will scroll without much effort.

Enough talking, now we will start the tutorial. In this tutorial all we have to do is with Storyboard and no code. :)

1) Let's create one project with Single View Application, save it in desired directory.

2) Now we have one view controller, let's add one UIScrollView from Object Library to the view present in view controller. The UIScrollView should fill whole view as shown in below figure.

3) Now we have to add constraints for this UIScrollView, click Pin and add top, left, right and bottom constraints as shown in below diagram.

4) This part is very important for scrolling of UIScrollView. Now we have to  add UIView inside UIScrollview. This view will act as container of other controls, and we will increase its height to add other controls. So let's add UIView inside UIScrollView.

5) Increase this latest UIView's height, I am increasing to 800.

6) Now add your controls wherever you want.

7) Now we have add constraints to UIView, click on Pin and set constraint for top, left, right, bottom, width and height.

8) Now we have to change Bottom space constraint of scrollview's descendant view. Change its negative value to 0 as shown in figure.

9) Now run your program and happy scrolling. This project is added on Github here

Friday, 7 November 2014

Local Notification in iOS8

Hi All,

This is my first blog post for iOS8. In this blog post I am going to create a simple Local Notification. In earlier version of iOS( upto iOS7 ), we don't have to ask permission from user whether creating LocalNotification or not. But from iOS8 onwards, we have to ask user for permission same as we are asking permission for Push Notification, Contacts, Gallery, etc...

Here we are going to create a demo application of how we have to ask for user permission for Local Notification and create a Local Notification.

1) First create a project in Xcode6 (iOS8 will only be available in Xcode6, if you have older Xcode please update it.), a Single View Application. Below is its screenshot.

2) Enter project name (whatever your project name), enter Organization Name, enter Organization Identifier, select Language (Swift OR Objective-C) (In this tutorial we are going to use Objective-C.), Click Next.

3) Save it wherever you want.

4) Now, we have to write code for asking user permission for Local Notification. Here I am writibf in AppDelegate's - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

Here is its Code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    // None of the code should even be compiled unless the Base SDK is iOS 8.0 or later
    // The following line must only run under iOS 8. This runtime check prevents
    // it from running if it doesn't exist (such as running under iOS 7 or earlier).
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    return YES;

6) Now run the project, it will ask for user permission.

7) Now we are going to create a Local Notification, go to ViewController.m's viewDidLoad method and write this code

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    localNotification.alertBody = @"Local Notification in iOS8";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

This simple code will create a LocalNotification that will fire after 1 minute of application launch. As soon as you launch the application press command + shift + H (to go to home screen), else you will not be able to view LocalNotification.

Note :- If you don't ask for user permission and try to create LocalNotification, compiler will generate a warning similar to this
Local Notification in iOS8[667:14153] Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7bf758e0>{fire date = Saturday, November 8, 2014 at 9:13:02 AM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, November 8, 2014 at 9:13:02 AM India Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts
and it will not create LocalNotification.

This demo project is uploaded on Github, you can download it from here.

If anyone have any question regarding this, they can write comments below.