Peter Boesen Peter Boesen - 1 year ago 60
Swift Question

How to make an image visible only on iPhone 6/6s

so I would like to make an image only visible on the iPhone 6 and 6S in xcode with swift or storyboard. Do you guys have any suggestions? :)

Answer Source

The first step is determining if the device is a 6 or 6S. Here's the Swift code to do that:

import Foundation
import UIKit

extension UIApplication
    public func isIPhone6or6S() -> Bool
        let IPHONE_6 = "iPhone7,2"
        let IPHONE_6S = "iPhone8,1"

        var systemInfo = utsname()
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8 where value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))

        return ( identifier == IPHONE_6 || identifier == IPHONE_6S) ? true : false;

Then you can use it in your UIViewController easily enough, something like this:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var phoneOnlyImage: UIImageView!
    @IBOutlet weak var hiddenStatusLabel: UILabel!

    override func viewDidLoad() {

        // hide it if it is not an iPhone 6 or 6S
        self.phoneOnlyImage.hidden = !(UIApplication.sharedApplication().isIPhone6or6S())

        // show the label so you know if it's working
        self.hiddenStatusLabel.text = (self.phoneOnlyImage.hidden) ? "Image is Hidden" : "Image is visible"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download