Search code examples
iphoneios4twitterios5sharekit

App crashes on ios 4.X for framework Twitter.framework


I am using twitter using Sharekit and for IOS5, I am using its class TWTweetComposeViewControllerClass as,

Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");

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

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



            [self.navigationController presentModalViewController:twitterViewController animated:YES];
            [twitterViewController release];
        }
    } else {
        [SHK flushOfflineQueue];
        SHKItem *item = [SHKItem text:text];
        //[SHKTwitter shareItem:item];
        SHKActionSheet *actionsheet = [SHKActionSheet actionSheetForItem:item];
        [actionsheet showFromToolbar:self.navigationController.toolbar];
    }

It is working fine with simulator 5.0 but crashes on 4.3 with below error.

dyld: Library not loaded: /System/Library/Frameworks/Twitter.framework/Twitter
      Referenced from: /Users/indianic/Library/Application Support/iPhone Simulator/4.3.2/Applications/241167D0-62E0-4475-85FD-0B8253B4E456/demoFBTW.app/demoFBTW
      Reason: image not found

How do I fix this. I tried to change the dependency for the framework from Required to Optional but didn't find for that


Solution

  • It looks like you found how to weak link the framework. Assuming you are using Xcode 4.2 and LLVM3, you can also simplify this code a bit and fix a bug you have while you're at it:

    #include <Twitter/Twitter.h>
    
    // This line is no longer needed:
    // Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");
    
    
    // this part can now be:
    if ([TWTweetComposeViewController class] != nil) { // no need to look up class by string now
    
        // note previously you were checking if this class responds to 'canSendTweet'
        // but you never called the method to see if you can actually send the tweet
    
        if([TWTweetComposeViewController canSendTweet]) {
    
            // you can type this correctly now...
            TWTweetComposeViewController *twitterViewController = [[TWTweetComposeViewController alloc] init];
    
            // ... and call this method directly
            [twitterViewController setInitialText:NSLocalizedString(@"TwitterMessage", @"")]; 
    
            [self presentModalViewController:twitterViewController animated:YES];
    
            [twitterViewController release];
        }
    }
    ... continue with the shareKit option