1

I'm 14 and developing my second iOS app: an iPhone version of the web-based chatroom I created for my school. I have very little knowledge of Objective-C and I need some help. This is a very basic app, where I have a UIWebView and a tool bar at the bottom of the view. The toolbar contains a text field and a "Send" button. I have the UIWebView working and pointing to the correct site, but I need two basic things:

1. I need the toolbar to reposition itself to the top of the keyboard when the text box is tapped, also resizing the UIWebView for the correct space.

and

2. I need to find a way to post the contents of my text field to my PHP script online when the "Send" button is tapped or the "Send" key on the keyboard is pressed.

Any help would be much appreciated!

Edit: Here are some screenshots of the app.

App screenshots.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Grant
  • 13
  • 4

3 Answers3

1

You'll want to take a look at "Managing The Keyboard".

In essense what you want to do is have the delegate of the textfield deal with the keyboard notification "UIKeyboardWillShowNotification", and use the info which it will supply to move the toolbar into place, possibly with an animation.

---UPDATE---

Looking around a little, I found this:

iPhone Keyboard Covers UITextField

Community
  • 1
  • 1
Hack Saw
  • 2,741
  • 1
  • 18
  • 33
1

Lots of stuff to answer here!

Repositioning the toolbar

To reposition the toolbar you'll need to do a few things:

  • Get notified when the user starts using it
  • Resize your webview and move your toolbar

In order to get notified when the user taps on your text field you will need to:

  • Implement the UITextFieldDelegate protocol in your controller (most likely the main view controller).
  • Implement the delegate method -textFieldDidBeginEditing:.
  • Set your controller object as the text field's delegate.
  • UITextFieldDelegateProtocol Reference should explain this.

To resize your controls you will need to have them declared as IBOutlets and hook them up in interface builder. Your best bet for this is to read the Interface Builder Quick Start Guide.

Sending the data

Sending the data is going to be the trickier bit. A couple of things you could do:

  • Cheat and send the data as "GET" data by using a method like NSString's -initWithContentsOfURL: (something like this:)

    NSString *chatString = [textField stringValue]; //get the chat string
    NSString *encodedString = [chatString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; //URL-encode any special characters
    NSString *urlToRequest = [NSString stringWithFormat:@"http://mysite.com/chat.php?message=%@", encodedString];
    NSURL *requestURL = [NSURL URLWithString:urlToRequest];
    NSString *urlResult = [NSString stringWithContentsOfURL:requestURL];
    

By doing it this way you will be able to get the message in your php script like so:

    $encoded_message = $_GET['message'];
    $message = urldecode($encoded_message);

This is not the most robust way but for a high school project it should probably be fine.

The more robust way you could do it is by using the cocoa libraries intended for this. I won't delve into these details as it is quite complex and Apple's own developer documentation does a much better job of explaining it: URL Loading System Programming Guide

Edit: As per Hack Saw's post, using NSNotification to determine when the keyboard will show is better. By doing things the delegate way as I've described, you will only be notified when that particular text field is selected. Documentation on managing the keyboard can be found here: Text, Web, and Editing Programming Guide for iOS

arrtchiu
  • 1,076
  • 1
  • 10
  • 23
  • Good Answer. But you can also use tag values to find out which field is tagged - hence you can use the `UITextViewDelegate` protocol with no troubles. – Aurum Aquila Feb 23 '11 at 08:05
  • Actually, the textfield sending the textFieldDidBeginEditing method will be supplying the textfield in question's pointer as the parameter. You can use that to get all the info you need. – Hack Saw Feb 23 '11 at 08:06
  • Yes, if you have multiple text fields then you can set your controller to be the delegate of all of them and use the parameter. Still, catching the NSNotification is better, because it will notify you *any* time the keyboard is shown rather than just for events we expect it to show the keyboard for (like when one becomes the first responder). Also take into account @fluchtpunkt's point about the iPad not necessarily having to show a keyboard to accept user input. – arrtchiu Feb 23 '11 at 08:31
1

This is a quick unfancy way to do move the toolbar up. I'm sure you'll be able to adapt it to your needs.

First you have to register for the keyboard notifications

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

and then you move the toolbar up. Like this.

- (void)keyboardWillShow:(NSNotification *)notification {
    if (keyboardShown) {
        return;
    }
    NSDictionary* info = [notification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    CGRect currentFrame = self.toolbar.frame;
    currentFrame.origin.y = currentFrame.origin.y - keyboardSize.height;

    [UIView beginAnimations:@"ShowKeyboard" context:NULL];
    [UIView setAnimationCurve:[[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    self.toolbar.frame = currentFrame;
    [UIView commitAnimations];

    keyboardShown = YES;
}

of course you have to create another method that will hide the keyboard, but this is basically the same as show keyboard, so I'll omit it.

To make things a little bit more easy you could make the toolbar and the webview subviews of a "content" view. You would then resize the height of the contentview and autoresizing will take care of the rest.

Oh and you should not resize the view just because of textFieldDidBeginEditing:. I don't know for the iphone, but it's possible to connect an external keyboard to the ipad. And you would resize the view without the keyboard showing up, leaving a big blank frame at the bottom.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247