Wednesday, August 8, 2012

How to Find MFMailComposeViewController Email Addresses


One of the missing iOS APIs is a way to find out whom did user send email to. Otherwise MFMailComposeViewController is excellent: easy to setup, easy to launch, easy to use and easy to close. You just have no idea what user was doing with it!

I really needed to know who were email recipients. There are several good reasons for this and I just had one of those. So it was time to do some experimenting!

Write this (thrice now), which is close, but just not good enough. Maybe someone can continue from here? First standard didFinishWithResult callback, nothing special there. Bit simplified for this case:
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{   
    if (result == MFMailComposeResultSent)
    {
        NSString *email = [self findEmailAddresses:controller.view depth:0];
        NSLog(@"%@", email);
        [self.viewController dismissModalViewControllerAnimated:YES];
    }
}
Here's the interesting code, which can be used to peek what's inside the MFMailComposeViewController blackbox (UPDATE: easier way to check view hierarchy). Pretty common view hierarchy walking code, now especially looking for text strings:
- (NSString *)findEmailAddresses:(UIView *)view depth:(NSInteger)count
{
    NSString *eAddress = nil;
    if (!view)
        return eAddress;
    
    NSMutableString *tabString = [NSMutableString stringWithCapacity:depth];
    for (int i = 0; i < depth; i++)
        [tabString appendString:@"-- "];
    NSLog(@"%@%@", tabString, view);
    
    if ([view isKindOfClass:[UITextField class]])
    {
        // MAGIC: debugger shows email address(es) in first textField
        // but only if it's about max 35 characters
        if (((UITextField *)view).text)
        {
            eAddress = [NSString stringWithString:((UITextField *)view).text];
            NSLog(@"FOUND UITextField: %@", eAddress ? eAddress : @"");
        }
    }
    
    NSArray *subviews = [view subviews];
    if (subviews) {
        for (UIView *view in subviews)
        {
            NSString *s = [self findEmailAddresses:view depth:depth+1];
            if (s) eAddress = s;
                }
    }
    
    return eAddress;
}
Problems: it finds the recipient email address(es), but only if that fits in about 35 character text string. Otherwise you find only a summary string like "aaa@aaa.aa & 2 more...". So where are the actual email addresses in this case?

Hide UITableView Empty Cell Separator Lines


Hide UITableView Empty Cell Separator Lines

Plain UITableView displays cell separator lines, when table is empty or has only a few real cells. This is the default behaviour - and it is good. You should not change it.

But sometimes it looks a bit too weird. When your cells have different heights, all empty cells following the last real one will use last cell's height. If this changes at every table refresh, the "jumping" empty cells start annoying users. Or at least developers.

Easy way is to define that table has no cell separator lines:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Hard way, if you need separator lines, is to define that table has no separator lines and create cell separator lines as part of custom UITableViewCell. Bottom part of cell, obviously, and most likely using really thin graphics image. Remember to define image autosizing and mode properly.

Another way is to define an empty UITableView's tableFooterView:
UIView *footer =
    [[UIView alloc] initWithFrame:CGRectZero];
self.myTable.tableFooterView = footer;
[footer release];
You really should not mess with default settings. Users have seen it before, know what it means and are even expecting it. Beware, do not confuse your users!

Note about MKAnnotation Protocol implementation



Sometimes you just have to take things as they are given, just like that. For example adding MKAnnotation protocol support all you have to do is create an instance variable called "coordinate":
@interface MyPointItem : NSObject {
    NSString *title;
    NSInteger distance;
    CLLocationCoordinate2D coordinate;
}
As Apple documentation says:
"An object that adopts this protocol must implement the coordinateproperty."
There you are. As long as you remember, WHY you have a variable called "coordinate" INSTEAD OF "location". If you don't remember and apply Natural Naming to your code, your application will start crashing horribly for mysterious reasons...

How to Create Custom Map Annotation Button



Custom accessory button for MKPinAnnotationView right side, create inside MKMapViewDelegate protocol mapView:viewForAnnotation method:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[button setImage:[UIImage imageNamed:@"myButton.png"] forState:UIControlStateNormal]; 
view.rightCalloutAccessoryView = button;
Usually sample codes use UIButtonTypeDetailDisclosure as right button, but you can't use that unless you really show additional details. For anything else, you must use a custom button.

How to Control UISearchBar Background Color



When you have a list, you want to search it. How would you do that? No, don't code it by yourself. Done it, removed it.

By default each UITableView comes with search support via a built-in UISearchDisplayController. You just add and connect UISearchBar to your table in Interface Builder and provide some delegate methods to make it work. Pretty easy:
self.searchDisplayController.delegate =
self.searchDisplayController.searchBar.delegate =
self.searchDisplayController.searchResultsDataSource =
self.searchDisplayController.searchResultsDelegate = self.mySearchObject;
Controlling how your UISearchBar looks like is a bit more difficult. If you want to show search box and some other component(s) side by side, you can do it by putting UIToolbar at back and a short UISearchBar on top of it. By default their backgrounds are different, looking pretty ugly together. Like a hack, what it really is.

You can fix that by doing two things. First get rid of default background:
for (UIView *view in self.searchBar.subviews)
{
   if ([view isKindOfClass:NSClassFromString
     (@"UISearchBarBackground")])
   {
     [view removeFromSuperview];
     break;
   }
}
...and the second thing? Something that will be extremely hard to find out, unless you get it right by accident? You have to set UISearchBar style Black Translucent!

Sad to think how many hours I wasted experimenting Things That Will Not Work, just to find out that kind of miniscule solution. Also bet that this will not work one year from now...

New Fonts in iOS 5.0 - and Some Missing


New Fonts in iOS 5.0 - and Some Missing

Apple iOS 5.0 was released few days ago andinstallation was rather hard. It's still not complete, since I had 500+ apps and restore from iOS 4.3.5 backup was about a total failure. Enough of that, waiting for iOS 5.1 release rather soon.

Let's look at fonts. Yes, with pictures!

iOS 5.0 was a huge update in many ways and one of them is built-in font selection. For the first time ever, iPhone and iPad have exactly the same fonts!

iPad lost two font families (Heiti J, Heiti K), got two new families (Euphemia UCAS, Marion) and 13 additions to already existing font families.

The following list has been created with the help of FontType application, created by yours truly! (available on App Store)

ModeliPhoneiPhoneiPadiPad
Device4.3.35.04.3.25.0

Academy Engraved LET

xxx

American Typewriter
xxxx
xxxx

x
x

x
x

x
x

x
x

Apple Color Emoji
xxxx

AppleGothic
xxxx

Arial
xxxx
xxxx
xxxx
xxxx

Arial Hebrew
xxxx
xxxx

Arial Rounded MT Bold
xxxx

Bangla Sangam MN
xxxx
xxxx

Baskerville
xxxx
xxxx
xxxx
xxxx

x
x

x
x

Bodoni 72

xxx

xxx

xxx

Bodoni 72 Oldstyle

xxx

xxx

xxx

Bodoni 72 Smallcaps

xxx

Bodoni Ornaments

xxx

Bradley Hand

xxx

Chalkboard SE
xxxx

x
x
xxxx

Chalkduster

xxx

Cochin
xxxx
xxxx
xxxx
xxxx

Copperplate

xxx

xxx

x
x

Courier
xxxx
xxxx
xxxx
xxxx

Courier New
xxxx
xxxx
xxxx
xxxx

DB LCD Temp
xxxx

Devanagari Sangam MN
xxxx
xxxx

Didot

xxx

xxx

xxx

Euphemia UCAS

x
x

x
x

x
x

Futura
xxxx

x
x
xxxx
xxxx

Geeza Pro
xxxx
xxxx

Georgia
xxxx
xxxx
xxxx
xxxx

Gill Sans

xxx

xxx

xxx

xxx

x
x

x
x

Gujarati Sangam MN
xxxx
xxxx

Gurmukhi MN
xxxx
xxxx

Heiti J
STHeitiJ-Lightx
x
STHeitiJ-Mediumx
x

Heiti K
STHeitiK-Lightx
x
STHeitiK-Mediumx
x

Heiti SC
xxxx
xxxx

Heiti TC
xxxx
xxxx

Helvetica
xxxx
xxxx
xxxx

x
x

x
x
xxxx

Helvetica Neue
xxxx
xxxx
xxxx

x
x

x
x
xxxx

x
x

x
x

x
x

x
x

x
x

Hiragino Kaku Gothic ProN
xxxx
xxxx

Hiragino Mincho ProN

xxx

xxx

Hoefler Text

xxx

xxx

xxx

xxx

Kailasa
xxxx
xxxx

Kannada Sangam MN
xxxx
xxxx

Malayalam Sangam MN
xxxx
xxxx

Marion

x
x

x
x

x
x

Marker Felt
xxxx
xxxx

Noteworthy
xxxx
xxxx

Optima

xxx

xxx

x
x

xxx

xxx

Oriya Sangam MN
xxxx
xxxx

Palatino
xxxx
xxxx
xxxx
xxxx

Papyrus

xxx

x
x

Party LET

xxx

Sinhala Sangam MN
xxxx
xxxx

Snell Roundhand
xxxx

x
x
xxxx

Tamil Sangam MN
xxxx
xxxx

Telugu Sangam MN
xxxx
xxxx

Thonburi
xxxx
xxxx

Times New Roman
xxxx
xxxx
xxxx
xxxx

Trebuchet MS
xxxx
xxxx
xxxx
xxxx

Verdana
xxxx
xxxx
xxxx
xxxx

Zapf Dingbats

xxx

Zapfino
xxxx