2014-10-16 53 views
0

我正在製作使用HealthKit的應用程序。該應用程序不能在iPad上工作,因此我的viewDidLoad方法包含一個if/then/else語句以向iPad用戶顯示警報。這是我的代碼:無法將設備檢測爲iPad

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0") && [HKHealthStore isHealthDataAvailable] == 1) { 
    ... 
} 
else { 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Whoops!" message:@"Looks like your device doesn't support HealthKit :(" preferredStyle:UIAlertControllerStyleAlert]; 
    [self presentViewController:alertController animated:1 completion:^(){ 
     NSLog(@"Showed error alert because of unsupported device."); 
    }]; 
} 

SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")位來自this gist

時,該設備是一臺iPad,是不是運行iOS 8.0或更高版本,或者只是(某些其他原因)的UIAlertController應該顯示不能使用HealthKit。這一切都很好,但是當我在運行iOS 8的iPad 2模擬器上運行應用程序時,該應用程序將正常啓動並且不顯示警報。爲了記錄,我知道警報沒有按鈕,但我不希望它消失。它只能顯示在iPad或iOS 8以下的設備上,因此在顯示時不需要消失。

那麼,爲什麼我的應用程序沒有顯示在iPad上的警報視圖?控制檯顯示沒有錯誤。

編輯:無按鈕通知將不會在最終的產品,只是在測試中。然而,這一點仍然存在,因爲警報仍應該顯示出來。

+0

蘋果不會讓你這樣做。僅限iPhone的應用程序必須在iPad上按原樣運行。您當然可以讓應用程序需要某些東西,但總的來說,您嘗試阻止在iPad上運行將不起作用,如果它確實不會被接受。 – rmaddy 2014-10-16 21:42:50

+0

@rmaddy我知道,這只是爲了在開發過程中測試系統。無論蘋果是否喜歡它,這個_應該在模擬器中工作,但它不會。 – 2014-10-16 21:46:49

+0

@rmaddy可能應該在問題中提到這一點。我剛剛編輯了這個問題以說明問題。 – 2014-10-16 21:48:21

回答

1

是否檢查該器件系列被設置爲通用?如果它被設置爲iPhone,則用戶習慣用語永遠不會是iPad。使應用普及似乎已經解決了這個question

PS問過類似的問題,道歉對我的第一個回答不讀書的問題正確。

+0

謝謝,作品像一個魅力。不用擔心,我們都做到了。 – 2014-10-29 16:57:24

0

如果你想只針對iPhone,去了解這個正確的方法是將它在你的部署目標

I've changed this one to match the iPhone only

我已經改變了這一個相匹配的iPhone只

現在,既然如此,您仍然可以通過「擴展」方法(通過縮放iPhone版本以適應iPad)來結束運行iPhone應用程序的iPad。

如果你仍然想在這種情況下該警報,那麼您可以在您的viewDidLoad

if (self.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPad) { 
    NSLog(@"I'm an ipad"); 

    UIAlertController *alert = [ UIAlertController alertControllerWithTitle:@"Sorry dude, no iPads" message:@"go buy an iphone" preferredStyle:UIAlertControllerStyleAlert]; 

    [self presentViewController:alert animated:YES completion:^{}]; 
    } 

在這種情況下刪除此我使用了新的traitCollection屬性來確定接口成語。

如果你只是想避免更大的屏幕,那麼我建議着眼於規模類按iOS 8的這肯定將是最好的途徑。

http://www.learnswift.io/blog/2014/6/12/size-classes-with-xcode-6-and-swift

一個地方得到它的一個開始,

當然蘋果的特徵集合引用和

https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UITraitSet_ClassReference/index.html

0

嘗試是這樣的:

NSString *modelString = (NSString *)[UIDevice currentDevice].model; 
if ([modelString hasPrefix:@"iPad"]) 
{ 
    // iPad 
    return YES; 
} 

我相信這一點即使在iPad上運行僅iPhone應用程序也能工作。