How to use Font Awesome in Swift

Font Awesome is a famous iconic font. In a case, iconic font is useful because we can use many icons without a lot of small files.

Follow the below steps to integrate Font Awesome in your Xcode project.

  1. Create a project in xcode.
  2. Download the zip font form http://fontawesome.io/Font Awesome Source
  3. Unzip the folder and drag fonts/fontawesome-webfont.ttf into your projectfont awesome drag
  4. Open Info.plist file
    • Add a new key “Fonts provided by application”. This is of type Array.
    • Expand the array, and for Item 0, set the string value to “fontawesome-webfont.ttf”

info.plist file

Congratulations, you have integrated font awesome into your project

How to use Font Awesome

  1. Drop a label on View Controller
  2. Set the font to “Custom”, and the family to FontAwesome
  3. Set the font size to 50Storyboard label
  4. Connect label to your view controller with IBOutlet
  5. Set the label text by using of font awesome unicode.IBOutlet with labelOUTPUT
    output

 

That’s It. If you like this post, Please comment below and subscribe this blog.

Leave your comments

Advertisements

How to show alert controller

UIAlertController Example

UIAlertController is introduced in iOS 8  and it supports two styles. Using this you can create an alert dialog like UIAlerview or action sheet like UIActionSheet.

1) SIMPLE ALERT DIALOG

Using the below code you can show alert with without any button

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Thank you using alert controller" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];

2) SIMPLE ALERT DIALOG WITH ONE BUTTON

Using the below code you can show alert with one button

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Thank you using alert controller" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];

3) SIMPLE ALERT DIALOG WITH TWO BUTTONS

Using the below code you can show alert with two buttons with action

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Thank you using alert controller" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
 //your code here
 }]];
[self presentViewController:alert animated:YES completion:nil];

4) CREATE AN ACTION SHEET WITH ACTIONS

Using the below code you can show actionsheet with two buttons with action

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Action sheet" message:@"Thank you using action sheet" preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
 //your code here
 }]];
[self presentViewController:alert animated:YES completion:nil];

5) CREATE AN ALERT DIALOG WITH USERNAME AND PASSWORD FIELDS.

To add a text field you can use the method addTextFieldWithConfigurationHandler.
Text fields can be added to only type UIAlertControllerStyleAlert.

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Enter your login Credentials" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
 textField.placeholder = @"Username"; //for username
 }];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
 textField.placeholder = @"Password"; //for passwords
 textField.secureTextEntry = YES;
 }];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
 //your code here
 }]];
[self presentViewController:alert animated:YES completion:nil];

You can find Swift Version here

Reference: Apple Documenation

That’s It. If you like this post, Please comment below or subscribe this blog.

Leave your comments

 

How to detect carrier connection type (3G / EDGE / GPRS)

Apple reachability class respond either your device is connected to Wi-Fi or WWAN(Cell).

Now, the question is “How to check network 2G or 3G in IOS”. Here is the code to detect 2G, 3G, 4G.

First of all, add CoreTelephony framework to your project.

In your AppDelegate.h Class, Import below class

#import "Reachability.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

Add the below code in AppDelegate.h file,

@property (nonatomic)NetworkStatus remoteHostStatus;
@property (nonatomic)Reachability *reachability;

in AppDelegate.m file, add this

@synthesize remoteHostStatus;
@synthesize reachability;


reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
remoteHostStatus = [reachability currentReachabilityStatus];

if(remoteHostStatus == NotReachable) {
   NSLog(@"You have not internet");
}else if (remoteHostStatus == ReachableViaWiFi){
   NSLog(@"Connected via wifi");
}else if (remoteHostStatus == ReachableViaWWAN){
  NSLog(@"connected via cell");
  CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
  NSString *telCarrier = [[netinfo subscriberCellularProvider] carrierName];
  NSLog(@"Your mobile carrier = %@",telCarrier);
  if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
  NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
  NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
  NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
  NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
  NSLog(@"4G");
}

That’s It. If you like this post, Please comment below or subscribe this blog.

Leave your comments

How to pass object with NSNotificationCenter

Hi,
There are so many reason to use NSNotification. Now i am writing about how to use NSNotificationCenter in iOS.

First make 2 methods in ViewController.

- (void)registerNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"myNotification" object:nil];
}
- (void) receiveNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"myNotification"])
        NSLog (@"Successfully received the test notification!");
}

in ViewWillAppear Call registerNotification method

-(void)viewWillAppear:(BOOL)animated{
   [self registerNotification];
 }

Now, you can call myNotification from any ViewController by using below code,

[[NSNotificationCenter defaultCenter]postNotificationName:@"myNotification" object:self];
 

If you want to pass data into notification, You’ll have to use the “userInfo” variant and pass a NSDictionary object.

NSDictionary* userInfo = @{@"Name": @(@"Ashutosh Kumar")};
NSNotificationCenter* notify = [NSNotificationCenter defaultCenter];
[notify postNotificationName:@"myNotification" object:self userInfo:userInfo];

and need some change in receiveNotification method

- (void) receiveNotification:(NSNotification *) notification
{    if ([[notification name] isEqualToString:@"myNotification"])
        NSDictionary* userInfo = notification.userInfo;
        NSString* GetName = userInfo[@"Name"];
        NSLog (@"Successfully received test notification! %@", GetName);
}

That’s It. If you like this post, Please comment below or subscribe this blog. Leave your comments

MapView and Annotation in iOS

In Xcode 6.3, go to File\New\New Project, select iOS\Application\Single View Application, and click Next.

Add New Project

On the Choose option for your new project screen,  Fill the all information about your project and click Next.

Choose your project

Save Your Project.

Click on Main.storyboard to bring up Interface Builder.

MainStoryboard

From the Object library, Select MapKit View and drag to the screen and fit as you want.

Mapkit View

Add the MapKit framework to your project, In Xcode 6.3, click on the name of your project, in General Tab, Go bottom of the screen. Locate Linked Frameworks and Libraries, click + button below.

Locate Framework and Libraries

In the “Choose Framework and Libraries” popup, typeMapKit, Select MapKit.Framework and click on add.

Add MapKit Framework

Click on Assistant Editor Button, then click on the Map Screen and hold control key and drag it to .h file and Set IBoutlet.

Assistant Editor

 

Go to ViewController.h file and import <MapKit/MapKit.h>

Import MapKit

Run the project. You will see map on the device.

Run on Simulator

Open ViewController.m, and add the following underneath the #imports and before the @implementation:

#define METERS_PER_MILE 1609.344

 

Now synthesize the mapView in ViewController.m file.

@synthesize mapView;

Now implement viewWillAppear ,

- (void)viewWillAppear:(BOOL)animated {  
 
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 28.632464;
    zoomLocation.longitude= 77.221023;
 
  
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
 
 
    [mapView setRegion:viewRegion animated:YES];
}

Run your Project again, you will find your entered location on map with zoom.

Map Run on simulator

Now Drop Pin on the Map

Add the below code in the viewWillAppear Method.

MKPointAnnotation *point= [[MKPointAnnotation alloc]init];
point.coordinate = zoomLocation;
point.title = @"Where i am?";
point.subtitle = @"I am Here!!";
[self.mapView addAnnotation:point];

Run the Project, you will find pin on the map.

Map Annotation in iOS

That’s It.

If you like this post, Please comment below or subscribe this blog.

Leave your comments

How to pass data back to previous ViewController using PresentViewController

Let suppose, We have to pass data from B to A ViewController. Here, I am using Present View Controller. When you  dismiss PresentViewController, Data will back to Parent View Controller.

In the B viewController.h File,

@protocol senddata <NSObject>
-(void) Passdatatoback:(NSString*)Passdataback;
@end

Also set the property in .h file,

@property(nonatomic, assign)id delegate;

Now, move to B viewController.m File,

@synthesize delegate;

and set a method,

-(void)viewWillDisappear:(BOOL)animated
{
[delegate Passdatatoback:@”This is data which you want in A ViewController”];
}

Now, Move to A viewController,

Create a same method which you have create in B ViewController.m file.

-(void) Passdatatoback:(NSString*)Passdataback{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Your string Data Showing” message:Passdataback delegate:self cancelButtonTitle:@”OK “ otherButtonTitles:nil];
 [alert show];
}

In A ViewContrller.m file, When You go in B ViewController,

BViewConoller *BVC = [[BViewConoller alloc]init];
BVC.delegate= self;
[self.navigationController presentViewController:BVC animated:YES completion:nil];

That’s It.

If you like this post, Please comment below or subscribe this blog.

Leave your comments