1

这是我第一次开发响应式网站,我尝试使用 CodeIgniter user_agent 类。

我注意到有

is_mobile()

is_browser()

然而,我想到的图片是平板电脑上的网站看起来与浏览器非常相似,只有移动网站才能view完全加载不同的文件。

但是,is_mobile() 包括平板电脑和手机,这不是我所希望的。有没有替代方案?

原因:我使用的是jQuery mobile,我为手机加载了一个完全不同的布局,我不希望这个视图出现在平板电脑上。

4

1 回答 1

6

你有几个选择。

您可以扩展库并创建一个方法来检查平板电脑:

class MY_User_agent extends CI_User_agent {

    public function __construct()
    {
        parent::__construct();
    }

    public function is_tablet()
    {
        //logic to check for tablet
    }
}

// usage
$this->load->library('user_agent');
$this->user_agent->is_tablet();

或者您可以覆盖库中的现有is_mobile()方法以获得所需的功能:

class MY_User_agent extends CI_User_agent {

    public function __construct()
    {
        parent::__construct();
    }

    public function is_mobile()
    {
        // you can copy the original method here and modify it to your needs
    }
}

// usage
$this->load->library('user_agent');
$this->user_agent->is_mobile();

https://www.codeigniter.com/user_guide/general/creating_libraries.html


例子

应用程序/库/MY_User_agent.php:

class MY_User_agent extends CI_User_agent {

    public function __construct()
    {
        parent::__construct();
    }

    public function is_ipad()
    {
        return (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
            // can add other checks for other tablets
    }
}

控制器:

public function index()
{
    $this->load->library('user_agent');

    ($this->agent->is_ipad() === TRUE) ? $is_ipad = "Yes" : $is_ipad = "No";

    echo "Using ipad: $is_ipad";

}
于 2013-04-18T17:19:18.500 回答