Monday, August 27, 2012

Twitter integration on iOS 5 & later (Tweets only)

Hi folks,
  In this section we are going to explore new Apple framework "Twitter.framework" which is available from iOS 5. There are lots of things you can track with this framework like tweeting from your app, extracting profile and pictures of others from within your app etc. But, for this blog we are only going to send out tweets from our iOS apps. Along with the tweet text, i will even show you how to add image/pictures and links with the tweet. Lets get started then:

1. Add Twitter.framework and Accounts.framework to your project. One thing to be noted is if you are going to support your app for below iOS 5 too then you have to weak link the Twitter framework since it is not available for older versions. For this, simply click on the Twitter.Framework on the Build Phases of your target settings and change the "Required" drop down to "Optional".
2. Include #import <Twitter/Twitter.h> in your .h file.
3. Now, lets write a code for tweeting in a custom method :
-(IBAction)sendTweetsFromYourApp : (id) sender {
if(NSClassFromString(@"TWTweetComposeViewController")!= nil){  // this if call is required if you are going to support your app for older than iOS 5, this simply checks whether this Tweet class is available or not in running device (we do have to weak link Tweeter framework as described above for older iOS,But for app targeting iOS 5 and later this check is not required.)

Friday, August 24, 2012

E-Mail address validation in iOS app

Email validation custom method :

 -(BOOL) validEmailAddress:(NSString*) emailString {
    NSString *regExPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$";
    NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];
    if (regExMatches == 0)
        return NO; // email invalid
     else
        return YES; // email valid
  }

Now, just call above method from where you want to validate email address (pass your email address as a emailString to this method).
For eg:
 if ([self validEmailAddress:yourEmailAddressText] == YES) {
      //email valid
}
else {
    //email invalid
}

Thursday, August 23, 2012

Pop-Up ViewController for iPad applications

In this section, i will guide you to show a pop up viewcontroller just like in attached picture for iPad application. To design a pop up viewcontroller, we are going to use Apple's UIPopoverController class and its delegates. You can use UIViewController or UITableViewController as per your requirement in popup view.

1. In your root .h class

    - Include <UIPopoverControllerDelegate>
    - @property(nonatomic,retain) UIPopoverController *popOverView;
    -(IBAction) showPopupViewController : (id) sender ;  // button method from where you want to show pop up

Different TextStyles in a single UILabel

In  general, i will say most of you guyz, will use two UILabels back to back to adjust the text/string of different styles like in colorwise,bold/nonbold etc. But here , i will show you another way to achieve this by simply using one UILabel to show text having different style.
For this section we will customize two different text in a single label,and keep in mind that in the similar way you can even customize more text in a single label.
Lets create a custom UILabel :
   UILabel *customLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 25)];
   customLbl.backgroundColor = [UIColor yellowColor];
    [self createTwoTextStyleInSingleUILabel:customLbl];// custom method calling
   [self.view addSubview:customLbl];
Now, lets create a custom method for customizing textstyles:
- (void)createTwoTextStyleInSingleUILabel: (UILabel *) myLabel;
Before applying this method,  add QuartzCore framework (needed for CALayers), and CoreText framework(needed for the attributed string.) 

Wednesday, August 22, 2012

Calculating Distance between two locations

In this section, i will show you the easy way of calculating distance between the any of two locations in meters or miles. First of all, we need to have the Longitude and Latitude of those locations to calculate their distance, and for getting latitude and longitude please have a look at MapKit framework and tutorials.
Here i assume we have the required longitude and latitude of those two locations.
For example:
Location 1 ->  double  long1 = -117.321570;   double  lat1 = 33.115120;
Location 2 ->  double  long2 = -122.406417;   double  lat2 = 37.785834;
Now, making CLLocation object for both locations;
CLLocation *location1 = [[CLLocation alloc] initWithLatitude: lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude: lat2 longitude:long2]; 
NSLog(@"LOC1  = %f, %f", location1.coordinate.latitude,  location1.coordinate.longitude);
NSLog(@"LOC2 = %f, %f", location2.coordinate.latitude, location2.coordinate.longitude);
// now calculating the distance using inbuild method distanceFromLocation:
 float distInMeter = [location1 distanceFromLocation:location2]; // which returns in meters
//converting meters to mile
float distInMile = 0.000621371192 * distInMeter;
NSLog(@"Actual Distance in Mile : %f",distInMile);

Tuesday, August 21, 2012

Animating UINavigationBar

In this section, i will show you the code for hiding and showing UINavigationBar with smooth animation effect.
Lets say , your UINavigationBar object is  : UINavigationBar *navigationBar;

- (void) animateAndShowNavBar{
    // get the frame of navigation bar
    CGRect navBarFrame = navigationBar.frame;
    // set the beginning y coordinate of the title/navigation bar. Its 0(on the top) for this case
    navBarFrame.origin.y = 0;
 
// Now, to animate use one of the method below:
/* For iOS 4 and later we can use the following code to animate :
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationCurveEaseOut   animations:^
     {
         navigationBar.frame = navBarFrame;
     }
       completion:^(BOOL finished)
     {
         NSLog(@"Done!");
     }
     ];
/* end of animation for iOS 4 and later ........*/

Monday, August 20, 2012

UI Element customization on iOS 5

From iOS 5, Apple has included many new APIs you can use to easily customize the appearance of various UIKit controls. We can use these features for each and every UI Elements as per requirement but moreover, iOS 5 has given us a new feature to customize our UI Elements once, that means we can customize the same UI Element all over the application by writing  code at once, and that part of customizing code is written in  application:didFinishLaunchingWithOptions: in application delegate class. If you are still confuse with my description then please have a look at the code written below, that will help you to understand what i am trying to describe.

Method: - resizableImageWithCapInsets:
   -Used to create resizable UIImages for iOS app, helps to stretch images. 
    - For the cap insets, you basically specify the fixed region of a given image in top, left, bottom, right. For example if you want whole image thing to be stretched then we pass 0 for all parameters :UIEdgeInsetsMake(0, 0, 0, 0). And lets say if you don't want to stretch 7 leftmost pixel and 5 rightmost pixel of UIImage(mainly the case for UIbutton images) , then we pass the parameters as :UIEdgeInsetsMake(0, 7, 0, 5).
    -  For using this property, you don't need to have large image, you should/may have small image with better resolution so that it can be stretchable without any problem.
 
1. Adding an Image as a UIColor  to the UIView 
     // This feature is available in older version too
    - (void)viewDidLoad {  
          [super viewDidLoad];
          [[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"your image name"]]];   
         }


2. Customizing UINavigationBar