Adding a side menu to your IOS App – RESideMenu Storyboard Tutorial Part 2

This is continuation from Part 1.

We have previously created the Storyboard, now we need to configure some settings.

RootViewController.h – Add the following codes

RootViewCodes

#import "RESideMenu.h"

@interface RootViewController : RESideMenu <RESideMenuDelegate

RootViewController.m – Add the following codes

- (void)awakeFromNib
{
    self.menuPreferredStatusBarStyle = UIStatusBarStyleLightContent;
    self.contentViewShadowColor = [UIColor blackColor];
    self.contentViewShadowOffset = CGSizeMake(0, 0);
    self.contentViewShadowOpacity = 0.6;
    self.contentViewShadowRadius = 12;
    self.contentViewShadowEnabled = YES;
    
    self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentViewController"];
    self.leftMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"left"];
    self.backgroundImage = [UIImage imageNamed:@"Stars"];
    self.delegate = self;
}

#pragma mark -
#pragma mark RESideMenu Delegate

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willShowMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"didShowMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

- (void)sideMenu:(RESideMenu *)sideMenu willHideMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"willHideMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

- (void)sideMenu:(RESideMenu *)sideMenu didHideMenuViewController:(UIViewController *)menuViewController
{
    NSLog(@"didHideMenuViewController: %@", NSStringFromClass([menuViewController class]));
}

Take note of the leftMenuViewController in the awakeFromNib method. We going to set those identifier to our storyboard’s viewcontroller.

Storyboard – RootViewController

Set the class to RootViewController.

SetClassforRootView

Storyboard – NavigationViewController

Set the StoryboardID to contentViewController.

 

SetNavigationControllerStoryboardID

 

Storyboard – HomeViewController

  1. Set both the Class and Storyboard ID.

SetClassandIDforHomeController

2. Now add a UIBarItem to your navigation.

BarButtonItem

3. Link the UIBarItem with the presentLeftMenuAction.

Linkthemup

 

Now you are good to test! Ping me up if you have issue.

Adding a side menu to your IOS App – RESideMenu Storyboard Tutorial – Part 1

In this tutorial, we are going to cover some of the quick and easy way of adding a side menu to your iOS app.

Basic Housekeeping

Firstly, let me introduce you to http://www.cocoacontrols.com, this is repository that provides a bunch of custom controls that are designed for iOS and Mac OSx. Sometimes we should not reinvent the wheel, but harness the power of the internet and community around.

Next, we are going use cocoa pods, if you are not sure what that is, head over to the following url -> Raywenderlich’s intro to cocoapod

So I have choose, RESideMenu, an amazing and beautiful control. GitHub – RESideMenu.

Getting Started – Adding RESideMenu to your project

  1. Adding the library into your project, if you have a pod file add the following:
    pod 'RESideMenu', '~> 4.0.7'
    
  2. Within your Terminal, browse to your XCode project location and type the following:
    pod install
  3. Xcode should generate a xcworkspace extension for you. Moving forward you should be only using this file.

Getting RESideMenu to work

Let create the following files in your project.

  1. RootViewController – Add New File -> Cocoa Touch Class
  2. HomeController – Add New File -> Cocoa Touch Class

The above should inherit UIViewController.

Next in your Main.Storyboard, add the following:

  1. UIViewController
    Searching for UIViewController
  2. UINavigationController

It should look like below after you are done.

Storyboard

In the next post, I am going to show you what we need to configure.

 

 

Using Reachability to test for Internet Connectivity IOS

Any apps that you build today will require some form of internet connectivity.

Today’s app is getting more and more intuitive and should have the ability to check on the internet connective and prompt the user of the app.

To do that we will be using the following library, Reachability. https://github.com/tonymillion/Reachability

There are 2 ways to setup the app in your Xcode project.

Part 1 : Getting Reachability to your project

1. Manual

1.1 Check out a copy from the above github url, add the Reachability.h/.m file to your project

2. Cocoapod

2.1 Add the following line to your pod file, pod ‘Reachability’

2.2 Run pod install on terminal.

 

Part 2: Setting up the SystemConfiguration Framework.

1. Go to the Project -> Targets -> Build Phases tab.

2. Add SystemConfiguration.Framework into your project.

 

Part 3: Codes to set up Reachability to test internet connectivity.

AppDelegate.h 

Import the Reachability.h, at the very top of the file.

#import “Reachability.h”

AppDelegate.m

Within the method,

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Add the following codes

 Reachability *reachability = [ReachabilityreachabilityWithHostname:@”http://localhost:3000/”];

    

    reachability.reachableBlock = ^(Reachability *reachability) {

        NSLog(@”Network is reachable.”);

    };

    

    reachability.unreachableBlock = ^(Reachability *reachability) {

        NSLog(@”Network is unreachable.”);

    };

    

    // Start Monitoring

    [reachability startNotifier];

Xcode & Three20 Framework

This article will cover the basic of adding the Three20 framework to your Xcode 4 solutions.

A little background of this particular framework, quoting from the source.

Three20 is a open source Objective-C library used by dozens of well-known brands in the App Store, including Facebook, Posterous, Pulse, Meetup.com, and SCVNGR. Three20 provides powerful view controllers such as the Launcher, the popular Photo Browser, and internet-aware tables.

Another post will be written to discuss about the features of the framework, Three20. But for now let’s us get started on adding this the Three20 to your Xcode 4 project.

Three20 source code could be downloaded from the following link. www.three20.info

I do strongly suggest that you download the tar.gz version.

  1. Start by extracting the content of the file you downloaded from the site.Again, I suggest that you should extract the file on the desktop.
  2. Locate the file ttmodule.py at that paticularly path three20/src/script/

    xcode4 three20 facebook
    xcode4 three20 facebook

     

  3. Open Terminal,
    Enter: python
  4. Drag ttmodule.py into the terminal window,
    Enter: -p
  5. Locate your xcode project, Drag the .xcodeproj file into the Terminal.
    Enter: Three20
  6. Check that the Terminal Window should now look something like this
    > python /Users/xxx/Desktop/three20/src/scripts/ttmodule.py -p /TestingProject/TestingProject.xcodeproj Three20 
    Press Enter.

 This will add the Three20 framework into the xcode project and you can find out more on how to use the following at the following site.

Popovercontroller for Iphone

I was searching for a good community control for the popoverviewcontroller seen on the ipad.

I present to everyone, .

It was so simple to get started with the above and you can remove the complexity in the developing customize UI and get along with your actual development work.

Steps to get it working.

1. Add the following files

  • FPPopoverController.h
  • FPPopoverController.m
  • FPPopoverView.h
  • FPPopoverView.m
  • FPTouchView.h
  • FPTouchView.m
  • ARCMacros.h

2. Add the following codes in your IBAction event.

ViewController *content = [self.storyboard instantiateViewControllerWithIdentifier:@"home"];


FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:content];
popover.arrowDirection = FPPopoverNoArrow;


[popover presentPopoverFromPoint:CGPointMake(self.view.center.x, self.view.center.y - popover.contentSize.height/2)];

Lastly, do check out the demo included in the git checkout!

UITableView Prototype Cell with UIButton

With the introduction of iOS 5, UITableview now comes with Prototype Cell.

For those who work with anything before this, you will know that creating a customized tableview cell take alot of effort.

Today, I am going to show 2 method of getting the instance of the button that is clicked.

Method 1 is the “downright dirty” way of doing things. Method 2 is much “cleaner”.

Method 1:

In the

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

We add the uibutton to the cell and add the indexpath.row to the tag of the UIButton.

UIButton *newButton = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 100, 50)];

newButton.tag = indexPath.row;

[newButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:newButton];

By adding the indexPath.row to the tag of the button allow us to determine which row in the UITableview the user selected.

Therefore in the ButtonPressed Method we can do the following.

-(void) buttonPressed:(id) sender{

//We load the data from a NSArray name array_data

[array_data objectAtIndex:[sender tag]];

….}

As you can see from the above we can therefore know which data is selected and the application can progress forward.

Method 2:

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @”Cell”;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }    

    UIButton *btn = (UIButton *)[cell viewWithTag:1];

    [btn addTarget:self action:@selector(onClicked:) forControlEvents:UIControlEventTouchUpInside];

}

The above is similar to the first method. Do note that the cell viewWithTag this have been already added to the Prototype cell in the Storyboard.

The tag can be set via the Storyboard, on the right of the Xcode, 3 tag from the right. The Tag Field.

Now in the onClicked Method.

-(void) onClicked: (id) sender{

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];

    NSIndexPath *clickedButtonPath = [self.tableview indexPathForCell:clickedCell];

  }

In the clickedButtonPath, this allow us to find out the row that was selected by the user.

Summary

The 2 method above shows the different way of doing things, each have its pro and con.

In method 1, by setting the tag of the uibutton we remove certain customising ability should we want to change the color of the uibutton programmatically at a later stage.

In method 2, we have to know the view to cast it as a UITableViewCell and the wrong view will cause us to hit EXE_BAD ACCESS error.

Thanks for the read

Local Notification

There are 2 type of Notification in IOS.

Apple’s definition of the differences and when to use what,
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1

Don’t confuse yourself with the 2 basically, Push Notification will require to set up the communication link to Apple’s Push Server and the way the Push Server works it have its own issue.

I have attached a guide that I used to set up APN previously.
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

Create your First Local Notification:

UIApplication *iosApp = [UIApplication sharedApplication];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

NSTimeInterval timeZoneDiff = 8 * 60 * 60;

NSTimeInterval bug = 30 * 60;

NSDate *notifDate = [alertDate dateByAddingTimeInterval:[alertTime timeIntervalSince1970] + timeZoneDiff – bug];

localNotification.fireDate = notifDate;

localNotification.timeZone = [NSTimeZone systemTimeZone];

localNotification.alertBody = [NSString stringWithFormat:@”Alert : %@”, contentData];

localNotification.soundName = UILocalNotificationDefaultSoundName;

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@”%d”, uniqueId] forKey:@”id”];

localNotification.userInfo = dictionary;

[iosApp scheduleLocalNotification:localNotification];

Add the following method into your AppDelegate.m file. This method will be called when the user click on the View button when the notification pops up.

 – (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

Now, the hard part, if you have a navigation controller in your application on clicking on the View Button most of the developers will just use the following

[(UINavigationController *) self.window.rootViewController pushViewController: <targetController> animated: TRUE];

Example:

-ViewA
-HomeView

By doing the above command, if we push another View A to the stack, the stack will look like the following:

-View A (new)
-View A
-HomeView

This will not be the result we want. So we need to clear all the view and make sure that we only have the HomeView (landing view) and the targetView.

In the applicationDidReceiveLocalNotification method,

We will declares the views we need:

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@”MainStoryboard” bundle:nil];

ViewA *vcA = (ViewA *)[storyboard instantiateViewControllerWithIdentifier:@”ViewA”];

HomeView *home = (HomeView *)[storyboard instantiateViewControllerWithIdentifier:@”home”];

UINavigationController *nav = (UINavigationController *) self.window.rootViewController;

nav.viewControllers = [NSArray arrayWithObjects:home,vcA, nil];

[(UINavigationController *)self.window.rootViewController popToViewController:vcA animated:TRUE];

As you can see from the above codes, we need to init the storyboard object as in the AppDelegate its still not created yet.

Then we declare the respective view, note you need to import the headers files.

Finally we add this views into the stack of the navigationController.

Instead of using push, we are using pop here. The reason for this is that push adds a new view to the stack, where as pop show something that is currently in the stacks.

I hope this help you guys in anyway possible and do drop me a comment if you need further clarification.

SenTestingKit not loaded, Image not found

After moving from my old macbook pro (2009) to the new macbook pro (2012) on the Xcode Version (4.5.1), i tried running my app in the iphone simulator but was faced with this error.

dyld: Library not loaded: @rpath/SenTestingKit.framework/Versions/A/SenTestingKit

I am pretty sure that it was working perfectly in the old macbook pro, therefore i set out to google and see that alot of people actually hit this issue but there wasn’t any clear cut answer.

To solve this basically, expand your solution -> Frameworks -> Remove the SenTestingKit from your project. This should resolve the issue.

Note : Please remove reference not delete it.

Xcode’s Neat Feature: “SVN”

Been working with Xcode for more than a year now, I just wanted to highlight the following neat feature available in Xcode.

The ability to take snapshot and revert this back to the way it was before.

If you work with Virtual Machine, you will appreciate this function and understand how valuable it will be.

If not let me explain, take for example you made a mistake in your development deleting some really important codes away and you can’t undo. You will either find from a backup hdd or try to rewrite it again. But with snapshot you will not need to do either of the above (provide you dont lost the development due to a dead hdd).

Creating a Snapshot

Instruction to take a snapshot:

In Xcode, File -> Create Snapshot

This will save a snapshot of the current state in the Xcode’s Organizer.

Restore the Snapshot

Restore the snapshot by going to the following:

In Xcode, File -> Restore Snapshot

Managing the Snapshots

Anyone who have publish an app or work extensively will be aware of Xcode’s Organizer, but if you dont, no worries.

In Xcode, Window -> Organizer -> Select Projects (this is the third icon out of the five on the window)

On the right of the screen, you will see the project and you can delete or export the snapshot.

I am linking the official documentation from apple. Click ME

Ending…

Ending this off, if you are not the only person working on the project, I recommend you go the commercial route which is the SCM. Eg. SVN or GitHub or TFS

But if you are just like me working on weekend project, do explore this snapshot features that comes along with the XCode.