iOS

An Intro to CocoaPods

CocoaPods LogoIn today’s development landscape, dependency management is becoming an increasingly important problem to solve. As applications incorporate more and more external libraries (especially very active open source libraries), making sure that your application has the most up-to-date versions of those libraries becomes more tedious. Add in the necessity to make sure that every one of your dependencies has its dependencies met, you are definitely facing a headache.

If you come to Objective-C and iOS development from other languages, you might already be familiar with the solutions that those ecosystems provide. In .Net there is NuGet, Ruby has Gems, Node has NPM, etc. In Objective-C, the most popular dependency management system is CocoaPods.

CocoaPods is made with Ruby and you can get started with it with the version of Ruby that is on the Mac by default. All you have to do is type “sudo gem install cocoapods” (without quotes) in the terminal, enter your password, and you are off and running.

CocoaPods Install

The terminal will just sit and wait for awhile during the install. Unlike Homebrew, you don’t get any feedback during the installation. When it is finished, you will get a bunch of text for the change log for the version that you just installed and – if all went well – it will conclude with this message (the version could vary based on what is current when you are doing your install):

Successfully installed cocoapods-0.29.0
Parsing documentation for cocoapods-0.29.0
1 gem installed

After that, CocoaPods is ready to use.

For the first step, we need to have to make a new project (or use an existing project). Since we are starting fresh and new, I’ll just make a new project. I’m going to open Xcode and make a new project:

Xcode New Project

Then, I’m going to choose “Empty Application” and click next. (Note: you might see different options here based on what you configurations or installations you have on your machine.

Choose a Template

Then, we can fill out a little information about our application and click Next again. After that, choose somewhere to save it and finish up:

Set Project Options

After your project opens, go ahead and shut down Xcode. You are never going to intentionally directly open that .xcodeproj file again.

Open the terminal and navigate to the directory where you saved your project. First, we’ll create a podfile. A podfile is just a text file that keeps track of what dependencies you are taking and a list of the versions that you are willing to accept. We are just going to make a very simple podfile and take a dependency on AFNetworking, a very popular Objective-C networking client.

With your terminal active in your project’s directory, just type “touch Podfile”. That will make a podfile (no extension) in your directory. Then, you can edit that file in your favorite text editor. If you want to stay in the command line, you can type “vim Podfile” and then press i to go into insert mode. Then add the following text, followed by the key combination of Esc, then colon, then wq then . This will take you back to your terminal.

The file contents:

platform :ios, '7.0'

pod 'AFNetworking', '~> 2.0'

Here are those steps visually:

Touch Podfile

Podfile Contents

Type pod install and your pods will be installed and configured*
pod install

If you look at the project directory in Finder, you will now see that files and folders have been added.

After the Pod Install

The file that you care the most about now is the .xcworkspace file. If you are familiar with .Net, the .xcworkspace file is like the .sln file and the .xcodeproj file is like the .csproj or .vbproj files. The way that CocoaPods works, it makes your Pods code its own project and your existing code stays its own project and the .xcworkspace encompasses them both. As I mentioned earlier, from now on, don’t open your .xcodeproj file, but instead have Xcode open the .xcworkspace.

I find it handy to go File -> Open Recent -> Clear Menu from the Xcode menu so that my .xcodeproj file is removed from recent files and I don’t accidentally open it. Then, I can either double click on the .xcworkspace or choose “Open Other…” from the Xcode launch screen.

Now that the workspace is launched, you can see that the CocoaPods have been added to your code. Now you are able to import the header files into your code files and use your imported libraries as if they had always been there.

Podified Workspace

It might seem like we did a lot of steps to get to this point, but that was only because we had to include the initial CocoaPods gem installation. From now on, you just need to add a completed Podfile to your project directory, run pod install and then use the workspace from then on.

If you are the kind of person who likes to watch to learn rather than just read, you can check out a FREE video available over at NSScreencast about CocoaPods. Happy Podding!

* If you have an error when you run pod install, check out this StackOverflow, it solved my issue caused by a bad update to Cocoapods.

Podcasts

Podcast Episode 10 – Interview With Dustin Rogers

Dustin RogersFor Episode 10 (double digits!) of the Pete on Software Podcast, my special guest was Dustin Rogers. Dustin is an extremely skilled interactive designer based in Columbus, Ohio.

I know Dustin from a few projects that we collaborated on in the mobile space and I can vouch for the fact that he is very talented. He is also very practical and his designs not only look visually appealing, but they are also eminently usable and intuitive.

During this episode of the podcast, we talk about the tension between developers and designers, how to get through creative roadblocks, the importance of communication, the nature of inspiration, why infographics are awesome, and the exciting promise of mobile.

Dustin also recently did me a solid and created cover art for my Pete on Software Facebook Fan Page. He took my cartoon avatar from the podcast art and put him in a studio with a microphone and some headphones with the Columbus skyline. Very cool and something I never could have put together.

Dustin is also a very talented sketch artist and you can see some of his work at his site. And when you are done there, why not check out his interview?

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

Business of Software

Podcast Episode 09 – My Interview With Craig Schwartz

Craig SchwartzIn episode 9 of the Pete on Software Podcast, I interviewed Craig Schwartz. Craig is the owner and principal at Gecko Jones, a marketing and product management company whose main goal is helping your organization to reach the next level.

It was kind of interesting how I even got the interview. In Episode 6 of the podcast, I reviewed the Choose Yourself book by James Altucher. I tweeted out the podcast and James was kind enough to RT me. On that show, I put out a call for ideas or volunteers for interviews and Craig stepped up. That is the power of social media at work right there!

In the episode, I talk to Craig about product planning and marketing. We not only talk about how to know if you have a viable product and how to promote it, but we also talk about self-promotion and how to get along better with the “creatives” over in marketing.

If you haven’t already listened, check it out. It will definitely help you be a more well-rounded developer!

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

Android

Dynamically Preventing Rotation on an Android Fragment

Recently at work, I’ve been working on making an Android version of the company’s iOS application. The application itself runs entirely in portrait mode, except for the image of the benefits card that is included in the application. In iOS, that was easy enough to accomplish. In Android, the most common way to limit orientation changes is on a per-activity basis in the manifest, like this:

<activity
   android:screenOrientation="portrait"
   android:configChanges="orientation|keyboardHidden">
</activity>

Unfortunately, we can’t do that. Our application makes use of the “slide out” menu that was made popular in the Facebook application, but that you can now find as a very common pattern in many applications. The way that you accomplish this pattern in Android is by using the DrawerLayout and by using Fragments. Fragments are much like User Controls in Asp.Net WebForms or Partial Views in Asp.Net MVC or Rails. You have a container and you can load different layouts into that container, while maintaining the rest of the page’s layout as-is. The code that drives the fragment layouts has its own lifecycle (making it much more like WebForms User Controls than a Partial View).

The problem that this causes us is that we have one activity that sometimes we want to be able to rotate and other times, we want to keep it locked into place depending on which fragment is loaded at any given time. I searched and searched and could not find a solution that worked for us 100% of the time and was simple enough to implement. Eventually, I came up with my own solution to this problem.

The code for this entire project can be found on GitHub. For demonstration purposes, the project is based on a Sliding Menu project created by Ravi Tamada. Most of the code is his, with just the modifications I’m going to discuss here.

The menu looks like this:
The Slide Out Menu

When you click an item, it loads a fragment into the main window section. Here are the landscape modes of the Home Screen, followed by the Communities Screen:
The Home Screen in Landscape

The Communities Screen in Landscape

What we want, however, is for the Communities screen to remain in portrait mode no matter how we turn the phone, but the other pages can rotate. To accomplish this, I modified the method that is called whenever someone selects one of the menu items out of the menu.

/**
	 * Diplaying fragment view for selected nav drawer list item
	 * */
	private void displayView(int position) {
		// update the main content by replacing fragments
		Fragment fragment = null;
		
		// We allow the Sensor to be used in all instances by default
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		switch (position) {
		case 0:
			fragment = new HomeFragment();
			break;
		case 1:
			fragment = new FindPeopleFragment();
			break;
		case 2:
			fragment = new PhotosFragment();
			break;
		case 3:
			// In just this one instance, we turn the sensor off
			// Until a different menu item is selected, which re-enables it
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
			fragment = new CommunityFragment();
			break;
		case 4:
			fragment = new PagesFragment();
			break;
		case 5:
			fragment = new WhatsHotFragment();
			break;

		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.frame_container, fragment).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			mDrawerList.setSelection(position);
			setTitle(navMenuTitles[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
		} else {
			// error in creating fragment
			Log.e("MainActivity", "Error in creating fragment");
		}
	}

Notice that the first thing we do is allow the orientation sensor to work.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

However, as we are setting to load the fragment for communities (if that is selected), we turn the orientation sensor off.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

That causes the device to not rotate and to return to the original orientation, no matter how you are holding the phone when it loads.

After this change, we see the following. The home screen looks right in portrait mode:
After - The Home Screen in Portrait

Rotating it, does make it move to landscape:
After - The Home Screen in Landscape

But, when I choose Communities and we are in landscape, the app will only display portrait mode:
After - The Communities Screen in Landscape

That’s all there is to it. If you have a better solution, please feel free to leave it in the comments. If you want to check out the code and play around with it, here is the link to the repo again.

Podcasts

New Podcast – Preparing to Give a Presentation

Lavalier MicrophoneI just published my most recent podcast yesterday and it is my most favorite solo (non-interview) podcast yet.

In this podcast, I talk about the best ways to prepare to give a talk. In my case, my example was for my upcoming Stir Trek talk, but I also make sure to talk about ways that you can apply this preparation to business presentations, best man toasts, or oral book reports. I made sure to include a lot of practical resources and steps you can take to ensure that you can give a great presentation.

Preparation truly is key and I’ve seen some smart people give terrible presentations because they didn’t prepare properly, so I’m very paranoid about the subject. However, one of my old bosses really drilled the importance of preparation into me, so I take it very seriously and I’m pretty passionate about it. It doesn’t matter if you are leading a meeting, giving a keynote, or writing a blog. People can tell if you’ve put the background time in or not.

I’d definitely appreciate a listen.

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.