问题
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