iPhone Code How to: Call a number from a function or button click

Below is the code to call a number within a function.

NSURL *phoneNumberURL = [NSURL URLWithString:@"tel:3053729787"];
[[UIApplication sharedApplication] openURL:phoneNumberURL];

I've tested this call function on both the iphone and ipod. The iphone opens a UIAlert and prompts the user to call or cancel. The ipod ignores the call without causing a crash (same as the simulator)

iPhone How to: UItextField Value to Show Float with Zeros

To have a UILabel print out a price float with 2 decimal places you'll want to cast a string with to except a float with 2 decimal places.

myFloatLabel.text = [NSString stringWithFormat:@"$%3.2f", [self.floatValue floatValue]];

Internet Explorer Bug: ie6 cuts off bottoms of words

A Frequent Problem with ie6: The bottom of the text (e.g. g y p) get cut off and make text illegible. This mostly happens with two line breaks and can be fixed by using a larger line-height in css.

To fix the problem we are going to create a class using the internet explorer 6 box hack.

We need to locate the exact area that we are having the problem with. For me the text was being cut off at .description h4 a

* HTML .description h4 a {
      line-height: 1.2em;
}

To fix the text cut off issue without the box hack just simply created a css class and adjsut the line-height

p { line-height: 1.2em; }

No Access to iPhone running OS 3.0.1 though XCode Organizer

After updating the iPhone in iTunes to Software OS 3.0.1 I was unable to access it through the xcode organizer. After reinstalling the newest xocde (failed) and some digging I found a line of code you need to copy and paste into your terminal once, restart xcode, and you have access to pushing onto the iphone again.

Open up Terminal, copy below, paste and enter into terminal, restart xcode.

ln -s /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0\ \(7A341\) /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0.1

iPhone SDK CoreData Debugging Error 1560 & 1570

One of my CoreData applications started acting buggy, and was spitting out a huge error dump on app close in the debug console. The errors spitting out didn't tell me anything but CoreData Error 1560 and 1570. Upon inspection I found on google that another layer of errors is stored in the CoreData foundation. On Stack OverFlow found a very useful function but re-wrote it because

![[survey managedObjectContext] save:&error]

was causing an error.

Add this into you applicationWillTerminate function in your Application Delegate

/**
 applicationWillTerminate: saves changes in the application's managed object context before the application terminates.
 */

- (void)applicationWillTerminate:(UIApplication *)application {
       
        NSError* error;
        if(![[self managedObjectContext] save:&error]) {
                NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
                NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
                if(detailedErrors != nil && [detailedErrors count] > 0) {
                        for(NSError* detailedError in detailedErrors) {
                                NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                        }
                }
                else {
                        NSLog(@"  %@", [error userInfo]);
                }
        }
       
}

The original post can be found here

The cause of Error in my CoreData Application was an Attribute in my xcdatamodel file. An optional field was checked as indexed and not optional. The simple action of unchecking indexed and checking optional fixed a whole mass of errors.
CoreData Error

How to Add a Home Navigation Button to the iPhone NavigationController

A quite useful button for a navigation controller with a deep stack would be a home button. It is simple to add.

In your RootViewController viewDidLoad Function

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *homeButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Home", @"")       style:UIBarButtonItemStyleBordered  target:self action:@selector(goHome:)] autorelease];
    self.navigationItem.rightBarButtonItem = homeButton;
}

That will add a home button in to top right of your navigation controller

Create a function called go home in your rootViewController

// return to root view
- (void)goHome:(id) sender {  
   [self.navigationController popToRootViewControllerAnimated:YES];
}

This function will pop your view controller to its root view

iPhone Form Submission Validation: UITextField Value Error Checking Required Fields

An iPhone Application we recently produced had a order form on it. We needed to create a function that would run through all our UItextFields, extract their values, loop through, alert the user if a UITextField wasn't completed, and execute a function (our ajax request) if all checks out.

What 's involved in this function

  • alertview - a pop up that lets the user know they missed a required field
  • array - a list of all the UITextField values
  • int - a variable that increment everytime time a UITextField Checks out
  • for loop - Loops through the Array
  • if statement - Check the array count to the increment count

This function assumes you know a bit about the iphone, have declared and linked your IBOutlet UITextFields, and have linked your submit button in the Interface Builder to  -(void)textFieldCheck:(id) sender; (which would be declared in your header file).

-(void)textFieldCheck:(id) sender {
        // Declare your Alert,  NSArray, increment int
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please Fill in All Required Fields." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        NSArray *fieldArray;
        int i = 0;

        // Load up our field array with the UITextField Values
        fieldArray = [[NSArray arrayWithObjects:
                  [NSString stringWithFormat:@"%@",orderName.text],
                  [NSString stringWithFormat:@"%@",orderPhone.text],
                  [NSString stringWithFormat:@"%@",orderAddress.text],
                  [NSString stringWithFormat:@"%@",orderEmail.text],
                  [NSString stringWithFormat:@"%@",orderQty.text], nil] retain];

        // loop through the array, alert if text field is empty, and break the the loop, other wise increment i  
        for (NSString *fieldText in fieldArray){
                NSLog(fieldText); // make sure all is reading correctly in the console
                if([fieldText isEqualToString:@""]){
                        [alert show];
                        break; // break out!!
                }
                i++;
        }
       
        // check that all the field were passed (i == array.count) if so execute
        if(i == [[NSNumber numberWithInt: fieldArray.count] intValue]){
                NSLog(@"Its all Good"); // some console fun
                [self sendOrder]; //where you execute your request function            
        }
       
        // release it all
        [alert release];
        [fieldArray release];
}