Create a popup menu in swift

  1. Add an new ViewController to your project Main Storyboard

  2. Create a new Swift file named RightMenuViewController with the Subclass UIViewController

  1. Assign the Class to the newly created ViewController as shown below. Also make sure you add the Storyboard ID for the ViewController as RightMenuID. This ID will help us to refer to the ViewController while opening the popup menu

 

  1. Add a Bar Button Item on the ViewController where you wish the popup menu to appear as shown below. We will refer this ViewController as HomeViewController.

  1. Create an action reference to the button on the HomeViewController as shown below.

This will create a new function with the name rightMenuButtonTapped on the HomeViewController. Here we can write our code to open and close the popup menu

  1. Add below code to the function created in above step

Above code creates the RightMenuViewController as a Child View Controller and shows it when the Right Menu Button is tapped. On tapping the same button again, the code checks if there are already any child view controllers being shown and if yes it removes them all.

This completes the basic functionality of showing and hiding the popup

However if you also want the popup to disappear when the user taps anywhere outside the popup then add below code to the RightMenuViewController

Finally it is also important to properly place and format your popup menu.

Now just compile and run the code. You will see the popup menu as shown below after clicking the Right Menu Button

 

Download PopupMenu Source Code

Adding UIBarButton items programatically in iOS

Adding a single Custom Image UIBarButton items programatically

Above code adds a single bar button on the left of the navigation bar

Adding multiple Custom Image UIBarButton items programatically

Above code adds two bar buttons on the right of the navigation bar

Adding a System UIBarButton item programatically

 

Disable OS Logging in XCode 8

If you run your project in XCode 8 all of a sudden it will start showing lot of strange logs. Many of these logs are not related to your application so don’t worry. They are just OS logs. It does appear to be non intentional because such logs are not going to help us in any way. In fact it makes it much more difficult and frustrating to find the right logs related to App crashes, debug messages, etc.

Fortunately there is a way to disable these logs

Here are the steps

  1. Open your project and navigate to Product => Scheme => Edit Scheme
  2. This will open a dialog box as show in the below screenshot
  3. On the Run Tab, Select Arguments and add the Environment Variable OS_ACTIVITY_MODE with value disable

Disable OS logs

Add Google analytics to iOS app

Add Google analytics to iOS app

This shows how to add Google Analytics to your iOS app and not having to use the Firebase Analytics.
1. Login to your Google Analytics Account and create a New Property

  1. On the New Property screen select Mobile app, Google Analytics Services SDK. Type your App Name as shown.

Rest of the settings can be selected based on your requirement. Then click the button Get Tracking ID

  1. Once you get the tracking ID which would be in the format UA-XXXXXXXX-X, click the link for the iOS SDK Getting started guide

  2. As mentioned on the Getting Started guide page, create a pod file using terminal by navigating to your project and typing below command

  1. Open the newly created pod file and add below code just below the line which says # Pods for My iOS App

  1. Run below command

  1. Now open your project as workspace

  2. Click the button GET A CONFIGURATION FILE on the instructions page which will take you to your firebase console

  3. Login and create your Firebase Project if you haven’t created by typing your App name and iOS Bundle ID

10. Now create a file within your project named as GoogleService-info.plist (Do not download the file from Firebase console)

  1. Within Firebase console, open your project and Click on Project settings as shown below. Here you will find all the necessary details to fill the above file related to your project. Tracking code will be on the Google Analytics Account Tracking Info page

  1. Now just go through the rest of the settings as mentioned on the Getting started guide page i.e. creating a bridging header file, adding necessary code in AppDelegate file, etc
How to install Cocoapods

How to install Cocoapods

Start by typing below command in terminal

In most cases just the second command would however sometimes you may get below error in case you have an older version of activesupport installed

Error installing cocoapods: activesupport requires Ruby version >= 2.2.2

The first command will make sure you don’t get the above error

Once the above commands run successfully type below command to set up Cocoapods master repo

This will download the master repo. The size of the repo is quite big around 380MB so it can take a while depending upon your internet download speed.

Once the above is completed, navigate within terminal to your Xcode project and run below command to create the podfile

Open the newly created podfile with text editor, and add the required libraries e.g.

pod ‘Google/Analytics’

Then run below command to install the required libraries

 

Configure and handle Apple Push Notifications (APN)

Configure and handle Apple Push Notifications (APN)

Configure Apple Push Notifications (APNs)
Photo Credit – 4585763335Johan – Apple Push Notifications

Configuring Apple Push Notifications (APN)

Here are the steps to configure Apple Push Notifications on your IOS app
1. Open project settings, Capabilities Tab

2. Turn On Push Notifications as shown below

How to turn on Apple Push Notifications (APN)

 

3. For security reasons Push Notifications require appropriate SSL Certificates which can be created through your Developer Portal. Here are the steps to create the required certificates.

3 A. Login to your Apple Developer portal

3 B. Click on App IDs under Identifiers

3 C. Click on your IOS App ID entry which shows the services enabled for your app

3 D. Click the Edit button at the bottom of the screen

3 E. Turn on Push Notifications (if not already enabled)

3 F. Click the Create Certificate button under the Development SSL Certificate 

3 G. Open Keychain application and Request a Certificate from a Certificate Authority as shown below

Request a certificate from a certificate authority

3 H. On the Certificate information add your email address, Name (make sure there are no spaces) and Save to Disk

3 I. Double click the certificate to add it to your Keychain.

3 J. Now find the certificate in your Keychain and expand the arrow next to it which will also show the Private Key associated with it

3 K. Using Shift button select both i.e. the Certificate and the Key and Export both the items in .p12 format. Name the file as certificatesDev.p12

3 L. You may choose to add your own password or leave it blank. If you add a password make a note of it somewhere.

3 M. Using below command in Terminal covert the .p12 format to .pem

4. Add below functions to your project’s AppDelegate.swift file

didRegisterForRemoteNotificationsWithDeviceToken gets the device token and passes the same along with some other information like systemversion, model etc to a php page which in turn stores the information in probably a MySQL database. This is the database of tokens to which we will be sending our Push Notifications.

5. Now add below code to the appDelegate.swift’s didFinishLaunchingWithOptions function

The above code basically calls the functions we created in Step 4

Apple Push Notification Payload

Push Notification Payload defines the format for the remote notification. Below is a basic format of a sample message.

For other fields refer to the Apple Documentation

At this point all the requirements to send and receive a notification are completed. However until this point when the user receives a notification, the message would just open the app and would do nothing else.

In most cases we would want the user to open a notification and perform certain task/s. Here are some examples.

  1. Download latest news items and open the View Controller associated with it
  2. Show a detailed message from the web related to an event, etc (Inside a Webview)

To achieve the above, we need something to handle the remote notifications inside your app.

This part mostly depends on what you want to achieve. Here we will just see an example. Let’s assume we want to promote an event and the notification should show the events page from the web on WebView added to a View Controller.

In a nutshell when the user taps the notification, instead of showing the dashboard of your application you want to automatically make him navigate to a certain View Controller which downloads a web page and shows the content of it in a Web View.

So here are the steps

1. Open your project settings page and turn on Remote notifications on the capabilities tab

2. Now your payload would include a URL to the webpage

In the above payload, link is not a defined field. It is a custom field which we will be reading in our app.

3. Create a View Controller named as NotificationViewController in your app and add a web view to it which opens an external link. The View Controller should be able to accept the payload as NSDictionary. Give some ID to the controller probably “notification

4. Now add below function to AppDelegate.swift file

Above function gets triggered when the user taps on the notification received because we have enabled remote notifications. This function basically just gets the payload and sends it to the NotificationViewController in your app.

5. The NotificationViewController grabs the link part from the payload and just opens the respective page.

Sending Push Notifications

The only part now remains is the method to send push notifications

While your app is under development you can test sending notifications using APN Tester

Once you are happy with it, you can create your own system to send notifications payload

Sandbox Notifications link: ssl://gateway.sandbox.push.apple.com:2195

Production Notifications link: ssl://gateway.push.apple.com:2195

Remember to include the respective .pem file to send the notification payload

 

Customising Navigation Bar or Header in an IOS app

Customising Navigation Bar or Header in an IOS app would involve following things

  • Changing Background Colour
  • Adding a logo
  • Changing Text Colour

You would need to add below code mostly to your Initial View Controller

Just add the lines as per your requirement and change the colour values as per your needs.

barTintColor method defines the Tint Color to apply to the navigation bar background

The tintColor combines with the translucent part of the bar along with the content behind the bar and results in some different colour. To achieve full control over the colour displayed on the navigation bar you would need to set translucent option to false for the navigation bar appearance method

The other bit of code is self explanatory. It just applied a background colour and shows how to add a logo to your app header