Wednesday 1 October 2014

How to use UIAlertController instead of UIAlertView & UIActionsheet in iOS8


In iOS8 UIAlertController replaces UIActionsheet and UIAlertView. An AlertController can be created with a "title" and a "message".

We can set two styles to an AlertController. Which is given below.
(a)UIAlertControllerStyleActionSheet -  for configuring UIAlertController as an ActionSheet
(b)UIAlertControllerStyleAlert             -  for configuring UIAlertController as an Alert.

How to create an Alert/ActionSheet
  • Creating an AlertController object


  • Adding an Action

      
The handler block can be used to get the call back when “ok” button is pressed.
  • Presenting the alertController

How to create a simple Alert with OK button and a message

The following method will create a simple Alert in iOS8.

The Alert will be looking like as given below.




How to create a an Alert with two buttons

The Alert will be looking like as given below.





How to create an Alert with a TextField


The Alert will be looking like as given below.




How to create an Alert with two textfields

The Alert will be looking like as given below.




How to create an Actionsheet with two buttons

For creating an ActionSheet use "PreferredStyle" as "UIAlertControllerStyleActionSheet".


The ActionSheet will be looking like as given below.






Thursday 14 August 2014

Accessing Elements Added in XIB without doing a IBOutlet Mapping

Probably most of you guys know about this. We can access a view element in a view controller by its tag number. This can be used in XIB's also.

For example:-

If we have added a UIButton in XIB , and we need to change its title dynamically from code. The traditional way of doing is creating a property with IBOutlet and mapping it to the corresponding element. Then change the title using the variable as given below.






But every time creating a property with IBOutlet is not necessary. We can do it the following way.

(Step 1) Add a tag to the corresponding element in the XIB file. Which is shown below in the case of UIButton.






(Step 2) Getting the UIButton element by accessing its tag value as given below.


    UIButton *button = (UIButton *)[self.view viewWithTag:234];
    if(button)
    {
        [button setTitle:@"Submit" forState:UIControlStateNormal];

    }


This can be applicable to most of the UIControl items also element like UILabel, UITableView, UIPickerView, UIView, which ever is having a tag property.






Tuesday 12 August 2014

YouTube Player for iOS

The older way of embedding youtube videos into iOS Applications was adding the video link into a UIWebView and loading it. Now google came up with a Helper library , by which we can embed youtube videos into UIView(internally UIWebView on top of UIView) and can play more easily.

The main advantage of this Helper library is , we will get more options and control over the video. By which we can do  Play, Pause, Stop and Video Skipping.

Steps for integrating YouTubeHelper library into existing Xcode Projects

1. Download the Library Source from GitHub.

2. Add the following files "YTPlayerView.h",  "YTPlayerView.m", and the "Assets" from library folder into your application project.

3. Create an IBOutlet property with "YTPlayerView" and map the corresponding "UIView" component in the XIB file. "YTPlayerView" is a subclass of "UIView", you need to change the class type of "UIView" component into "YTPlayerView" in the identity inspector.

4. Please refer the direct link for information.
https://developers.google.com/youtube/v3/guides/ios_youtube_helper#installation_manual

5. While running the App in iOS 7, incase if you encountered an issue as like the following
 "Received error rendering template: Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)"" , refer the following link.

http://stackoverflow.com/questions/23751817/try-to-play-you-tube-video-with-ytplayer-third-partry-libraray-in-ios

After Successfully integrating , use the following methods to load and play the youtube url.

(a)load video with youtube id.

(b)play a video after loading it.

(c)pause and stop the video

(d)Doings with delegate methods, the following "YTPlayerView" delegate methods can be used for having more control on top of YouTube player.

(e)getting the quality levels.

I have just embedded a youtube video into my "viewcontroller". The "UIView" size I have given is (640, 320) this is the size for a medium quality video.



 
Reference:-

Please refer the following google developer link for more info.
https://developers.google.com/youtube/v3/guides/ios_youtube_helper#installation_manual

Thursday 24 April 2014

Image Rendering Modes in iOS7


Apple introduces a new property UIImageRenderingMode with UIImage from iOS7. There are three types of rendering modes which can apply to an image which are given below.

typedef enum : NSInteger {
       UIImageRenderingModeAutomatic, 
       UIImageRenderingModeAlwaysOriginal, 
       UIImageRenderingModeAlwaysTemplate, 
} UIImageRenderingMode;

UIImageRenderingModeAutomatic
This is the default rendering mode.

UIImageRenderingModeAlwaysOriginal
By using this mode , the image will be rendered with out any modifications to the image property. Which is exactly equivalent to reading the image from disk file and rendering in the screen.

UIImageRenderingModeAlwaysTemplate
This mode will ignore the colour information of image. And apply the colour according to the way the context is defined. Context is the tintColor of the superview of UIImageView where the image is added.

Advantages using UIImageRenderingModeAlwaysTemplate

1. The number of image resources can be reduced by reusing the same kind of image file. This will help to the application bundle size.

2. Really suitable for applications supporting more that one theme as colour. A single line of code is enough to change the colour of all the images and there by changing the theme.


The following lines of code will change the colour of the image which is added in an UIViewController.

self.view.tintColor = [UIColor greenColor];

0r

[[[UIApplication sharedApplication] keyWindow] setTintColor:[UIColor orangeColor]];


Creating a UIImage with rendering mode


rOneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 292, 297)];
UIImage *image1 = [UIImage imageNamed:@"circular_arrow"];
image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
rOneImageView.image = image1;

[self.view addSubview:rOneImageView];


The following will illustrates the change in the UIImage with rendering mode and context.


UIImageRenderingModeAlwaysTemplate
tintColor - orangeColor
UIImageRenderingModeAlwaysTemplate
tintColor - greenColor
UIImageRenderingModeAlwaysOriginal

   with green-color                Original image             with orange-color

How it works


The following code will create a UIImageView with UIImage and add to UIViewController's view as given below.


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    rOneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 292, 297)];
    UIImage *image1 = [UIImage imageNamed:@"circular_arrow"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    rOneImageView.image = image1;
    [self.view addSubview:rOneImageView];

}

And the button action is given below.


- (IBAction)changeColor:(id)sender{
      self.view.tintColor = [UIColor greenColor];
}


For tracking changes to tintColor , subclass UIView and override the following method.

- (void)tintColorDidChange{
    NSLog(@"tintColorDidChange");

}