How to Create Delegates in Swift

What are delegates?

Delegates are the objects which allow one object to send a message as events happen. Imagine an object A calls an object B to perform an action. Once the action is complete, object A should know that B has completed the task and take necessary action, this can be achieved with the help of delegates!

Delegate == Protocol

Creating a delegate is as simple as creating a protocol — that’s pretty all it is.

protocol CastHandlerDelegate: class {

    /// Media Launched successfully on the cast device
    func launchObjectSuccess()

}

Calling the delegate

To use the delegate we add an optional variable to the class we’ll be sending notifications from.

weak var delegate: CastHandlerDelegate?

We can then call this from anywhere within that class like so:

delegate?.launchObjectSuccess()

Conforming to CastHandlerDelegate

As you probably already know, the next step is to conform to the delegate. First, we tell the class it needs to implement the methods set in the delegate.

class AViewController: UIViewController, CastHandlerDelegate {

}

Next, we implement the methods within our delegate.

class AViewController: UIViewController, CastHandlerDelegate {

    func launchObjectSuccess() {
        print("Media successfully launched!")
    }

}

That’s it! Our delegate has been created and we’re successfully calling the method.

iOS App Icons Sizes

According to Apple’s iOS Human Interface Guidelines, every app must provide an icon to be displayed on a device’s Home screen and in the App Store. An app should specify several different icons of different dimensions to suit different screen sizes and different situations.

iOS_7_Icon_Template
Here are the dimensions for iOS apps:

Name Size(px) Usage
Icon-Small.png 29×29 iPad Settings
Icon-Small@2x.png 58×58 iPhone Settings, iPad Settings for Retina display
Icon-Small@3x.png 87×87 iPhone Settings for Retina display
Icon-40.png 40×40 iPad Spotlight results
Icon-40@2x.png 80×80 iPhone Spotlight results, iPad Spotlight results for retina display
Icon-40@3x.png 120×120 iPhone Spotlight results for retina display
Icon-60@2x.png 120×120 iPhone App Icon
Icon-60@3x.png 180×180 iPhone App Icon for Retina display
Icon-76.png 76×76 iPad App Icon
Icon-76@2x.png 152×152 iPad App Icon for Retina display
Icon-120.png 120×120 CarPlay App Icon
Icon-20@2x.png 40×40 iPhone Notification
Icon-20@3x.png 60×60 iPhone Notification
Icon-20.png 20×20 iPad Notification
Icon-20@2x.png 40×40 iPad Notification
Icon-83@2x.png 167×167 iPad Pro App Icon
iTunesArtwork.png 512×512 App Submission
iTunesArtwork@2x.png 1024×1024 App Submission

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

Leave your comments

Navigation bar customization

A navigation bar appears at the top of an app screen, below the status bar. Navigation bar contains the navigation buttons of a navigation controller, which is a stack of view controllers which can be pushed and popped.

Let’s do something with Navigation bar

  1. Hide navigation bar

    [self.navigationController setNavigationBarHidden:YES animated:YES];
  2. Change navigation bar color

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
  3. Change navigation tint color

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  4. Change navigation bar Title

    self.navigationItem.title = @"This is title text";
  5. Change navigation bar title color

    [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
    [UIFont fontWithName:@"HelveticaNeue-Light" size:18.0], NSFontAttributeName, nil]];
  6. Hide back text from back button

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
  7. Hide back button

    [self.navigationItem setHidesBackButton:YES animated:YES];
  8. Add left button with Image

    UIImage *image = [[UIImage imageNamed:@"menu.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(menuBtnClicked:)];
    self.navigationItem.leftBarButtonItem = button;
  9. Add right button

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBtnAction:)];
    self.navigationItem.rightBarButtonItem = addButton;

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

Leave your comments

How to Generate pem file to setup Apple PUSH Notification

To enable Push Notification for your iOS app, you will need to create and upload the Apple Push Notification Certificate (.pem file) to us so we will be able to connect to Apple Push Server on your behalf.

Follow these steps:

  • Login to apple developer account, click “Certificates” on the left navigation bar. Then, click “+” button.Certificate
  • Select Apple Push Notification service SSL (Production) option under Distribution section, then click “Continue” button.APNS Certificate
  • Select the App ID you want to use, then click “Continue” to go to next step.APP ID Screen
  • Follow the steps “About Creating a Certificate Signing Request (CSR)” to create a Certificate Signing Request.Create CSR File
  • Navigate to Certificate Assistant of Keychain Access on your Mac.Keychain Image
  • Fill in the user Email Address, Common Name and Save to disk and click continue.Keychain
  • Upload the “.certSigningRequest” file which is generated, then click “Generate” button.Generate Certificate
  • Click “Done” to finish the registration.

Then Click “Download” button to download the certificate (.cer file) you’ve created just now.

Double click the downloaded file to install the certificate into Keychain Access on your Mac.

On your Mac, go to “Keychain”, look for the certificate you have just installed. If unsure which certificate is the correct one, it should start with “Apple Production IOS Push Services:” followed by your app’s bundle ID.

Expand the certificate, you should see the private key with either your name or your company name. Select both items by using the “Select” key on your keyboard, right click (or cmd-click if you use a single button mouse), choose “Export 2 items”.

Export P12

Then save the p12 file with name “pushcert.p12” to your Desktop. Now you will be prompted to enter a password to protect it, you can either click Enter to skip the password or enter a password you desire.

Now the most difficult part – open “Terminal” on your Mac, and run the following commands:

cd
cd Desktop
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts

We successfully generated .pem file, If you find any difficulties generating .pem please do comment and we will be happy to help.

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

Leave your comments

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

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];

 

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