How to use XCode Application loader

how to run xcode application in device and how to make xcode application and xcode how to launch another application and how to run xcode app on iphone
Dr.KiranArora Profile Pic
Dr.KiranArora,Canada,Teacher
Published Date:27-10-2017
Your Website URL(Optional)
Comment
An Introduction to Xcode In the last chapter, we saw the Playground tool. It's a simple tool to test your code, but if you want to actually create an application and run it on a device or publish and distribute it through the Mac or iOS App Store, you need to create an Xcode project. In this chapter, we will take a tour of Xcode. We will take a detailed look at the interface, create a very basic "Hello World" application, and make it run on the simulator and later on the device. We will be covering the following topics extensively in this chapter: • Xcode application types • The Xcode interface • Running the app on the device 69 An Introduction to Xcode Xcode application types Xcode comes with some predefined application types for creating some specific types of applications. They are the Master-Detail Application, Page-Based Application, Single View Application, Tabbed Application, and Game types, as shown in the following screenshot. We have already seen what is in the Game type, so let's look at the other types: Master-Detail Application If you want to create a very basic application type, such as the Notes application, for the iOS, you should choose this template and then modify it to create your application. There is a Master list (shown in the following screenshot), and when you click on the each item on the list, it will show the details of the selected item from the list. There is a customizable Master button in the top-left corner of the Detail view. Clicking on it will take you back to the Master screen: 70 Chapter 3 You can create the project in a way similar to how you created the Game application in the first chapter: click on the icon for the type of application, and then select the name of the project, its location, bundle ID, and platforms that you want to create the app for. Page-Based Application If you want to create an application that resembles a book with a page-flipping effect, you need to choose the Page-Based Application template. You can specify the book cover and then add content to the pages to create a flipbook application. You can swipe your fingers to the left to turn to the next page, or swipe to the right to go back to the previous page. 71 An Introduction to Xcode Tabbed Application The Tabbed Application template will have a row of buttons at the bottom of the screen, and clicking through it will show you different views. A Tabbed Application template is similar to the way the tabs work when opening the Music app on your device. When the Music app is opened, the tab displayed is Playlists. The other tabs are Artists, Songs, Albums, Genres, Compilations, and Composers (the last tab). The Playlist tab shows the playlists that you have stored, but if you click on the Artists tab, it will show a list of songs that has been sorted by artists. If you build the default application, you will have two tabs: First and Second. Clicking on the tab at the bottom will open the respective screens, like this: Single View Application The Single View Application template is the most basic one. If your application doesn't adhere to any of the previous application template types, then you can use this to create your own applications. For this chapter, we will start from the basics, so we will create a Single View Application. Click on Xcode to open it and then on Create a new Xcode project. Under iOS, select Application on the left panel. Then, select Single View Application and create it. When you create a project and run it, the simulator will open up to show a blank screen, as shown in the following screenshot. When I said Single View Application templates are the most basic, I wasn't kidding. You will get a single view controller and no buttons to navigate. The advantage here is that you will see some basic classes that Apple already provides for creating the app from scratch, and then you can add items of your own to make it your app. 72 Chapter 3 You should save this blank project, as we will be referring to it when we cover the interface of Xcode in the next section. So go ahead; name the file and save the project in the Mac. The Xcode interface If you are continuing from the previous section, then open the single view application you created in the previous section. Otherwise, you can create a new Single View Application project. As we will be spending most of our time looking at this window for the rest of the book, let's get a detailed understanding of its layout. Open the project and you will be greeted with the window that is shown in the following screenshot. The top area of the Project view is called a toolbar. Below the toolbar on the left is the Project Navigation Panel. Right under in the middle is the Editor panel, and to the right of that is the Utilities panel. There is one more panel, the Debug panel, that might not be open by default, but we will activate it later: 73 An Introduction to Xcode There is a lot of information in each of the toolbars and panels. Let's look at them individually. The toolbar The top of the Xcode project is the toolbar. Next to the maximize button on the left, the play and stop button are for running and stopping the application. Next to these buttons are the schemes in which you can select the target app and the device that you would like to run the application on, whether it is an actual device or a simulator. At the center of the toolbar is the Activity View. This shows the status of the application at any given time. When you build a project, it will show the progress of the build, displaying the various stages of the build process. To the right of the Activity View, there are two sets of three buttons. The r fi st set of three buttons on the right is used to change the Editor panel according to your needs: • Standard Editor: This is activated by default whenever a new project is created. This setting is used when coding, debugging, or navigating through the project. We will be using this panel almost all the time. • Assistant Editor: Click on this button and another panel will open up to the right of Standard Editor. This is called Assistant Editor. It will provide more information, depending on what is clicked on in Standard Editor. In the following example, in the navigation panel, I clicked on theMain. Storyboard file. The Standard Editor changes to display the objects in the file, and the Assistant Editor shows the contents of the class associated with the View Controller as it is selected in the Standard Editor. 74 Chapter 3 • Version Editor: When selected, the Standard Editor is again split into two panels, and we will be able to see and compare the changes in the current and previous versions of the code. In the example shown in the following screenshot, I made some changes to theViewController.swift file. The comment added is highlighted on the left panel, and the right panel shows where these changes are being made to the file. This is obviously very powerful when using source code management for your project. Source Control Management is already included in Xcode and is initiated when a new project is created. The three buttons to the right of the editor are used to hide and show the Navigator, Debug, and Utilities panels. These panels can be shown or hidden as per your requirement or convenience. For example, to take the preceding screenshot and get more screen real estate, I hid the utilities panel by clicking on the rightmost button. That is all for the toolbar. Let's look at the Navigation panel next. 75 An Introduction to Xcode The Navigation panel Below the toolbar on the left is the Navigation panel. It has eight tabs. From left to right, they are Project Navigation, Symbol, Find, Issue, Test, Debug, Breakpoint, and Report Navigator. Clicking on any tab will activate it. By default, the Project Navigation tab is selected. The Project Navigation tab The Project Navigation tab shows the content of the files that you have in a project, including code files and asset files. Later, if you add frameworks to the games, those will also be displayed is this panel, making it easier to navigate through. As we saw earlier, you can edit code by clicking on a code file such as a .swift file, and in the editor window, you can add, remove, and modify code. If you have image assets and three-dimensional meshes, those files will also be shown here. By clicking on the images and three-dimensional files, you can view the content, but you will not be able to modify it. For that, you will need to open the file in applications such as Photoshop to edit images and three-dimensional packages such asMaya or3dsmax to edit the three-dimensional geometry: You can organize a set of files using folders (called groups here). Keep in mind that this is meant just to organize files in Project Navigator. Creating a new group or folder in the Navigation panel won't create a folder in the project directory in the system. 76 Chapter 3 The Symbol Navigator tab The next button to the right is the Symbol Navigator tab. You can access all the symbols such as functions, methods, properties, classes, structs, enums, and variables in the project. C denotes that it is a class, M stands for method, and P stands for property. There are other notations also, but these are the most basic notations you will usually come across. So, in theAppDelegate class, there are six methods and a property calledwindow. 77 An Introduction to Xcode The Find Navigator tab The Find Navigator tab is used to search for phrases or files in the project by name. In the following example, I am trying to search for the termUIKit, and it looks as if it is imported in the three classes: You can also choose to search for the keyword in the current score, or create a new scope to look in other places. Also, by clicking on the Find drill-down arrow, you can choose to find or replace commands on text that contain, match, start with, or end with your search term. 78 Chapter 3 The Issue Navigator tab The errors and warnings list, count, and description will be displayed in the Issue Navigator tab whenever your project has build errors. You can click on the items in the list to show the location and file where the error exists: In the preceding example, I forgot to close the comment section in the ViewController class, so I get an error after building the project telling me that in ViewController.swift, there is an undetermined/ comment. In some cases, the error message may not be very explicit, but at least, you will get a hint of what might be causing the error. 79 An Introduction to Xcode The Test Navigator tab If you are using unit tests on the project, then the results of the test are displayed in the Test Navigator tab. Unit testing is a topic in itself, and is unfortunately beyond the scope of this book. To know more about unit testing, you can look at Apple's official documentation for more information athttps://developer.apple.com/ library/ios/recipes/xcode_help-test_navigator/Recipe.html//apple_ ref/doc/uid/TP40013329-CH1-SW1. The Debug Navigator tab The Debug Navigator tab is used for debugging and showing how optimized your code is. This navigator is active only when an application is running. While an application is running on the simulator or on a device, we can get information regarding CPU, memory, disk, and network usage. Apart from general system information, you also get a stack of methods and functions that were called in the corresponding order. Clicking on the methods and functions will open them in the editor panel. 80 Chapter 3 There are two buttons to the right of the running app. The first button on the left lets you hide or show the gauges, and the button on right lets you select whether you want to view the Process by thread, Process by Queue, or View by UI Hierarchy. The Breakpoint Navigator tab The Breakpoint Navigator tab shows the location of all the breakpoints we've added to the files in the project. The location of the breakpoints is shown class- and method- wise. It will also show the line number on which the breakpoint was added. By right-clicking on the any breakpoints, you can edit, disable, share, delete, or move it. The Report Navigator tab The Report Navigator tab shows the history of the recent builds and logs of the project, along with a timestamp: This concludes the section on the Navigation panel. 81 An Introduction to Xcode The Utility panel The top part of the Utility panel is context-sensitive, depending on what kind of file is clicked on in the Navigation panel. The bottom part of the Utility panel has four tabs for the different library types used to drag-and-drop certain library-specific objects onto the Editor panel. Sometimes, the top part gets hidden under the bottom part of the panel, but you can drag the bottom part of the utility to reveal the content hidden under it. The Utility panel is better understood once we jump through the project and look at each file individually. The Single View Project Go back to the Project Navigator tab. This is the first tab from the left in the Navigation panel. Let's look at the files in the Project Navigator tab. The Project Navigator tab shows the project root and under it, all the files associated with the project. Let's look at the project root first. The project root When you click on the root of the application that is at the top of the tab, you will see the Editor panel change to display six tabs: General, Capabilities, Info, Build Settings, Build Phases, and Build Rules. Most of the time, you will be concerned with the first three tabs: General, Capabilities, and Info. General This tab contains the basic information about the app. We had a brief look at it in Chapter 1, Getting Started, when we changed the orientation of the game. This tab has five subsections, as shown in the following screenshot: 82 Chapter 3 The preceding screenshot has the following sections: • Identity: This shows the bundle identifier, version number of the app, build number of the app, and team. We will look at the team when we deploy the app on the device, as it requires some steps to get it. • Deployment Info: Here, you can select the value of Deployment Target. By default, we are targeting iOS 8.1 devices here, but if we want our app to be compatible with previous version, we can use 7.0. However, we must make sure we are not using any APIs from 8.1, in which case the app will give build errors. We can select the device that we want to target, whether we want to target the iPhone or iPad, or make a universal app. The main interface file is the first file that will be called when the application has finished loading. The files here should have the.storyboard extension, so here we are calling theMain.storyboard file when the application launches. We can also change the device orientation here and select the value for Status Bar Style, and also hide the status bar by clicking on the check box. 83 An Introduction to Xcode • App Icons and Launch Images: The source file for the app icons is provided here. The AppIcon file is selected here. If you are wondering where this file is, it is in theImages.xcassets folder. We will use this file to assign the icons for different iOS versions and devices. If you want an image to be displayed at launch of the application, you can create an asset catalog file to display the launch images for different devices and iOS versions, similar to icons. We can also specify a launch screen file, which will be displayed while the application is launching. Here, theLaunchScreen file will be displayed during the launch. The file extension for theLaunchScreen file, which was in .nib format previously, should be in.xib format. Even though the extension has changed, they are still referred to as NIB files. • Embedded Binaries: This shows any binaries that are embedded in the project. • Linked Frameworks and Libraries: This displays the list of frameworks and libraries included in the project. The Capabilities tab This shows the Apple services that the application uses. To use a service, you will need to activate it by turning it ON on the right-hand side. Games usually use services such as GameCenter, In-App Purchases, and iCloud integration for cloud saves. 84 Chapter 3 The Info tab The Info tab contains information regarding the version number, build number, storyboard file base name (which is the storyboard file to be loaded at start of the app), app name, and some other information. All of this information is loaded from theinfo.plist file, which is located in the Supporting Files folder of the project. There are some other things such as document types, exported UTIs, imported UTIs, and URL types, which, for the most part, you won't be dealing with during the course of this book. 85 An Introduction to Xcode The Build Settings tab The basic view of the Build Settings tab shows information regarding deployment, such as the target device and iOS version, location of the path for frameworks, packaging info such as location of theinfo.plist file and Product Name, Asset Catalog App Icon Set Name, and other user-defined settings. A more detailed look can be taken by clicking on the All tab instead of Basic. This will give information about the setting for the architectures supported, locations and options for the build, and code signing, which we will cover when we are ready to test the app on the device and deploy it on the App Store. It has further settings and information regarding the kernel module, linker, and compiler. 86 Chapter 3 The Build Phases tab The Build Phases tab shows the target dependencies added, list of source files, libraries added, and asset resources added. This tab is sort of important, in the sense that when you want to add frameworks to your project, you will have come to this tab to include them. Also, sometimes when you get build errors, you might want to check whether all the required source files are actually in the Compile Sources list, as this can be the reason for build errors. Build errors can be introduced once your project becomes bigger as you might end up deleting some source files. 87 An Introduction to Xcode The Build Rules tab You will probably never be required to change anything here, as it is mostly used if you want a specific file type to compile in a certain way. To define a custom process for a certain file type, you just have to create a new build rule. Now that you have taken a look at the project root, you can click on the small triangle button to the side of the project root to open the project tree, if it is not already open. Under the created project, there are three groups. The first is given the same name as the project. The second group holds the files for the project test files, so it will always have the project name and be suffixed with Tests. The third is the Products group, containing the.app file and the Tests files. For the majority of the time, we will be dealing with the files in the first group, that is, the group names after the name of the application. The first folder is where all the code, assets, and project-related files for the project should exist. You can create subfolders like theSupported Files folder to organize your project better. So, you can get aClasses folder that contains all the classes for the project, and an assets folder in which you can put your images, icons and three-dimensional objects. Let's look at each of the files of the main project folder in detail. 88