[英]How to determine if device location permission is denied twice in iOS swift?
[英]How to determine device type from Swift? (OS X or iOS)
我知道 Swift 相對較新,但我想知道是否有辦法確定設備類型?
(就像你以前可以用#define
做的那樣)?
我主要想知道如何區分 OS X 或 iOS。 我在這個主題上什么也沒找到。
如果您正在為 iOS 和 macOS(也可能為 watchOS 和 tvOS)進行構建,那么您至少要編譯兩次:每個平台一次。 如果希望在每個平台上執行不同的代碼,則需要構建時條件檢查,而不是運行時檢查。
Swift 沒有預處理器,但它有條件構建指令——而且在大多數情況下,它們看起來像 C 等價物。
#if os(iOS) || os(watchOS) || os(tvOS)
let color = UIColor.red
#elseif os(macOS)
let color = NSColor.red
#else
println("OMG, it's that mythical new Apple product!!!")
#endif
您還可以使用構建配置來測試架構( x86_64
、 arm
、 arm64
、 i386
)、目標環境(iOS 模擬器或 Mac Catalyst)或-D
編譯器標志(包括由標准 Xcode 模板定義的DEBUG
標志)。 不要假設這些東西會一起出現——Apple 已經宣布在 arm64 上的 macOS 將於 2020 年推出,所以 arm64 並不意味着 iOS,而 iOS Simulator 並不意味着 x86,等等。
請參閱The Swift Programming Language中的編譯器控制語句。
(如果你想區分你在運行時使用的是哪種 iOS 設備,就像你在 ObjC 中一樣使用UIDevice
類。查看對你來說重要的設備屬性而不是設備通常更有用和安全名稱或習慣用法——例如,使用特征和大小類來布置您的 UI,檢查 Metal 以獲得您需要的 GPU 功能等)
這應該為您提供每個用例:
#if os(OSX)
print("macOS")
#elseif os(watchOS)
print("watchOS")
#elseif os(tvOS)
print("tvOS")
#elseif os(iOS)
#if targetEnvironment(macCatalyst)
print("macOS - Catalyst")
#else
print("iOS")
#endif
#endif
從 Swift 4.2 開始,您可以替換
#if os(iOS) || os(watchOS) || os(tvOS)
let color = UIColor.redColor()
#elseif os(OSX)
let color = NSColor.redColor()
#else
println("OMG, it's that mythical new Apple product!!!")
#endif
經過
#if canImport(UIKit)
let color = UIColor.redColor()
#elseif os(OSX)
let color = NSColor.redColor()
#else
#error("OMG, it's that mythical new Apple product!!!")
#endif
更新 Mac Catalyst。 您現在還可以使用以下內容來確定是 iOS 還是 Mac Catalyst:
let color: UIColor
#if targetEnvironment(macCatalyst)
color = .systemRed
#else
color = .systemBlue
#endif
例如。
enum TargetDevice {
case nativeMac
case iPad
case iPhone
case iWatch
public static var currentDevice: Self {
var currentDeviceModel = UIDevice.current.model
#if targetEnvironment(macCatalyst)
currentDeviceModel = "nativeMac"
#elseif os(watchOS)
currentDeviceModel = "watchOS"
#endif
if currentDeviceModel.starts(with: "iPhone") {
return .iPhone
}
if currentDeviceModel.starts(with: "iPad") {
return .iPad
}
if currentDeviceModel.starts(with: "watchOS") {
return .iWatch
}
return .nativeMac
}
}
用法:
print(AppUtilities.TargetDevice.currentDevice)
Swift 5和Xcode 13
我正在使用“Scale interface to Match iPad,你不能使用UIDevice.current.userInterfaceIdiom
,因為它會返回一個.iPad
值。但是建議使用targetEnvironment
代替。這里是示例代碼:
#if targetEnvironment(macCatalyst)
//execute your code for mac device
#endif
如果您不使用縮放界面來匹配 iPad,而是使用“優化 mac 界面”,您可以使用此代碼來確定設備:
UIDevice.current.userInterfaceIdiom == .mac
這是提供更多信息的蘋果文檔。
我已經實現了一個超輕量級的庫來檢測使用過的設備 : https : //github.com/schickling/Device.swift
它可以通過Carthage安裝並使用如下:
import Device
let deviceType = UIDevice.currentDevice().deviceType
switch deviceType {
case .IPhone6: print("Do stuff for iPhone6")
case .IPadMini: print("Do stuff for iPad mini")
default: print("Check other available cases of DeviceType")
}
var device = UIDevice.currentDevice().model
這段代碼對我有用。 我已經在文本字段和鍵盤關閉部分實現了這一點。 見下文。
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
print(device)
if (textField.tag == 1 && (device == "iPhone" || device == "iPhone Simulator" ))
{
var scrollPoint:CGPoint = CGPointMake(0,passwordTF.frame.origin.y/2);
LoginScroll!.setContentOffset(scrollPoint, animated: true);
}
else if (textField.tag == 2 && (device == "iPhone" || device == "iPhone Simulator"))
{
var scrollPoint:CGPoint = CGPointMake(0,passwordTF.frame.origin.y/1.3);
LoginScroll!.setContentOffset(scrollPoint, animated: true);
}
return true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.