serbanghita / Mobile-Detect

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Home Page:http://mobiledetect.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

New iPad OS detected as computer

foRsxs opened this issue · comments

Hello! New iPad OS detected as computer. In new version Apple use core like on Mac

Same with Microsoft Surface Go Win 11. Also detected as computer.

Hello! New iPad OS detected as computer. In new version Apple use core like on Mac

I created a hacky way to detect the new iPad iOS. I found that the version number on a Mac contains a dot (.) and the version number on an iPad contains an underscore(_).

I'm just checking the version for an underscore and setting a global variable.

This may be of use to someone else.

(As a side note: I'm actually using this to set a session cookie within my own site rather than a global variable. But that code is out of scope for this reply.)

       /**
	 * HACKY WAY TO SEE IF WE'RE ON A IOS DEVICE 
	 ** Checking the user agent
	 ** Separate the content between the brackets returned by the user agent 
	 ** Create an array of the mobile device os separated by semicolon
	 ** Check for an underscore which is only used by iOS within the version number 
	 ** @example  '10.1' (Mac OS) / '10_1' (iOS)
	 */
	$mobile_detect = new \Detection\MobileDetect;
	$userAgent     = $mobile_detect->userAgent;
	$explode       = explode( '(', $userAgent );
	preg_match( '#\((.*?)\)#', $userAgent, $match );
	$current_os        = $match[1] ?? '';
	$current_os_array  = explode( '; ', $current_os );
	$mobile_os_version = ( str_contains( $current_os_array[1], '_' ) && str_contains( $current_os_array[1], 'OS X' ) );

	// Create the global variable...
	$GLOBALS['is_new_apple_tablet'] = $mobile_os_version; // @return boolean true || false