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
}

Friday, 14 November 2014

iOS 6 Grouped Style UITableView in iOS 8 App

I came across a requirement where i have to show  iOS 6 grouped style UITableView in iOS8 App.
So i did some workaround and it really works.

Steps:

  • I added a UITableView on UIViewController (Scene) in UIStoryboard.
  • I reduced the width of the UITableView and centre aligned it (eg. UIViewController width - 320px so i take 300px UITableView).
  • Add & Import QuartzCore framework.
  • In cellForRowAtIndexPath method make the cell layer corner round as per requirement and set masktobounds as shown below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TableCell";
    
    UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (tableCell == nil) {
        tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
    
    tableCell.layer.cornerRadius = 5;
    tableCell.layer.masksToBounds = YES;
    
    [tableCell setBackgroundColor:[UIColor lightGrayColor]];
}


Wednesday, 23 April 2014

Text to Speech in iOS7

iOS7 makes Text to Speech really easy and that also in multiple languages.
iOS7 has added two new classes for Text to Speech functionality:
1. AVSpeechSynthesizer
2. AVSpeechUtterance

First Link AVFoundation and AudioToolbox Framework from Application Target -> Build Phases -> Link Binary With Libraries.

Import both frameworks in Header file (.h file)
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

add delegate <AVSpeechSynthesizerDelegate>.

In implementation file (.m file)
    AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *speechUtterance = [AVSpeechUtterance speechUtteranceWithString:@"Welcome to iOS App."];
    speechUtterance.rate = AVSpeechUtteranceMaximumSpeechRate / 4.0f;
    speechUtterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; // language
    [speechSynthesizer speakUtterance: speechUtterance];

By adding the above code you can add text to speech functionality to your iOS7 app.