2016年1月11日 星期一

[ios]判斷iPhone版本

 if( [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad ){
        self.label.text = @"iPad";
    }else{
        
        CGFloat detectionSize = 0;
        if ( [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait ){
            detectionSize = self.view.bounds.size.height;
        }else{
            detectionSize = self.view.bounds.size.width;
        }
        
        NSInteger ht = detectionSize;
        NSLog(@"ht:%ld",ht);
        switch (ht){
            case 480:
                self.label.text = @"iPhone 4S";
                break;
            case 568:
                self.label.text = @"iPhone 5/5s";
                break;
            case 667:
                self.label.text = @"iPhone 6";
                break;
            case 736:
                self.label.text = @"iPhone 6 Plus";
                break;
            default:
                self.label.text = @"iPhone";
        }

    }


Swift version


        if UIDevice.currentDevice().userInterfaceIdiom == .Pad{
            label.text = "iPad"
        }else{
            
            var detectionSize:CGFloat = 0
            
            if self.interfaceOrientation == .Portrait{
                detectionSize = self.view.bounds.height
            }else{
                detectionSize = self.view.bounds.width
            }
            
            switch detectionSize{
            case 480:
                label.text = "iPhone 4S"
            case 568:
                label.text = "iPhone 5/5S"
            case 667:
                label.text = "iPhone 6"
            case 736:
                label.text = "iPhone 6 Plus"
            default:
                label.text = "iPhone"
            }
        }