Friday, 3 July 2015

Variable declaration in Swift


Declaration of iVar:

Let's start with declaring iVars in Swift

In Swift we can declare an iVar using keyword:
1. let - it declares an immutable iVar
2. var - it declares a mutable iVar

let myLetObject = "My immutable iVar"
So now i cannot change the value of myLetObject after it is once assigned.

var myVarObject = "My mutable iVar"
myVarObject = "Changed the value of iVar"
I can change its value at any instance.

Some examples of declaring objects:
let luckyNumber = 9
let myAge = NSInteger(27)
var myHeight = 5.5
let myImage = UIIMage(named:"MyPhoto.png")
var myName: NSString = NSString"string:Suraj Ramjan Mirajkar"
var tableView: UITableView!



Wednesday, 3 June 2015

Youtube Video Player


To play Youtube video within the app we have 2 ways:

1. Open Youtube App (if installed) or launch Safari to play video:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=Xk1zfQOklJY"]];

In this case we don't have a choice to bring back the user to our app.
So we better use the 2nd way.

2. Embed Youtube iOS Player Helper:
I have used youtube-ios-player-helper Version - 0.1.3
  1. Select YTPlayerView.hYTPlayerView.m, and the Assets folder from my Github repository CODE.
  2. Drag these files and folders into your project. Make sure the Copy items into destination group’s folder option is checked. When dragging the Assets folder, make sure that the Create Folder References for any added folders option is checked.
  3. Drag a UIView the desired size of your player onto your Storyboard.
  4. Change the UIView's class in the Identity Inspector tab to YTPlayerView
  5. Import "YTPlayerView.h" in your ViewController.
  6. Add the following property to your ViewController's header file:
    @interface ViewController : UIViewController <YTPlayerViewDelegate>
    @property(nonatomic, strong) IBOutlet YTPlayerView *playerView;
  7. In Implementation's viewDidLoad() add following code:[self.playerView setDelegate: self];
    [self.playerView loadWithVideoId:@"Xk1zfQOklJY"];
  8. We are done.
  9. In case you want to get all the controls to Play, Pause, Stop, Seek to Time do download my code and check it out.

        Download - CODE


Below links helped me:
https://developers.google.com/youtube/v3/guides/ios_youtube_helper
https://github.com/youtube/youtube-ios-player-helper



Friday, 13 March 2015

Check Internet Connectivity Using AFNetworking 2.0



I have created NetworkRechabilityMonitor class to get the Internet Connection status in my app using AFNetworking 2.0 library.

1. Create an NSObject class naming NetworkRechabilityMonitor, add below code in respective Header & Implementation files.


  NetworkRechabilityMonitor.h


#import <Foundation/Foundation.h>

@interface NetworkRechabilityMonitor : NSObject

+(void)startNetworkReachabilityMonitoring;
+(BOOL)checkNetworkStatus;


@end



NetworkRechabilityMonitor.m

#import "NetworkRechabilityMonitor.h"

@implementation NetworkRechabilityMonitor

#pragma mark - Start Monitoring Network Manager
+(void)startNetworkReachabilityMonitoring {
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

#pragma mark - Check Internet Network Status
+(BOOL)checkNetworkStatus {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}

@end


2. Import the NetworkRechabilityMonitor in your ViewController class where you want to check the internet status.

3. In viewDidLoad method of your ViewController add below line of code:

[NetworkRechabilityMonitor startNetworkReachabilityMonitoring];

4. Now in ViewController if i want to check internet status before making an API call add below code:
if ([NetworkRechabilityMonitor checkNetworkStatus]) {
          NSLog(@"Connected to Internet");
          // Add your code
} else {
         NSLog(@"Not Connected to Internet");
         // Notify user to connect to Internet
}