Monotouch CLLocationManager AuthorizationStatus

The current release of MonoTouch (3.2.4) does not have bindings for CLLocationManager.AuthorizationStatus. Here is what you can use to get that value until they create that binding.

public static CLAuthorizationStatus AuthorizationStatus
{
  get
  {
    var classHandle = (new Class("CLLocationManager")).Handle;
    var selectorHandle = new Selector("authorizationStatus").Handle;
    return (CLAuthorizationStatus)Messaging
      .int_objc_msgSend(classHandle, selectorHandle);
  }
}

Modifying UIScrollView height when keyboard appears

When the keyboard appears over a UIScrollView (that includes the UITableView) the content underneath the keyboard isn’t accessible unless you change the height of the frame of the UIScrollView. Here’s how to do that in MonoTouch.

private NSObject _keyboardShowObserver;
private NSObject _keyboardHideObserver;

// this method assumes the height of a navigation bar on top
// hence the 44 pixels
public override void ViewWillAppear (bool animate)
{
  base.ViewWillAppear (animate);
  
  _keyboardShowObserver = NSNotificationCenter.DefaultCenter
    .AddObserver(UIKeyboard.WillShowNotification, (notification) => {
      var keyboardBounds = (NSValue)notification
        .UserInfo.ObjectForKey(UIKeyboard.BoundsUserInfoKey);
      var keyboardSize = keyboardBounds.RectangleFValue;
    
      UIView.BeginAnimations("k");
      UIView.SetAnimationBeginsFromCurrentState(true);
      _scrollView.Frame = new RectangleF(0, 44, View.Frame.Width, 
        View.Frame.Height - keyboardSize.Height - 44);
      UIView.CommitAnimations();
    });

  _keyboardHideObserver = NSNotificationCenter.DefaultCenter
    .AddObserver(UIKeyboard.WillHideNotification, (notification) => {
      UIView.BeginAnimations("k");
      UIView.SetAnimationBeginsFromCurrentState(true);
      _scrollView.Frame = new RectangleF(0, 44, 
        View.Frame.Width, View.Frame.Height - 44);
      UIView.CommitAnimations();
    });
}

// and don't forget to remove the observers on disappear
public override void ViewWillDisappear(bool animate)
{
  base.ViewWillDisappear(animate);
  NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardHideObserver);
  NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardShowObserver);
}

Generating Address Book Contacts on iPhone Simulator with MonoTouch

One of the things that I needed for testing my app was a list of contacts to play around with in the Contacts app on the iPhone simulator. The only way to do this is to either manually input contacts — which I am not willing to do for 50+ contacts — or programmatically add them in with a bit of random data.

I have done the latter. Check out the monotouch-generate-contacts project on github. This app, once run on the simulator, will add 50 contacts into the address book and generate some random phone numbers for each contact.

For those of you that don’t care about the project on git hub, here’s the main gist of how you add a contact to the address book.

var addressBook = new ABAddressBook();
var record = new ABPerson();
record.FirstName = "Marco";
record.LastName = "Polo";
var phones = new ABMutableStringMultiValue();
phones.Add("4566545555", ABPersonPhoneLabel.Mobile);
record.SetPhones(phones);
addressBook.Add(record);
addressBook.Save();