Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a Visualforce page that is used in both Salesforce1 and the desktop. On that page is a button. That button should only display if the user is loading the page on the desktop. It should be hidden on Salesforce1. Any suggestions?

Essentially I am just looking for a way to detect if the Visualforce page is loaded in Salesforce1 or not. I can handle the logic of displaying the button, I just don't know if it is possible to tell if it is Salesforce1 or not.

share|improve this question
add comment

3 Answers 3

up vote 8 down vote accepted

If you can make due with JavaScript checking you can use the method outlined in this documentation that looks for the sforce JavaScript object, and just adapt it to do your work.

if( (typeof sforce != 'undefined') && (sforce != null) ) {
    // do your SF1 mobile button stuff
}
else {
    // do your desktop stuff
}

There isn't an official way to do a server side check. See this twitter conversation for more, where the following gist by TehNrd (Jason Venable) is referenced which checks for the presence of certain parameters.

//Determine if this is SF1 app
public static Boolean isSF1(){
    if(!Utils.checkNullOrBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameHost')) ||
        !Utils.checkNullOrBlank(ApexPages.currentPage().getParameters().get('sfdcIFrameOrigin')) ||
        ApexPages.currentPage().getParameters().get('isdtp') == 'p1' ||
        (ApexPages.currentPage().getParameters().get('retURL') != null && ApexPages.currentPage().getParameters().get('retURL').contains('projectone') )
    ){
        return true;
    }else{
        return false;
    }
}

There is also an Idea,"Provide a supported url param to determine if page is in SF1", on the Idea Exchange.

share|improve this answer
    
Peter - would the best/simplest way to make a non-SF1 page compatible be to use the If/Else to split the page into two paths? One that has the legacy VF page details with the other being the SF1 view? –  44f May 29 at 10:26
    
@44f - It would depend on the requirements for me. It could be cleaner to do something like two includes or just redirects and share the controller from the two different views (i.e., pages). –  Peter Knolle May 30 at 11:29
add comment

In addition to the officially sanctioned methods above, I wanted to add a way to check if the user is on a mobile device using user agent detection in apex:

public static boolean isMobile(String userAgentString) {
    //Using RegEx, figure out if the user is on a mobile device based on the user-agent string
    if (userAgentString == null)  return false;
    Pattern p = Pattern.compile('Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune');
    Matcher pm = p.matcher( userAgentString );

    return pm.find();

}
share|improve this answer
add comment

Try this:

var isSF1=typeof sforce === 'undefined'?false:true;

isSF1 will be a boolean indicating whether you're loaded in SF1.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.