Tuesday, 23 April 2013

iCloud Backup alert problem in iOS device - resolution !

If you're seeing this alert message in your device, which is not going away and sticking on screen then here's the solution.

It won't work even if you will try to turn off the device. So, all you need to do is,

* Hold on the home screen button with "turn off" switch for around 10 seconds till Apple logo appears and then it will work okay ! (what we do to take a screenshot for ten seconds)

* Note : Don't worry, this reset won't erase any of your data.

Monday, 22 April 2013

How to set default text or hint in UITextField ?

In textfields, sometimes we need to show some default message which is a hint which disappears when gets focused or is in editing mode. So, for this do you the placeholder property of textfield.







For this just write the text you want to show in the field placeholder in property window.



Password and Confirm Password in UITextField Xcode

You can write the code below on your button touch event of either sign up or login. This will check whether the strings/passwords written in both UITextFields are same or not and will prompt an alert message :

iif ([self.pwd.text isEqualToString:self.confirmpwd.text])
    {
        UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Message" message:@"successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
        
        [alert show];
    }
    
    else
    {
        UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Message" message:@"Password mismatch" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alert show];
        
    }

Password validation in UITextField

Code for password validation is as below, written in textFieldDidEndEditing method of UITextField delegate :

*viewcontroller.h
Example :

@interface SignUpViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic,retain)IBOutlet UITextField *pwd;

*viewcontroller.m

@synthesize pwd;

- (void)textFieldDidEndEditing:(UITextField *)textField
{


   if (textField == pwd)
    {
        int numberofCharacters = 0;
        BOOL lowerCaseLetter,upperCaseLetter= 0;
        if([pwd.text length] >= 10)
        {
            for (int i = 0; i < [pwd.text length]; i++)
            {
                unichar c = [pwd.text characterAtIndex:i];
                if(!lowerCaseLetter)
                {
                    lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
                }
                if(!upperCaseLetter)
                {
                    upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
                }
                /*if(!digit)
                {
                    digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
                }
                if(!specialCharacter)
                {
                    specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
                }*/
            }
            
            if(lowerCaseLetter && upperCaseLetter)
            {
                //do what u want
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Please Enter at least 10 password"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    }
}

Email validation in UITextField

Create a function for validating email like the code below :


- (BOOL)validateEmail:(NSString *)emailStr 
{

  NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
   NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailStr];

}

UITextField delegate has a method textFieldDidEndEditing, write the code below to display an alert message when email is not valid :

if (textField == email)
    {
        
if(![self validateEmail:[email text]])
        {
            // user entered invalid email address
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter a valid email address." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
           
            //email.text=@"";
        }
    }

How to use UINavigationViewController in Xcode ?

Hello World, 

I thought that navigation is one of the useful thing we do, so thought for a little help making a tutorial. So, here's how we do it :

If you are using storyboard then there's no need of this but if you are going through xib then learn using this method.

So, first of all do include the below code in your appdelegate.m file otherwise your navigation would never work :




Now, on a button touch event, we perform the navigation to the another screen. So, I've created an object of secondview here.

Watch the video to understand it well :)

Sunday, 14 April 2013

How to get longitude and latitude for an iPhone App code ?

It's really easy doing this using CoreLocation.framework. Fast learning tutorial is as below :


Open your AppDelegate.m, and import the "CoreLocationViewController.h".
// Add this line on top of your AppDelegate.m

#import "CoreLocationViewController.h"
Change the application:didFinishLaunchingWithOptions: method in your 'AppDelegate.m' with the code below. First of all create a 'CoreLocationViewController'. Then create a UINavigationController with the 'CoreLocationViewController' as the RootViewController.
Now we need to add the UINavigationController as a subview of the UIWindow.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
 
    CoreLocationViewController *myCoreLocationViewController = [[CoreLocationViewController alloc] 
                                                      initWithNibName:@"CoreLocationViewController" 
                                                      bundle:[NSBundle mainBundle]];
 
    UINavigationController *nav = [[UINavigationController alloc] 
                                   initWithRootViewController:myCoreLocationViewController];
 
    [[self window] addSubview:[nav view]];
 
    [self.window makeKeyAndVisible];
    return YES;
}

Open your 'CoreLocationViewController.xib' and add two objects of type UILabel. Switch to the 'Assistant Editor'. Select the UIView and insert outlets for the two newly added objects. Give the labels the following names. One label 'locationLabelOne' and the other lable 'locationLabelTwo'. You could create outlets by 'ctrl-clicking' a object and drag it to the left side of the 'Assistant Editor'. This two labels are later used to fill with the longitude and latitude of your CoreLocation service.
Now it's time to create your CoreLaction service. We need a CLLocationManager to get the current locationof the users device.
Open your 'CoreLocationViewController.h' and fill it as the code below. First import the CoreLocation service, after that make a CLLocationManager object.

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
 
@interface CoreLocationViewController : UIViewController <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
}
@property (retain, nonatomic) CLLocationManager *locationManager;
@property (retain, nonatomic) IBOutlet UILabel *locationLabelOne;
@property (retain, nonatomic) IBOutlet UILabel *locationLabelTwo;
 
@end

After that it's time to use the CoreLocation service in your 'CoreLocationViewController.m'. Open your file and change or add the methods below. First of all synthesize your before created CoreLocation service.
#import "CoreLocationViewController.h"
 
@implementation CoreLocationViewController
@synthesize locationManager; 
@synthesize locationLabelOne;
@synthesize locationLabelTwo;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
The second change is in the viewDidLoad: method. Initialize your CoreLocation service by changingthat method with the code below. 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
 
    [self setLocationManager:[[CLLocationManager alloc] init]];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    [locationManager startUpdatingLocation];
}

Now it's time to add the new locationManager:didUpdateToLocation:fromLocation: methodFill the method with the code below. This method is called by the CoreLocation service when the device is going from point a to point b. In this method you've the longitude and latitude, and you could do something with that.
In this situation we're adding the current location to the two above added UILabels. You could read the longitudeand latitude from your screen.

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
 
 
    [locationLabelOne setText:[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]];
 
    [locationLabelTwo setText:[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]];
}

Finished! Now you're ready to run your project and use the CoreLocation service to get the longitude andlatitude of the users device.

A reference link and code is on this site : 

Find a detailed tutorial and code on this link !

Saturday, 13 April 2013

Why to use clean in XCode ?

Well, many a times, we see errors which are irrelevant and we think this doesn't make sense. When we code and error appears, we directly resolve some of the lines above and when we see below there are some errors still there.

These are actually resolved already but keep on appearing. So, if you will keep on seeing and thinking that these are the errors and you will try to resolve them, you will be in a mess.

So, rather keep using "clean" frequently as it will clean unnecessary error messages those are already resolved.

Cannot use super because it is a root class error in Xcode project !



Getting an error "cannot use super because it is a root class" and wasn't able to find the proper solution. After a while found that have misplaced the name so "self" calling was not working.

This error may appear either you haven't written @interface "" @end or misplaced the name of it or written a different name after "@implementation"

e.g. in above image, as I was working on CoRoViewThree_iPad, when I replaced "Four" with "Three" and similarly Three instead of Two in @implementation, the error resolved.


Such kind of error mostly occurs when we copy the codes of different view controllers and forget to change the names.




How to make an invisible button in iOS App xib ? Hidden Vs. Custom type

I was developing an app where I needed to replace the existing buttons with the invisible ones, means in the background I've set my useful image that replaces buttons.

There were some sound play events on button touch up inside. I firstly, tried making the buttons hidden by ticking one of the properties of making a button hidden. 


The problem I faced was that on running the app the sounds were not being played. Tried a little to resolve it and the solution was :

* To select the button type to custom, that makes the button disappear and works like hidden. So now, sounds got played and it worked !


And in order to show the button getting pressed, tick the option of "shows touch on highlight"