Mik*_*nov 4 frameworks objective-c weak-linking ios xcode4.2
我想在iOS 5中使用Twitter框架,但能够在较旧的操作系统中运行我的应用程序.
我在Xcode 4.2目标设置中添加了弱引用框架(即设置"可选"标志).Base SDK是iOS 5,iOS部署Target是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
}
Run Code Online (Sandbox Code Playgroud)
它在iOS 5模拟器上运行良好.一旦我尝试使用具有较旧操作系统版本的模拟器或真实设备,我就会收到错误的"Twitter/Twitter.h"文件(在编译时).如果我删除"#import"指令,我会发现两个错误TWTweetComposeViewController类未找到.
如果我评论所有与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
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?
5个小时后,吨傻文档阅读器,更改所有目标和项目设置等.我至少来解决方案.事实证明,当你知道它时,这很容易.也许,我的答案将节省一些人的一天.
如您所见,真实设备目的地("mkeskinov的iPod")增加了一倍.我从不关注这个事实.看起来它只是因为一些错误而翻了一番,但事实并非如此.如果选择"产品\编辑方案"并打开目的地列表(在窗口顶部),您可以清楚地看到差异:
我需要做什么来成功编译真实设备的应用程序 - 只需选择第二个选项.它将针对iOS 5进行编译,然后在具有OS 4的真实设备上运行.第一个选项意味着它将针对iOS 4进行编译,并且如果您有任何对Frameworks的引用,这在iOS 4中没有呈现(没关系,弱引用或强) - 你得到编译时错误.