Is there a way to perform a mobile detect that EXACTLY matches Facebook's mobile detect?

十年热恋 提交于 2019-12-24 12:32:09

问题


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:


…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.



来源:https://stackoverflow.com/questions/15751011/is-there-a-way-to-perform-a-mobile-detect-that-exactly-matches-facebooks-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!