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.

NSDictionary *playerVars = @{@"playsinline" : @1,};
if([self.mYTPlayerView loadWithVideoId:@"M7lc1UVf-VE" playerVars:playerVars])
{
self.mYTPlayerView.delegate = self;
}
else
{
NSLog(@"failed loading player");
}
view raw gistfile1.txt hosted with ❤ by GitHub
(b)play a video after loading it.

- (IBAction)play:(id)sender
{
if(self.mYTPlayerView.playerState != kYTPlayerStatePlaying){
[self.mYTPlayerView playVideo];
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
(c)pause and stop the video

- (IBAction)pause:(id)sender
{
if(self.mYTPlayerView.playerState == kYTPlayerStatePlaying){
[self.mYTPlayerView pauseVideo];
}
}
- (IBAction)stop:(id)sender
{
if(self.mYTPlayerView.playerState == kYTPlayerStatePlaying || self.mYTPlayerView.playerState == kYTPlayerStatePaused){
[self.mYTPlayerView stopVideo];
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
(d)Doings with delegate methods, the following "YTPlayerView" delegate methods can be used for having more control on top of YouTube player.

#pragma ---
#pragma YTPlayerView Delegates
- (void)playerViewDidBecomeReady:(YTPlayerView *)playerView
{
NSLog(@"YTPlayerView : playerViewDidBecomeReady");
NSArray *qualityLevels = [self.mYTPlayerView availableQualityLevels];
NSLog(@"quality levels available:- %@", qualityLevels);
}
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state
{
NSLog(@"YTPlayerView : didChangeToState");
switch (state) {
case kYTPlayerStatePlaying:
NSLog(@"Started playback");
break;
case kYTPlayerStatePaused:
NSLog(@"Paused playback");
break;
default:
break;
}
}
- (void)playerView:(YTPlayerView *)playerView didChangeToQuality:(YTPlaybackQuality)quality
{
NSLog(@"YTPlayerView : didChangeToQuality");
[self printQualityLevel];
}
- (void)playerView:(YTPlayerView *)playerView receivedError:(YTPlayerError)error
{
NSLog(@"YTPlayerView : receivedError");
}
view raw gistfile1.txt hosted with ❤ by GitHub
(e)getting the quality levels.

- (void)printQualityLevel
{
NSLog(@"current playback quality:- ");
switch (self.mYTPlayerView.playbackQuality) {
case kYTPlaybackQualitySmall:
NSLog(@"kYTPlaybackQualitySmall");
break;
case kYTPlaybackQualityMedium:
NSLog(@"kYTPlaybackQualityMedium");
break;
case kYTPlaybackQualityLarge:
NSLog(@"kYTPlaybackQualityLarge");
break;
case kYTPlaybackQualityHD720:
NSLog(@"kYTPlaybackQualityHD720");
break;
case kYTPlaybackQualityHD1080:
NSLog(@"kYTPlaybackQualityHD1080");
break;
case kYTPlaybackQualityHighRes:
NSLog(@"kYTPlaybackQualityHighRes");
break;
case kYTPlaybackQualityUnknown:
NSLog(@"kYTPlaybackQualityUnknown");
break;
default:
break;
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
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