Sunday, February 28, 2016

How to redirect the visualforce page based on the mobile user or browser user in salesforce

Visualforce page:
<apex:page action=”{!checkingAgent}” controller=”agentController”>
</apex:page>


Controller.

public class agentController {
public agentController() {
}
public PageReference checkingAgent() {
String userAgent = ApexPages.currentPage().getHeaders().get(‘User-Agent’);
if (userAgent.contains(‘iPhone’)) {
PageReference pr = Page.iPhoneFriendlyPage;
pr.setRedirect(true);
return pr;
} else if (userAgent.contains(‘iPad’)) {
PageReference pr = Page.iPadFriendlyPage;
pr.setRedirect(true);
return pr;
} else if (userAgent.contains(‘Salesforce’)) {
PageReference pr = Page.SalesforceFriendlyPage;
pr.setRedirect(true);
return pr;
} else if (userAgent.contains(‘BlackBerry’)) {
PageReference pr = Page.BlackBerryFriendlyPage;
pr.setRedirect(true);
return pr;
} else {
PageReference pr = Page.NormalFriendlyPage;
pr.setRedirect(true);
return pr;
}
return null;
}
}

======================
and please make sure to add the following meta tag in your visualforce page:
<apex:page showheader=”false”>
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<title>Visualforce in Mobile View</title>
    <meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;” />
</head>
<body>
<h2>Visualforce in Mobile View:</h2>
</body>
</html>
</apex:page>

No comments:

Post a Comment