~If you would like to request a tutorial, email Collateral at collateraldamag3@gmail.com~

Share This!

iPhone Tutorial ~ UIButtons and Animation

in



So you want to animate your UIButtons but don't know where to start? Well here is a guide that will have your UIButtons jumping around in no time. For this tutorial we will be using the "UIViewAnimationTransitionFlip" Animation. Basically, it flips stuff. 
If you look up most other tutorials on the web, they will say that in order to flip a button, you need 3 things: 2 buttons and a containing view. This is NOT TRUE. I will show you how to flip a single button. 
First, create your button, either in the Interface Builder or programmatically. If you don't know how to do that, go learn and come back. 
So, once you have your button (mine is named "btnMyButton") , make a method that gets called on UIControlEventTouchUpInside. 




- (IBAction) btn1_click
{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5]; //Sets the Animation time to .5 Seconds

/*The Below Sets the transition to flip the button from the right. */
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:btnMyButton cache:YES];

[UIView commitAnimations];//Starts the Animation

}
That's it. Thats all you need to do to animate a button. Quite simple, really. The key that most people miss is that you can use a UIButton for the "forView:" key. Instead of assigning a UIView to this and flipping a button that is on top of the view, we are simply using the UIButton in the "forView:" key and flipping the button itself. 


~Collateral

Read More...

iPhone Tutorial ~ NSZombieEnabled and EXC_BAD_ACCESS

in


 EXC_BAD_ACCESS is an error received when a command is given to an deallocated target. Basically, you've released the object that your trying to send commands.  The only problem is, the error isn't very descriptive in telling you which object gave you that error, making it tricky to fix. 


Well there is a feature that a lot of people don't know about called "NSZombieEnabled". This makes it A LOT easier to find and debug Bad-Access errors. How it works is by leaving a dummy or "zombie" of every object you release in memory and then alerts you when you have sent a command to one of the dummies. Here is a picture of "NSZombieEnabled" in action:



It shows the exact same scenario as the top picture, except with "NSZombieEnabled" enabled. 
So how do we set this up? Its actually VERY simple. First right-click on your executable, and click "Get Info"


Then go to the "Arguments" tab and click on the bottommost plus sign (environmental variables). Then fill in the information as follows (REMEMBER TO CLICK THE CHECKBOX):



Then build and debug your program. It should now tell you the deallocated object that you sent a command to.
Just remember to uncheck the box when your ready to release. 
~Collateral

Read More...

iPhone Tutorial ~ Reachability and Checking Network Status

in


This is a tutorial on the Reachability class and checking your network status. Apple's Human Interface Guidelines for the App Store require any application that uses a network connection to check to see if that connection is active and if not, display an alert telling the user. Well, i spent a good 2 days trying to figure this one out, and hopefully with this tutorial, you'll be able to do it in 10 minutes. 
The first thing that is tricky about this is that the Reachability class that is used for this ISN'T EVEN INCLUDED IN THE SDK! WTF APPLE?!? The Class is actually found in one of Apple's sample code applications with the same name. I have downloaded that sample and extracted the Reachability class from it for you to download, here:
DOWNLOAD - REACHABILITY CLASS
You can download Apple's original sample code application in its entirety from HERE.


Unzip the Reachability class and add the Reachability.h/m to your Xcode project. This class also requires use of the SystemConfiguration Framework. So right-click on the "Frameworks" folder, go to "Add" and then "Existing Frameworks". 



Then click on "SystemConfiguration.framework". 


Alright, You have now successfully added the Reachability class to your project. Now how do we utilize it?
Well what we are going to do is check for network connectivity, and if there is none, alert the user. If there is, we will use NSLog to log in the console how the user is connected. 
First, declare a method: - (void)networkCheck; in your .h file.
Then, move to your .m file and fill in the method:



-(void)networkCheck{
Reachability *curReach = [[Reachability reachabilityForInternetConnection] retain];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
UIAlertView *connectionAlert = [[UIAlertView alloc] init];
[connectionAlert setTitle:@"Error"];
[connectionAlert setMessage:@"myApp was not able to reach the host. Please check your network conenction."];    
[connectionAlert setDelegate:self];
[connectionAlert setTag:1];
[connectionAlert addButtonWithTitle:@"Back"];
[connectionAlert show];
[connectionAlert release];
NSLog(@"NETWORKCHECK: Not Connected");
break;
}
case ReachableViaWWAN:
{
NSLog(@"NETWORKCHECK: Connected Via WWAN");
break;
}
case ReachableViaWiFi:
{
NSLog(@"NETWORKCHECK: Connected Via WiFi");
break;
}
}
}



First  we are declaring *curReach as our current network connectivity. We are then assigning that to a NetworkStatus object (Thanks to your Reachability Class) and preforming a switch-case on it. If it is "Not Reachable", we alert the user. If it connects, we log to the console by which method the connection is being preformed. 


Thats it :) That should get you set for compliance with Apple's HIG's. See ya next time.
~Collateral

Read More...

iPhone Tutorial ~ UISwitch Colors

in



Alternate UISwitch Color. If you've ever used an iPhone, you've definitely seen the orange "AirPlane Mode" switch, designed to say "Hey, I'm here, switch me!". Well here's a tutorial about how to do it yourself. But be careful, this uses undocumented API's and Apple can/will reject your app for doing this. 


But as for it being so naughty and getting your app rejected, its actually VERY simple and requires only one line of code:




UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 0, 0)];
[mySwitch setAlternateColors:YES]; // OrangeColor
/* Do Stuff Here. */
[self.view addSubview:mySwitch];
[mySwitch release];

Thats it. It's THAT simple. Thanks again to Skylar for showing me this tip.


~Collateral

Read More...

Android Tutorial ~ Installing the Android SDK

in


With the release of Verizon & Motorola's "Droid" phone series, I decided to get ready to start developing on the Android SDK. So here is a tutorial to get you started on installing Eclipse (JAVA IDE) and the Android SDK on your Windows, Linux or Mac system. 


First, what is Android? Android is Google's Open-Source mobile phone operating system. Android is based off of the linux kernel and is made to be an alternative to the iPhone OS or Windows Mobile. Android applications are developed in the JAVA programming language, allowing programs to be used on multiple different hardware configurations. This was a smart move on Google's part as JAVA is the #1 most used programming language according to developer.com. Objective-C (used for iPhone development) is not even on the list. 


To install the Android SDK, you are first going to need Eclipse. Eclipse is a free and open-source IDE used for development in JAVA, C, C++, PHP, and other languages. You can install Eclipse from their website, http://www.eclipse.org/. Make sure to install the "Classic" version, which is at the bottom of their "Downloads" page. 


Next you need to download the Android SDK from here : http://developer.android.com/sdk/index.html. Unzip it and put it somewhere you will remember it. I unzipped it to /Users/Collateral/Android SDK/. 


Now Open Eclipse and go to "Help" and then "Install new Software".




You will see a button titled "Add", click that. Under "Name", name it whatever you want, under "Location" add: " https://dl-ssl.google.com/android/eclipse/  ". It will download some stuff. Once you are back on the "Install New Software" menu, click on the "Work With:" drop down and click on the location you just added. 





Check "Developer Tools" and Install them. Restart Eclipse. Go to "Preferences" and click "Android" on the right hand side. Where it says "SDK Location" browse to the location where you unzipped the SDK that we downloaded.
Once you have done that, click "Finish" and then click the button. 







Click "Available Packages" and then check the android one. Click "Install Selected". Agree to All and wait for all of it to download and install. Restart Eclipse.




Congratulations, you now have the Android SDK installed! But we still need to set up an emulation product.




So press the  button again, and then click on "Virtual Devices". Click "New" and add a new device with these settings :







Click "Create AVD". Then Select your new device and click "Start". You should see this:













Congrats! You now have the Android SDK and a working Android Emulator!
Go ahead and start developing! :)


~Collateral

Read More...

iPhone Tutorial ~ NSUserDefaults and Settings.bundle - Part II

in




Ok, now that we have learned the basics of NSUserDefaults, we can now start to make a Settings Bundle. What is a Settings Bundle you may ask? A settings bundle is the application preferences that you see in the Settings.app. These are loaded automatically when you enter Settings.app and allow you to change the NSUserDefaults from outside the application itself; a very helpful feature.


To make a Settings Bundle we have to start off by making a folder named "Settings". Inside of that, make a new file in TextEdit and make it plain text. 






Then fill your plain-text document like this:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
   Root.plist
   Preference settings for MyApp
-->
<plist version="1.0">
    <dict>
    </dict>
</plist>
Once you are done, save it as "Root.plist". Now rename your "Settings" folder to "Settings.bundle". The settings folder should become a lego-block-like icon and the Root.plist should not be visible. Now, Open Xcode and open your project. Click "Project" and then "Add to Project".





Then choose your Settings.bundle. Click the arrow next to it in Xcode and you should see your Root.plist again. If you can, good, move on. If not, check to make sure everything is spelled correctly. "Settings.bundle" and "Root.plist" are CASE SENSITIVE.

Now as an example, we will add a username to the preferences. So open up Root.plist in the Property List Editor (Right Click and Select "Open with Finder"). You should see a key labeled "Root", click on it and then click "Add Child." Make the new child's key "Title", make its type "String", and make its value the name of your application. Now click on Root again. Make another child, this time with a key named "PreferenceSpecifiers". Make its type "Array".

Now click on "PreferenceSpecifiers" and then on "Add Child" (You should now be adding a child to PreferenceSpecifiers, not to Root). This items key, by default, should be "Item 0". Make its type "Dictionary".

Almost done. Now add 4 children to "Item 0" and make them match what I have below:



The "Title" key for Item 0 is the title that the user will see when he/she edits this in Settings.app. The "Type" key tells Settings.app that it is a PSTextFieldSpecifier, which is basically a UITextField. The "Key" key tells the program which key we are loading from in our code. NOTE: THIS IS OUR VARIABLE NAME FOR INSIDE THE CODE. See Here. And the default value is pretty self-explainatory. It is the value that is loaded if none is entered.

Now save Root.plist. Compile and run your app in the simulator. Exit the app and enter Settings.app. Check to see if there is an entry for your app at the bottom. If there is, then you have done it correctly.

For this example, the code to load the username we stored into a NSString would be:








NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *strUserName = [prefs stringForKey:@"userName"];
Here are the other types for storing NSUserDefaults into your Settings.bundle:






  1. PSTextFieldSpecifier
  2. PSTitleValueSpecifier
  3. PSToggleSwitchSpecifier
  4. PSSliderSpecifier
  5. PSMultiValueSpecifier
  6. PSGroupSpecifier
  7. PSChildPaneSpecifier
Congratulations! You now have a working Settings Bundle and you can store and load information from NSUserDefaults!

Go Back to Part I

~Collateral

Read More...

iPhone Tutorial ~ NSUserDefaults and Settings.bundle - Part I

in


Alright, This is a tutorial on the NSUserDefaults feature and how to use them in conjunction with Settings.bundle.


First off, What is NSUserDefaults? If you want the "Apple Definition" you can find it HERE. But basically, NSUserDefaults is a way to store data for a another point in time. You can use NSUserDefaults to save a character's level, or what items he/she's carrying. Or You can use it to remember a username and password or whether the user prefers a light or dark GUI. Basically, it saves stuff. 
So how do I use NSUserDefaults? Well, Apple has made it VERY easy actually. There are two ways to access data in NSUserDefaults, saving data and loading data. We'll start with saving.





NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger: 0 forKey: @"whatever"];//Save
[prefs synchronize];


 That ^^ is a very simple example of saving data to NSUserDefaults. I'm starting off by labeling [NSUserDefaults standardUserDefaults] as just simply "prefs", mostly because prefs is about 20x easier to write.  I am then saving "0" as an integer to the key "whatever". Now, you may be asking, what is a key? A key is basically a save slot. whenever you have an option you have a slot. For example, if you have a game where you have to save the character's inventory, you will probably store an array of the character's objects into a key named "inventory". Basically a key is a spot in the NSUserDefaults where certain info is stored.  And [prefs synchronize] just writes it to the disk.


Here are the different syntaxes you can use when saving to NSUserDefaults (of course all linked to their reference :p):





NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *strUserName = [prefs stringForKey:@"userName"];




Here I am loading a String from the key "userName" and storing it into a NSString titled "strUserName". Easy Peasy. Here are the syntax's and references for what you can use while loading data. Be careful it IS different from the first list.






Read More...

iPhone Tutorial ~ Inserting UITextFields into UIAlertViews Automatically

in


So I noticed a lot of people are trying to get UITextFields into UIAlertViews by manually generating one and fixing the coordinates, etc. But What a lot of people don't know is that Apple has an easier (non-documented) way that they do this, in apps like iTunes, etc. 


// First Let's Make a UIAlertView Like Normal..



UIAlertView *myAlert = [[UIAlertView alloc] init];
[myAlert setTitle:@"ALERT TITLE"];
[myAlert setMessage:@"HELLO WORLD"];
[myAlert setDelegate:self];
[myAlert setTag:1];
[myAlert addTextFieldWithValue:@"" label:@"ADD LABEL HERE"];
[myAlert addButtonWithTitle:@"OK"];
[myAlert addButtonWithTitle:@"Cancel"];


// Now Let's Use Apple's Special Way of Adding a UITextField with little to no work
txtInput = [myAlert textFieldAtIndex:0];
txtInput.clearButtonMode = UITextFieldViewModeWhileEditing;
txtInput.keyboardType = UIKeyboardTypeDefault;
txtInput.keyboardAppearance = UIKeyboardAppearanceAlert;
txtInput.autocorrectionType = UITextAutocorrectionTypeNo;
txtInput.secureTextEntry = NO;

[myAlert show];
[myAlert release];
Clear Button Mode adds the little "X" while typing to clear the UITextField. Keyboardtype obviously sets the type of keyboard. Here are your options for that:



UIKeyboardTypeDefault

Use the default keyboard for the current input method.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypeASCIICapable

Use a keyboard that displays standard ASCII characters.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypeNumbersAndPunctuation

Use the numbers and punctuation keyboard.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypeURL

Use a keyboard optimized for URL entry. This type features “.”, “/”, and “.com” prominently.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypeNumberPad

Use a numeric keypad designed for PIN entry. This type features the numbers 0 through 9 prominently. This keyboard type does not support auto-capitalization.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypePhonePad

Use a keypad designed for entering telephone numbers. This type features the numbers 0 through 9 and the “*” and “#” characters prominently. This keyboard type does not support auto-capitalization.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypeNamePhonePad

Use a keypad designed for entering a person’s name or phone number. This keyboard type does not support auto-capitalization.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

UIKeyboardTypeEmailAddress

Use a keyboard optimized for specifying email addresses. This type features the “@”, “.” and space characters prominently.
Available in iPhone OS 2.0 and later.
Declared in UITextInputTraits.h.

and Finally, secureTextEntry allows you to change the characters to dots if you are entering a password.

And of course, you can add more than one UITextField to a UIAlertView, so this should please all of your username/password needs.

~Collateral


Read More...