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

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

its possible to redirect the visualforce page either mobile page view or normal browser page view based on the condition (User-Agent).

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

Apex Class (agentController):
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”>
<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>
this is a purpose to A typical mobile-optimized site contains something like the above code.

You may also like

Leave a Comment