如何避免編譯iOS 4.0提供的常量,並內置到iOS 3.1的設備中 (How to avoid compiling the constant which is provided in iOS 4.0, and build into the device with iOS 3.1)


問題描述

如何避免編譯iOS 4.0提供的常量,並內置到iOS 3.1的設備中 (How to avoid compiling the constant which is provided in iOS 4.0, and build into the device with iOS 3.1)

I have been tried the following two kinds of codes.

Run time:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
    CFReadStreamSetProperty(theReadStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    CFWriteStreamSetProperty(theWriteStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); 
}

Compile time:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 {
    CFReadStreamSetProperty(theReadStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    CFWriteStreamSetProperty(theWriteStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); 
}

kCFStreamNetworkServiceType and kCFStreamNetworkServiceTypeVoIP are the constants provided in iOS 4.0. However, I need to build the above codes into the device with iOS 3.1 due to some reasons. If I build the above codes into the device with iOS 3.1, it will crash when I open the application. Are there any ways to solve the problem?


參考解法

方法 1:

I agree with Apurv that you should set the Deployment Target in your Xcode build settings, to iOS 3.1, and built with the latest Base SDK.  But, I don't recommend that test for the operating system version.

OS versions are not floating point numbers.  "3.1.3" is a valid OS version, but it is not a floating point number.  Also, comparison of floating point numbers is never really a good idea.

Instead, use the macros from this other Stack Overflow answer

<pre class="lang-objc prettyprint-override">if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"4.0")) {     CFReadStreamSetProperty(theReadStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);     CFWriteStreamSetProperty(theWriteStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);  } </pre>

Edit: As an example of why this is necessary, consider the code:

NSString* osVersion = @"3.1.3";
float x = [osVersion floatValue];
NSLog(@"OS version = %lf", x);
if (x >= 3.1) {
    NSLog(@"OS >= 3.1");
} else if (x < 3.1) {
    NSLog(@"OS < 3.1");
}

Running this on an iPhone 5.0 simulator produces the following output, which is obviously not what you want:

2012-07-31 17:56:38.155 HelloWorld[5856:f803] OS version = 3.100000

2012-07-31 17:56:41.257 HelloWorld[5856:f803] OS < 3.1

If you inspect the value of x in the debugger, I see it as 3.0999999, which triggers the incorrect comparison result.

方法 2:

You should build your application with latest SDK and provide the deployment target to iOS 3.1.

If you are putting the below check, then it should not get crash.

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
    CFReadStreamSetProperty(theReadStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    CFWriteStreamSetProperty(theWriteStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); 
}

(by Chih-Shu ShihNateApurv)

參考文件

  1. How to avoid compiling the constant which is provided in iOS 4.0, and build into the device with iOS 3.1 (CC BY-SA 3.0/4.0)

#runtime #iphone #constants #Version #compilation






相關問題

通過java運行的執行程序似乎超時? (Executed programs run through java seem to time out?)

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)

選擇不包含主要類型 - 錯誤 (Selection does not contain main type - error)

在活動之間使用意圖錯誤 (using intents between activities error)

在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

如何計算此代碼的時間複雜度? (How can I compute the time complexity of this code?)

運行 .exe 文件生成 bt jar2exe 軟件時出現 java runtime environment not found 錯誤 (Getting java runtime enviroment not found error while running .exe file produced bt jar2exe software)

在運行時在 VBA 中顯示所有變量及其值 (Show all variables and their values in VBA during runtime)







留言討論