This is a fantastic challenge for a jailbreak tweak! It involves hooking into UI components, fetching data from a framework, performing calculations, and injecting new UI elements.
We'll focus on the modern Contacts framework (Contacts.framework and ContactsUI.framework). The key is to identify the UIViewController or UITableViewCell that displays the contact's details, specifically the birthdate field.
Based on common iOS UI patterns and some reverse engineering knowledge:
CNContactViewController is the primary view controller for displaying a single contact.
- The actual content (fields, labels) is often managed by an internal view controller, such as
CNContactContentViewController.
- The fields are usually displayed in a
UITableView, and each field is a UITableViewCell.
We'll hook -[CNContactContentViewController tableView:cellForRowAtIndexPath:] to intercept cell creation/configuration and then modify the birthdate cell.
Theos Project Setup
First, create a new Theos project:
nic.pl
# Choose option 4: iphone/tweak
# Project Name: ContactsAgeTweak
# Package Name: com.yourname.contactsagetweak
# Author/Maintainer: Your Name
# MobileSubstrate Bundle Filter: com.apple.MobileAddressBook (This is the bundle ID for the Contacts app)
# List of applications to terminate: MobileAddressBook
control File
Edit control to describe your package.
Package: com.yourname.contactsagetweak
Name: Contacts Age Tweak
Version: 1.0.0
Architecture: iphoneos-arm
Description: Shows person's age next to birthdate in iOS Contacts.
Maintainer: Your Name <your.email@example.com>
Author: Your Name <your.email@example.com>
Depends: firmware (>= 11.0), mobilesubstrate, com.apple.MobileAddressBook
Section: Tweaks
Makefile
Edit Makefile to include necessary frameworks.
ARCHS = arm64 arm64e
TARGET = iphone:latest:14.0 # Adjust based on your target iOS version, 14.0+ is good for modern Contacts.framework
# For earlier iOS versions, you might need to target an older SDK (e.g., 10.0)
# and potentially include AddressBook.framework.
include $(THEOS)/makefiles/common.mk
TWEAK_NAME = ContactsAgeTweak
ContactsAgeTweak_FILES = Tweak.xm
ContactsAgeTweak_FRAMEWORKS = UIKit Foundation Contacts ContactsUI # Key frameworks!
include $(THEOS_MAKE_FILES)/tweak.mk
Tweak.xm
This is where the magic happens.
#import <UIKit/UIKit.h>
#import <Contacts/Contacts.h> // For CNContact, CNContactStore, CNContactFormatter
#import <ContactsUI/ContactsUI.h> // For CNContactViewController, CNContactContentViewController
// Forward declaration of CNContactContentViewController
// This is good practice as CNContactContentViewController might be a private class.
@interface CNContactContentViewController : UIViewController
@property (nonatomic, retain, readonly) CNContact *contact;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@end
// Helper function to calculate age from NSDateComponents
static NSInteger calculateAgeFromBirthdayComponents(NSDateComponents *birthdayComponents) {
if (!birthdayComponents) {
return -1; // Indicate no valid birthday components
}
NSCalendar *calendar = [NSCalendar currentCalendar];
// Create NSDate from NSDateComponents
NSDate *birthDate = [calendar dateFromComponents:birthdayComponents];
if (!birthDate) {
return -1; // Could not create date from components
}
NSDate *today = [NSDate date];
// Calculate age components
NSDateComponents *ageComponents = [calendar components:NSCalendarUnitYear fromDate:birthDate toDate:today options:0];
return [ageComponents year];
}
// A unique tag for our added UILabel to prevent duplicates on cell reuse.
static const NSInteger kAgeLabelTag = 987654321;
%hook CNContactContentViewController
// Hook the method that provides table view cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Call the original implementation to get the base cell
UITableViewCell *cell = %orig;
// Remove any previously added age label to prevent duplicates on cell reuse
// This is crucial because UITableViewCells are reused.
UIView *existingAgeLabel = [cell.contentView viewWithTag:kAgeLabelTag];
if (existingAgeLabel) {
[existingAgeLabel removeFromSuperview];
}
// Get the current contact from the view controller.
// This assumes CNContactContentViewController has a `contact` property, which it does.
CNContact *currentContact = self.contact;
// Check if we have a contact and it has a birthday
if (currentContact && currentContact.birthday) {
// This is the heuristic part: trying to identify the birthdate cell.
// It's brittle to localization, but generally reliable enough for tweaks.
// We're looking for cells whose `textLabel.text` might indicate "Birthday"
// and whose `detailTextLabel.text` is the formatted date.
// Get the birthdate components
NSDateComponents *birthdayComponents = currentContact.birthday;
// Common patterns for birthdate labels: "Birthday", "Geburtstag", "Fecha de nacimiento"
// Let's check if the primary text label contains "birth" (case-insensitive)
// or if it explicitly matches "Birthday" (for English).
NSString *textLabelText = cell.textLabel.text;
// Attempt to parse the detailTextLabel's content to confirm it's a date.
// This is more robust than just checking the textLabel.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle = NSDateFormatterLongStyle; // Match typical Contacts app date format
NSDate *detailDate = [formatter dateFromString:cell.detailTextLabel.text];
// Compare the parsed date to the actual birthday components to be sure.
// This is the most accurate way to confirm it's *the* birthdate cell.
// Convert birthdateComponents to an NSDate for comparison.
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *actualBirthDate = [calendar dateFromComponents:birthdayComponents];
// Check for nil dates before comparison
if (detailDate && actualBirthDate) {
// Compare only year, month, day to ignore time components.
NSDateComponents *detailDateComponents = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:detailDate];
NSDateComponents *actualBirthDateComponents = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:actualBirthDate];
if ([detailDateComponents isEqual:actualBirthDateComponents]) {
// This is very likely the correct birthdate cell!
NSInteger age = calculateAgeFromBirthdayComponents(birthdayComponents);
if (age != -1) {
// Create and configure the age label
UILabel *ageLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // Frame set below
ageLabel.text = [NSString stringWithFormat:@" (%ld years)", (long)age];
ageLabel.font = cell.detailTextLabel.font; // Match the original font
ageLabel.textColor = cell.detailTextLabel.textColor; // Match the original color
ageLabel.tag = kAgeLabelTag; // Assign unique tag
[cell.contentView addSubview:ageLabel];
[ageLabel sizeToFit]; // Adjust label size to fit its text
// Position the new label next to the detailTextLabel.
// This uses the detailTextLabel's frame, which can be tricky with Auto Layout,
// but often works for simple tweaks.
CGRect detailTextFrame = cell.detailTextLabel.frame;
// Adjust the detailTextLabel's width to make space for the new label
// This prevents overlap and makes the layout cleaner.
CGFloat availableWidth = cell.contentView.bounds.size.width - detailTextFrame.origin.x - ageLabel.bounds.size.width - 15.0; // Some margin
detailTextFrame.size.width = MIN(detailTextFrame.size.width, availableWidth);
cell.detailTextLabel.frame = detailTextFrame;
// Place the age label immediately to the right of the detailTextLabel
// with a small padding.
CGFloat padding = 5.0; // Small padding between date and age
ageLabel.frame = CGRectMake(CGRectGetMaxX(detailTextFrame) + padding,
detailTextFrame.origin.y,
ageLabel.bounds.size.width,
ageLabel.bounds.size.height);
[ageLabel release]; // Release since it's added as subview
}
}
}
[formatter release]; // Release the formatter
}
return cell;
}
%end
How it Works:
calculateAgeFromBirthdayComponents: A helper function that takes NSDateComponents (from CNContact.birthday) and calculates the person's age in years using NSCalendar.
%hook CNContactContentViewController: This targets the class responsible for displaying the contact's details in a table view.
tableView:cellForRowAtIndexPath:: This method is called by the UITableView whenever a cell needs to be displayed or reused. We hook it to get control over each cell.
%orig: Calls the original implementation of tableView:cellForRowAtIndexPath: to get the default UITableViewCell that the Contacts app would normally provide.
- Cell Reuse Handling:
[cell.contentView viewWithTag:kAgeLabelTag]; and [existingAgeLabel removeFromSuperview]; are crucial. UITableView reuses cells to save memory and improve performance. Without removing the old label, you'd end up with multiple " (X years)" labels stacked on top of each other every time you scroll.
CNContact *currentContact = self.contact;: We access the contact property of CNContactContentViewController to get the CNContact object, which holds the birthdate.
- Identifying the Birthday Cell:
- We retrieve the
birthday components from currentContact.
- We use
NSDateFormatter to try and parse the cell.detailTextLabel.text into an NSDate.
- Then, we compare the parsed date with the actual birthdate from
currentContact.birthday (ignoring time components for robustness). This is the most reliable way to confirm we have the correct cell, even with localization.
- Age Calculation and UI Injection:
- If it's the birthdate cell,
calculateAgeFromBirthdayComponents is called.
- A new
UILabel (ageLabel) is created, configured with the calculated age, and its font/color are matched to the existing detailTextLabel for visual consistency.
[ageLabel sizeToFit]; adjusts the label's frame to fit its content.
- Positioning:
CGRectGetMaxX(detailTextFrame) + padding positions the directly to the right of the .
To Build and Install:
- Navigate to your
ContactsAgeTweak project directory in the terminal.
- Run
make package install.
- Your device must be connected via SSH, and Theos must be correctly set up.
- After installation, SpringBoard (or the Contacts app) will be restarted as specified in the
Makefile.
- Open the Contacts app, view a contact with a birthdate, and you should see the age next to it!
Important Considerations and Potential Improvements:
- Localization Robustness: The string check
[textLabelText containsString:@"birth"] is a heuristic. The date parsing and comparison of NSDateComponents is much more robust against different language settings.
- Auto Layout: For more complex or future-proof tweaks, using
NSLayoutConstraint directly within the tweak would be more robust than frame manipulation, especially if Apple changes UI layout extensively. However, for simple appended labels, frame manipulation is often sufficient.
- Performance: Hooking
tableView:cellForRowAtIndexPath: can be called many times during scrolling. Keep the logic inside the hook as lightweight as possible. Our age calculation is fast, so it shouldn't be an issue.
- Error Handling: Basic checks for
nil values are included.
- iOS Version Compatibility: This code is written for modern iOS versions (iOS 10+ where
Contacts.framework is prevalent). For very old iOS versions, you'd need to target AddressBook.framework and AddressBookUI.framework and adjust the class names (e.g., ABPersonViewController).
This implementation provides a solid foundation for achieving your goal accurately using Theos and Objective-C.