2012-10-22 44 views
4

我想要檢測用戶有什麼iDevice,然後將device name置於UILabel。使用以下代碼,應用程序只能檢測到我喜歡擁有iPhone 4/iPod 3G/iPad 1G的iPhone/iPad/iPod ...或確切名稱(iPhone 3.1/iPod 2.0/iPad 2.4)...如何準確檢測iDevice模型?

這裏是我的代碼:

iDevice.text = [UIDevice currentDevice]. localizedModel; 

我想這

iDevice.text = [UIDevice currentDevice]. model; 

但小巷它sayes iPhone,我喜歡iPhone 3.1

+0

的http://stackoverflow.com/questions/1108859/detect-the-specific-iphone-ipod-touch-model – propstm

+0

我嘗試了所有的這些,但他們的非可能的複製爲我工作:( –

+0

你檢查對我的鏈接接受的答案是什麼?我已經使用過,取得了一些成功。「最完整的UIDevice(硬件)類別可能是http://github.com/erica/uidevice-extension/」 – propstm

回答

2

好了,所以這聽起來像你將要使用的方法使用類別c由Erica Sadun位於https://github.com/erica/uidevice-extension/

在我開始使用它之前,我會傳遞一些有關類別的信息。蘋果類提供文檔這裏http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

您可以通過下方的類別名稱的接口文件 宣佈他們以相同的名稱定義它們在實現文件 方法添加到一個類。類別名稱表示這些方法是添加到其他地方聲明的類中的,而不是新的類。但是,您不能使用一個類別將其他實例變量添加到 類中。

下載從GitHub的項目,而這兩個文件添加到您的項目:

- (NSString *) platform; 
- (NSString *) hwmodel; 
- (NSUInteger) platformType; 
- (NSString *) platformString; 

所以你:

UIDevice-Hardware.h 
UIDevice-Hardware.m 

您將使用將是其中的一個方法將要將UIDevice-Hardware.h導入到您要使用該方法的文件中。你可以使用該方法返回一個NSString值和值賦給一個標籤,所以你會做同樣的事情到

mylabel.text = [[UIDevice currentDevice] platformString] 

這裏有一個很好的介紹類另一個鏈接:http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

編輯:使用設備仿真器的示例屏幕截圖: enter image description here 注意:我的@interface行上面也有#import "UIDevice-Hardware.h"

+0

現在我添加了UIDevice-Hardware.h和UIDevice-Hardware.m到我的項目,我#import「UIDevice-Hardware.h」在我的.m文件中的文件,然後我添加mylabel.text = [[UIDevice currentDevice] platformString] - (void)viewDidLoad和我的應用程序崩潰:(我的錯誤是什麼?我必須對 - (NSString *)平臺進行任何操作; - (NSString *)hwmodel; - (NSUInteger)platformType; - (NSString *)platformString;? –

+0

爲你的實例,你會想'iDevice.text = [[UIDevice currentDevice] platformString];'這只是一個通用標籤名稱寫道。它可能會崩潰,因爲我的變量名稱尚未定義。 – propstm

+0

我有iDevice.text = [[UIDevice currentDevice] platformString]; –