Print this page

Detect user agent in Visualforce

Knowledge Article Number 000004854
Resolution

When you login through a mobile device, there is a parameter 'USER-AGENT' which gets passed in the .URL. Using this you can redirect users to specific pages.


 

Visualforce page

 
<apex:page action="{!redirect}" controller="MyController">
</apex:page>

 

Controller

 
public class MyController {

public MyController() {
}

public PageReference redirect() {
String userAgent =
ApexPages.currentPage().getHeaders().get('USER-AGENT');

if(userAgent.contains('iPhone')) {
PageReference pr = Page.iPhoneFriendlyPage;
pr.setRedirect(true);
return pr;
}
Return null;
}

}




promote demote