Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically control iPhone OS versions to enable functions for both OS 3.x and 4 - MFMessageComposeViewController problem

Tags:

iphone

ios4

In order to support iPhone OS 3.x and 4.0 I programmatically control MFMessageComposeViewController functionality like this (use it only if the OS version is 4.0 or above):

// if iPhone OS version >= 4.0
if (os_version_num >= 4) {
   MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
   if([MFMessageComposeViewController canSendText])
   {
      controller.body = text;
      controller.recipients = [NSArray arrayWithObjects: nil];
      controller.messageComposeDelegate = self;
      [self presentModalViewController:controller animated:YES];
      [controller release];
   }
}

But, when I try to run it on iPhone 3.1.3 device, I immediately get the following error (even before application gets loaded):

dyld: Symbol not found: _OBJC_CLASS_$_MFMessageComposeViewController
  Referenced from: /var/mobile/Applications/7232C474-FAD5-4E28-ABC5-8520F62300B0/TextMe.app/TextMe
  Expected in: /System/Library/Frameworks/MessageUI.framework/MessageUI

Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)

What am I doing wrong?

like image 358
Darko Hebrang Avatar asked Jun 30 '10 13:06

Darko Hebrang


3 Answers

You need to make sure you're doing a few things:

In your target's build settings:

  • set Base SDK to iPhone Device 4.0
  • set iPhone OS Deployment Target to iPhone OS 3.1.3 (or lower)

In your target's general settings, under Linked Libraries, change the "Type" next to MessageUI.framework to Weak.

Don't import <MessageUI/MFMessageComposeViewController.h> or it will crash on launch in 3.x. Just import <MessageUI/MessageUI.h>

I don't know what os_version_num is, but here's the code I use to test for the availability of MFMessageComposeViewController:

Class smsClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (smsClass != nil && [MFMessageComposeViewController canSendText]) {
   MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
   controller.body = text;
   controller.recipients = [NSArray arrayWithObjects: nil];
   controller.messageComposeDelegate = self;
   [self presentModalViewController:controller animated:YES];
   [controller release];                
}
like image 54
Christopher Pickslay Avatar answered Dec 04 '22 22:12

Christopher Pickslay


Class theClass = NSClassFromString(@"MFMessageComposeViewController");
MFMessageComposeViewController *controller = theClass ? [[theClass alloc] init] : nil;

You can use the type MFMessageComposeViewController as in:

MFMessageComposeViewController *controller;

But you cannot use the global object MFMessageComposeViewController as in:

[MFMessageComposeViewController alloc];

Instead use the class lookup so you are not dependent on the linker:

[NSClassFromString(@"MFMessageComposeViewController") alloc];
like image 30
drawnonward Avatar answered Dec 04 '22 21:12

drawnonward


In your target parameters, simply set messageUI.framework to type 'weak'

like image 43
Raphaël Pinto Avatar answered Dec 04 '22 21:12

Raphaël Pinto