使用 Qt WebAssembly 检测移动浏览器

问题描述 投票:0回答:1

我尝试通过检查

screen.orientation
来实现它,如下所示:https://stackoverflow.com/a/14301832/4159530,但即使对于桌面 Chrome 浏览器,它也会返回
true

#include <emscripten.h>

#include "widget.h"

EM_JS(bool, callMobileCheck, (), {
    let check = false;

    if (typeof screen.orientation !== "undefined") {
        check = true;
    }

    return check;
})

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    bool result = callMobileCheck();
    qDebug() << result;
}
c++ qt webassembly
1个回答
0
投票

解决方案:https://stackoverflow.com/a/29509267/4159530

#include <emscripten.h>

#include "widget.h"

EM_JS(bool, isMobile, (), {
    return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);;
})

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    qDebug() << "isMobile:" << isMobile();
}

QT += core gui widgets

CONFIG += c++17

INCLUDEPATH += "C:\emsdk\upstream\emscripten\cache\sysroot\include"

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h
© www.soinside.com 2019 - 2024. All rights reserved.