Hello guys, In this tutorial, I am going to tell you that how we can integrate a custom framework in our Xcode project using cocoa pods.
Things we need to do this tutorial are:
- Macbook or system that supports Mac OS.
- Application on which we will create the whole application i.e. – XCode.
- Some basic knowledge of programming.
What is XCode?
XCode is integrated development environment (IDE) tool developed by Apple contains the software to create the applications for iPhone, ipads, tvOS, macOS,watchOS.
This tool supports the programming language like C, C++ etc and makes the use of all the languages to develop the application in a simple way. Right now the updated Xcode is version 9.2!
I am gonna use the Xcode version 8.3.3 for this tutorial which makes go easy with almost every version.
As we all know that Apple provides us the ability to integrate different type of frameworks within the application so that we can use and update them according to our requirements.
What is a Framework?
A framework contains a bundle of data and its content which used core foundations library of apple. Through this, we can create our own custom applications and can edit and update them according to our wish or choices.
So today we will be going to create a dummy project by adding the google maps framework to it using cocoa pods.
Normally there are two types of methods which can be used to install the frameworks into your project and we will discuss both the methods side by side.
Let’s first follow the method which is importing the frameworks into the project directly.
Download the google maps SDK frameworks kit from their official website. I am giving the link below.
After downloading the framework library. Go to the downloaded folder where the file is present and double-click on it to extract the folder.
Launch the Xcode or open the existing project. If you are new to iOS then create a single view application and name it according to your requirement.
After that just head back to the main storyboard.
Open the folder which you have extracted earlier and drag and drop the bundle files into the project as mentioned below. I am showing you the path of the project so that you can find them into the downloaded folder easily.
File 1. GoogleMaps-2.5.0/Base/Frameworks/GoogleMapsBase.framework
File 2. GoogleMaps-2.5.0/Maps/Frameworks/GoogleMaps.framework
File 3. GoogleMaps-2.5.0/Maps/Frameworks/GoogleMapsCore.framework
Now right click on the GoogleMaps.framework and select Show in Finder.
After that drag the googlemaps.bundle file from the framework and drop it into the project from Resources folder.
Note- remember that while dragging the file into the project there is a prompt will appear just confirm that copy items into destination group’s folder is not selected.
Now select the project from navigation and choose your application target.
Open the build phase tab and within it select the option Link binary with libraries.
After that add the following important frameworks into your project each at a time.
- GoogleMapsM4B.framework (Premium Plan customers only)
Now you can import the following framework in you viewcontroller.m file and can start developing the application.
The second method is by using the cocoa pods.
Before we start this project we need to understand what is cocoa pods?
Basically, cocoa pod is a dependency for objective-c projects.it means that you can integrate a third-party library into your project and improve the efficiency of the application.
Now, we understood the meaning of cocoa pods we can move ahead and jump directly into the project. Below are the steps required to be followed within the development of the application:
Go to Xcode and click on create a new project and right next to it you need to select single view application and hit next.
Here you need to give a name to your application. I am using DummyGMaps name for my project. You can choose whatever you want for yours and hit next.
Select the location for the application so that you can save it and can access it in future to continue work on it.
Now as we are ready with our setup, the last thing we need to create is a pod-file through which we will going to add google maps framework into our project.
Open a terminal, and hit the command cd desktop right after the $ sign as shown in the snapshot below.
Now add the folder where your project is present into the terminal command, and the shortcut to do it is just dragged and drop the folder into the terminal after entering cd into it and hit enter as shown below.
Enter the command pod in it to initialize the pod file into your project and this will create a pod file within your project which will create an environment to add third-party frameworks into your Xcode projects as shown.
Now when you go into your project folder you will see a podfile will be there. It means that you are going to the right path and can now integrate the google map framework into it.
Now you need to open the pod file through some editor and for doing this I am going to use a Sublime text editor to open and add the name of the framework into that podfile, it is up to you and your choice to use any editor according to your comfort level.
After opening it you will see a lot of names will be there just go above end word and enter the name of the framework you want to add into it, for Google maps I am going to use – pod ‘GoogleMaps’ and pod ‘GooglePlaces’ and save the file after doing this.
Now again go back to the terminal file and hit the last command pod install right next to the $ sign as shown. This will install the google maps framework into your project but will take some time according to the speed of your internet connection so just wait for a while and have a cup of coffee with you until it gets installed into your project.
Now I think it will successfully installed the library into your project if you follow all the process shown above.
Please note that from this point onwards you need to use a .xcworkspace file rather then .xcodeproj as it will provide you the framework access you integrate into your project and this file will be there into your project folder itself.
Congratulations!! You have successfully integrated a framework into your project.
Feel free to use the commands above to integrate other frameworks into it, all you need to do is just change the name of frameworks and you are good to go.
As we have integrated the google maps framework into the project now let’s create a simple map by using the same project.
I am going to make this tutorial as simple as I can so just follow the steps that’s it.
Click ==> Here <== This link will redirect you directly to the google maps page and you can click on the get key button to get the unique key to access all features of google maps.
Hit on the Get a Key button and create the name of the application, I prefer using the same name for your app you used in Xcode and hit next as shown below.
Copy the unique API key and save it into some file as we will going to use it soon into our project and click done after that.
You can also restrict your key or app to run on only ios application and to do it just head to the dashboard within the google API console and in the restriction section select iOS apps then enter your bundle id which is present in your Xcode project and click save.
Go to the Google Api Console and select your application which you have created and go to the dashboard.
There you will see a line named as Enable Apis and Services just click on it to enable all the services link to your project as shown below.
Now we are halfway home and to finish it we now need to do some coding part into the Xcode project side, so to do it just head back to your Xcode project.
Add the Api Key in AppDelegate.m file as follows.
- @import GoogleMaps;
- Add the following to your application:didFinishLaunchingWithOptions: method.
First, Add a map to your view controller.
#import <GoogleMaps/GoogleMaps.h> to your viewcontroller.m file.
Secondly, In your view did load method add the following code:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:28.628454 longitude:77.376945 zoom:6]; GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; mapView.myLocationEnabled = YES; self.view = mapView; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.position = CLLocationCoordinate2DMake(28.628454, 77.376945); marker.title = @"Noida"; marker.snippet = @"Sector-63"; marker.map = mapView;
Now build the application and you will be able to see the running map in your application with a market in the centre of Noida location.
Some Points To Remember:
While making the application using a custom library always remember that there are certain important things to use within the application.
Ex – Always import the files into the view controller before start the coding part as it can show you the clues while writing the code which can be more useful for the first time.
Before running the project always save and clean the build then create the build so that any remaining work can be saved and did not lost while making the build.
Understand the difference between two tags which is:
#import and @import
Sometimes what happened that when you import a file into view then it won’t appear for the first time then you don’t need to panic .
Use #import when you use drag and drop method to link library.
Use @import when you use cocoa pods while linking the library to the project and then you are good to go.
If you wish to import the places API in your application to create your custom map or adding the feature of selecting the custom location and shows it into the map then you can do it by using the GooglePlaces API.
I hope this tutorial helps you a lot while creating your application.
For your reference, I am going to provide you the link to the google maps API SDK and other useful things down there in the reference section.
Till then enjoy being a coder and feel free to give your suggestions below in the comments.
References For Library:
- Google maps API iOS:- https://developers.google.com/maps/documentation/ios-sdk/start
- Cocoa Pods:- https://cocoapods.org/
- Google Places:- https://developers.google.com/places/ios-api/start#step-2-install-the-api
- X-Code:- https://developer.apple.com/xcode/
Hope this blog serves all your queries related to the integration of Google Maps Framework in Xcode Project. Anythough for further queries, please feel free to ping me through the comments section below!