3

I'm running up against problems trying to incorporate some iOS5-specific libraries into an app targeted at both iOS5 and iOS4.3. I've gone through the following steps:

  • weakly-linked the Twitter framework by setting it as optional in 'Link Binary with Libraries"

  • added it as a framework for the iOS5.0 SDK in Other Linker Flags with `-framework Twitter.framework'

  • conditionally linked the framework in the class header:

    #if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
    #import <Twitter/Twitter.h>
    #import <Accounts/Accounts.h>
    #endif
    
  • then in the method itself, I'm then checking whether the user's set up for Twitter:

    if ([TWTweetComposeViewController class]) {
        self.canTweet = [TWTweetComposeViewController canSendTweet];
    }
    

This works beautifully on both the 5.0 and 4.3 simulators. However, I've got problems getting it to run on, or archive for, actual devices.

When I've got either a 3GS running 5.0, or a 4 running 5.0 attached, both show up twice in the Scheme dropdown. Selecting the top one, and attempting build or run the project fails with an Use of unidentified identifier 'TWTweetComposeViewController' error.

Using the second device entry, the build fails with a ld: framework not found Twitter.framework error.

I'm sure there's something I'm missing here, but I'm stumped. Can anyone advise?

TimD
  • 8,014
  • 2
  • 24
  • 34

4 Answers4

3

If you are using a week linking then you have to check first availability of API using NSClassFromString, respondsToSelector, instancesRespondToSelector etc. So, change your if condition. First try to get your class object using above specified runtime function.
here is a link explaining in detail how to do such. link

Community
  • 1
  • 1
Iducool
  • 3,543
  • 2
  • 24
  • 45
  • Thanks - have amended that in line with the suggestion. It seems like I'm hitting two issues here though, as I've got the the framework not found error as well... – TimD Oct 15 '11 at 14:32
2

The code for presenting twitter controller

Before this you have to add the frameworks as optional and make the import in h file if iOS is min iOS 5

Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");

     if (TWTweetComposeViewControllerClass != nil) {
          if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) {
              UIViewController *twitterViewController = [[TWTweetComposeViewControllerClass alloc] init];

              [twitterViewController performSelector:@selector(setInitialText:) 
                                          withObject:NSLocalizedString(@"TwitterMessage", @"")];
              [twitterViewController performSelector:@selector(addURL:) 
                                          withObject:url];

               [twitterViewController performSelector:@selector(addImage:) 
                                           withObject:[UIImage imageNamed:@"yourImage.png"]];
                [self.navigationController presentModalViewController:twitterViewController animated:YES];
                [twitterViewController release];
                }
            } 
Alex Terente
  • 12,006
  • 5
  • 51
  • 71
0

Further digging into the error thrown back by the compiler suggested that it was ignoring the weak link flag. Although I've no idea how or why, it was fixed by a reinstallation of XCode.

TimD
  • 8,014
  • 2
  • 24
  • 34
-2

if you link to 4.2 or later and deploy to 3.1 or later, you can use the new weak linking features to make this check simple.

you have to add Twitter frameworks as optional and then

Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil) 
{
    if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) 
    {
          UIViewController *twitterViewController = [[TWTweetComposeViewControllerClass alloc] init];
          [twitterViewController performSelector:@selector(setInitialText:) 
                                      withObject:NSLocalizedString(@"TwitterMessage", @"")];
          [twitterViewController performSelector:@selector(addURL:) 
                                      withObject:url];

          [twitterViewController performSelector:@selector(addImage:) 
                                       withObject:[UIImage imageNamed:@"yourImage.png"]];
          [self.navigationController presentModalViewController:twitterViewController animated:YES];
          [twitterViewController release];
     }
} 
Pratyusha Terli
  • 2,343
  • 19
  • 31
Pankaj
  • 61
  • 8