簡體   English   中英

無法將設備檢測為iPad

[英]Cannot detect device as iPad

我正在制作一個使用HealthKit的應用程序。 該應用程序不能在iPad上運行,因此我的viewDidLoad方法包含if / then / else語句,以向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")位來自此要點

UIAlertController應該顯示設備是iPad,未運行iOS 8.0+還是只是(由於其他原因)無法使用HealthKit。 在紙面上這一切都很好,但是當我在運行iOS 8的iPad 2模擬器上運行該應用程序時,該應用程序將正常啟動,並且不顯示警報。 作為記錄,我知道警報沒有按鈕,但我不希望它消失。 它只能在iPad或低於iOS 8的設備上顯示,因此在顯示時不需要消失。

那么,為什么我的應用程序無法在iPad上顯示警報視圖? 控制台未顯示任何錯誤。

編輯:沒有按鈕的通知將不會在最終產品中,僅在測試中。 然而,問題仍然存在,因為警報應該仍然會顯示出來。

您是否檢查過設備系列設置為通用? 如果將其設置為iPhone,則只有用戶慣用語不會是iPad。 使應用普及似乎已經解決了這個問過類似的問題的問題

PS,很抱歉我的第一個答案沒有正確閱讀問題。

如果您只想定位iPhone,執行此操作的正確方法是將其設置在部署目標中

我已更改此款手機,使其僅與iPhone匹配

我已更改此款手機,使其僅與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

嘗試這樣的事情:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM