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.

(1019)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply