2011-12-20 49 views
4

我想爲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 

這裏有什麼問題?

回答

5

經過5個小時,噸愚蠢的文檔閱讀器,改變所有目標&項目設置等我至少來解決。事實證明,這很容易。也許,我的答案會挽救某人的一天。

enter image description here

正如你所看到的,真實設備的目的地( 「mkeskinov的iPod」)增加了一倍。我從不關注這個事實。它看起來只是因爲一些錯誤而倍增,但事實並非如此。如果您選擇「產品\編輯方案」 &開放的目的地列表(在窗口頂部),你可以清楚地看到其中的差別:

enter image description here

我需要做的,成功編譯實際設備應用程序 - 只需選擇第二個選項。它將針對iOS 5進行編譯,然後在具有OS 4的實際設備上運行。第一個選項意味着它將針對iOS 4進行編譯,並且如果您有任何對iOS 4中未提供的Frameworks的引用(不介意,弱引用或強大) - 你會得到編譯時錯誤。

+1

是的,你從我的命運中拯救了我!在我的情況下,方案編輯器中的兩個項目之間沒有區別,但切換確實有效。我可能通過在較新的Xcode中安裝較舊的iOS SDK來觸發此操作。記錄/分享自己的回答總是一件好事。謝謝。 – 2012-08-05 03:03:11

+0

Apple在XCode 4.3中修復了這個問題。以上爲XCODE v。4.2的答案 – 2012-08-06 15:03:04

0

除了使用import之外,還應該使用從NSClassFromString()獲得的Class對象引用TWTweetComposeViewController類,例如, [twClass canSendTweet]而不是[TWTweetComposeViewController canSendTweet]。

+0

如果我這樣做,我會得到一些像「Class method + canSendTweet not found」或「Ins​​tance method -setInitialText:not found」的warrings。不喜歡它。而最大的問題 - 由於鏈接錯誤,我仍然無法在較舊的操作系統下運行我的代碼(請參閱上面編輯的問題)。 – 2011-12-20 20:26:12

1

添加到您的頭文件的.h:

#import <Twitter/TWTweetComposeViewController.h> 

下面是我用我的應用程序:

if ([TWTweetComposeViewController class]) 
{ 
    //can tweet 
} else 
{ 
    //can't tweet 
} 
+0

你在.m文件的頂部使用#import ,對不對?出於某種原因,此#import會導致編譯時錯誤。 – 2011-12-20 20:30:54

+0

我編輯我的答案,包括必要的頭文件。 – user523234 2011-12-20 21:33:25

+0

所以。您可以將設備與舊版操作系統版本連接到您的MAC,並使用Xcode 4在此設備上運行應用程序?如果你的答案是肯定的,請告訴我,你是怎麼做到的。每次嘗試使用較舊的操作系統版本時,都會收到編譯錯誤。 – 2011-12-21 13:31:59

4

您的代碼可能是罰款。

您絕對想要針對iOS5 SDK構建您的應用程序。您生成的二進制文件將在較舊的iOS版本上運行(前提是您的目標SDK是舊版本,如您所示)。

您的代碼正確檢查iOS5功能並做正確的事情,並且您正確地連接到Twitter框架。正是這些技術可以讓您的應用程序(針對最新的SDK構建)在老式iOS版本上運行而不會崩潰。

+0

但**如何**我可以在具有較舊操作系統**的真實設備上運行我的代碼?我預計,設置Base SDK = 5.0應該會導致Xcode始終針對iOS5 SDK進行構建。 – 2011-12-21 13:28:43

+0

你需要有一箇舊版本的設備。 :-)雖然你可以用模擬器來測試。只需使用4.3模擬器,並且您的應用程序應該運行(針對iOS5 SDK構建)就好了。您正在檢查的所有iOS5特定功能在4.3模擬器下都不存在,但您的應用應該運行得很好。 – 2011-12-21 15:16:01

+0

最後,我發現如何在iOS 5上進行編譯。當你知道(我發佈了自己的答案)時,它非常容易。 – 2011-12-21 15:24:01