我想爲iOS 5使用Twitter框架,但能夠在較舊的操作系統中運行我的應用程序。如何訪問iOS中的弱鏈接框架?
我在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類未找到。
如果我評論所有與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
這裏有什麼問題?
是的,你從我的命運中拯救了我!在我的情況下,方案編輯器中的兩個項目之間沒有區別,但切換確實有效。我可能通過在較新的Xcode中安裝較舊的iOS SDK來觸發此操作。記錄/分享自己的回答總是一件好事。謝謝。 – 2012-08-05 03:03:11
Apple在XCode 4.3中修復了這個問題。以上爲XCODE v。4.2的答案 – 2012-08-06 15:03:04