4

我想使用适用于 iOS 5 的 Twitter 框架,但能够在旧操作系统中运行我的应用程序。

我在 Xcode 4.2 目标设置中添加了弱引用框架(即设置“可选”标志)。基础 SDK 是 iOS 5,iOS 部署目标是 iOS 3.2。

接下来,我尝试使用 Twitter 框架:

#import <Twitter/Twitter.h>
...
    Class twClass = NSClassFromString(@"TWTweetComposeViewController");
    if (!twClass) // Framework not available, older iOS
    {
        [self shareWithTwitterPriorIOS5];
        return;
    }

    if ([TWTweetComposeViewController canSendTweet]) // Check if twitter is setup and reachable
    {
        TWTweetComposeViewController* twc = [[TWTweetComposeViewController alloc] init];
//        [twc addURL:[NSURL URLWithString:@"http://mail.ru"]];
//        [twc addImage:[UIImage imageNamed:@"Some image.png"]]
        [twc setInitialText:textToShare];
        [viewController presentViewController:twc animated:YES completion:^{
            // Optional
        }];
        [twc release];
        // Assume twc is ARC released or call [twc release];
    }
    else
    {

    // Twitter account not configured, inform the user
}

它在 iOS 5 模拟器上运行良好。一旦我尝试使用具有旧操作系统版本的模拟器或真实设备,我就会收到错误“Twitter/Twitter.h”文件未找到(在编译时)。如果我删除“#import”指令,我会收到一些错误 TWTweetComposeViewController class not found。

如果我评论所有与 Twitter 相关的代码,我会收到链接器错误:“ld: framework not found Twitter”。ld 命令导致错误:

Ld /Users/mikhailkeskinov/Library/Developer/Xcode/DerivedData/Dictionary-eiyrziajmltuglfzgtnjxffkojwi/Build/Products/Debug-iphoneos/Dictionary.app/Dictionary normal armv6
    cd /Developer/WorkShop/XDictionary/trunk
    setenv IPHONEOS_DEPLOYMENT_TARGET 3.2
    setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk -L/Users/mikhailkeskinov/Library/Developer/Xcode/DerivedData/Dictionary-eiyrziajmltuglfzgtnjxffkojwi/Build/Products/Debug-iphoneos "-L/Developer/WorkShop/XDictionary/trunk/Dictionary/Twitter+OAuth/Libraries & Headers" -F/Users/mikhailkeskinov/Library/Developer/Xcode/DerivedData/Dictionary-eiyrziajmltuglfzgtnjxffkojwi/Build/Products/Debug-iphoneos -filelist /Users/mikhailkeskinov/Library/Developer/Xcode/DerivedData/Dictionary-eiyrziajmltuglfzgtnjxffkojwi/Build/Intermediates/Dictionary.build/Debug-iphoneos/Dictionary.build/Objects-normal/armv6/Dictionary.LinkFileList -dead_strip -miphoneos-version-min=3.2 -lxml2 -framework AVFoundation -framework UIKit -framework Foundation -framework CoreGraphics -lOAuth -weak_framework Twitter -o /Users/mikhailkeskinov/Library/Developer/Xcode/DerivedData/Dictionary-eiyrziajmltuglfzgtnjxffkojwi/Build/Products/Debug-iphoneos/Dictionary.app/Dictionary

这里有什么问题?

4

4 回答 4

5

After 5 hours, tons silly documentation reader, changing all target & project settings etc. I at least come to solution. Turns out it is pretty easy, when you know it. Probably, my answer will save somebody's day.

enter image description here

As you can see, real device destination ("mkeskinov's iPod") doubled. I never pay attention at this fact. It looks like it just doubled by some mistake, but it is not. If you select "Product\Edit Schemes" & open Destination list (on the top of window), you can clearly see the difference:

enter image description here

What I need to do to successfully compile app for real device - just select second option. It will compile for iOS 5 and then run on real device with OS 4. First option means that it will be compile for iOS 4, and if you have any references to Frameworks, which is not presented in iOS 4 (never mind, weak references or strong) - you get compile time error.

于 2011-12-21T15:20:29.510 回答
4

Your code is probably fine.

You absolutely want to build your app against the iOS5 SDK. The binary you produce will run on older iOS versions (provided your target SDK is an older version, as you have indicated).

Your code is correctly checking for iOS5 capabilities and doing the right thing, and you are correctly weak linking to the Twitter framework. It is these techniques that allows your app (built against the latest SDK) to run without crashing on older iOS versions.

于 2011-12-20T21:36:49.913 回答
1

Add this to your header file .h:

#import <Twitter/TWTweetComposeViewController.h>

Here is what I have used for my app:

if ([TWTweetComposeViewController class])
{
   //can tweet
} else
{
   //can't tweet
} 
于 2011-12-20T20:23:13.417 回答
0

Instead of using import, you should reference the TWTweetComposeViewController class using the Class object you got from NSClassFromString(), e.g. [twClass canSendTweet] instead of [TWTweetComposeViewController canSendTweet].

于 2011-12-20T20:12:47.880 回答