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

Share This!

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...

iPhone Tutorial ~ In-App Email

in


Ok, so here is a tutorial for getting the Mail.app modal view in your application for OS 3.0+ and to launch Mail.app from your application if your on 2.X.X.
*Heads up: Clicking on the images will zoom them ;) 
99% of the time, this is going to be run as an event, or a response to user input (pressing a UIButton, responding to a UIAlertView, etc.), so right off the bat, you know its going to be a -(IBAction) or - (void). 
First, however, we are going to want to import the mail framework, titled "MessageUI.framework". So right click on the "Frameworks" folder, then click "Add" then "Existing Frameworks" then "MessageUI.framework". Now you should see this: 



Then you are going to want to add this to your import statements: 



This will allow you to access those oh-so-special frameworks you just added. We now just have to add the methods to call the Mail.app modal view.  So add these to your .h file under your @property calls.



- (void)displayComposerSheet;
- (void)showPicker;
- (void)launchMailAppOnDevice;
- (IBAction)callMail;


 This is the call for the methods that we are going to define in your .m file. Speaking of which, turn to your .m file now, and I will break down what is necessary to call to make this puppy work. 
First, we need an -(IBAction) to call our method. This is what callMail { } is for. So make a UIButton and assign callMail { } to it. If don't know how to do this, then the easiest way in in the Interface Builder. In the IB, click on "File's Owner" and then on the "Connections" tab in the inspector. You should see "Received Actions" and under that, "callMail". Click on the "+" sign next to callMail, and drag it to your UIButton. It will give you a drop-down menu. Choose "Touch Up Inside." As shown: 


Save your .XIB and go back to your .m file. Type: 


- (IBAction) callMail{
[self showPicker];
}




Ok, now your - (IBAction) is done. Wasn't that easy? Now here comes the method definitions. Just type them in exactly as i write them:




-(void)showPicker
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}


This is the method we just called.  This method checks if the device can bring up the modal view. If it can, it calls the method to do so. If it can't, it tells the divice to open Mail.app. Now we need to call the modal view itself:




// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"SUBJECT HERE"];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"bob@john.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:@""];

[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];

// Fill out the email body text
NSString *emailBody = (@"BODY TEXT HERE");
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
    [picker release];
}


The above will call the mail composition GUI as a modal view inside your application. From here, you can set the subject, recipients, cc's, bcc's and the body text. (This, of course, ends up being totally editable by the end user, however). 


// Dismisses the email composition interface when users tap Cancel or Send.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}

This, as the comment says, dismissed the composition view when the user sends or cancels the email. 
And finally we must type the method  to allow the 2.X.X users to call their Mail.app. 


// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = @"bob@john.com";
NSString *body = (@"BODY TEXT HERE");

NSString *email = [NSString stringWithFormat:@"%@%@",recipients,body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
And there you have it. When you click your UIButton, In-app mail should now work :)


~Collateral
Code based on Apple's examples. **


Read More...

Hello World!

in




Hello everybody. This is my new blog. I will type here about.. well pretty much anything. So yea. Welcome.

Ps. Be sure to click on the ads to give me free money :p

Read More...