Is there a way to perform a mobile detect that EXA

2019-08-08 02:52发布

I'm building a hybrid Facebook app: desktop users will be directed to a Page Tab, while mobile users will be directed to a Mobile Web App.

I need to figure out a fast, accurate way to detect the user's device and perform the appropriate redirect – ideally something not too complex. Can it be done?

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-08 03:22

…Yes! It can absolutely be done.

Facebook automatically redirects mobile users attempting to access a Canvas App (apps.facebook.com/your_app) to its Mobile Web App equivalent.

So, just set up a server-side redirect to apps.facebook.com/your_app, and set up a client-side redirect from the Canvas App to the Page Tab.

Facebook will redirect mobile users to your Mobile Web App; desktop users will land on your Canvas App, which will immediately redirect them to your Page Tab.

The server-side redirect looks like this, in PHP:

<?php
header('Location: http://apps.facebook.com/your_app', TRUE, 302);
exit;

The client-side redirect looks like this, in JavaScript:

<script type="text/javascript">window.top.location.href = "http://yoururl.com/desktop/";</script>

This is a reliable solution, much better than trying to match user agents. Your list of user agents might be different from Facebook's; and indeed, Facebook is very inconsistent with how it performs mobile redirects.

查看更多
登录 后发表回答