Additional clarification for "visible in community sites". The iframe src
needs to be dynamic if the page will be visible in both standard Salesforce as well as a Lightning Community.
This is because the relative URL for standard Salesforce access to your Visualforce page is /apex/YourPageName
. However, if you have a community with the URL name "my_community" then the relative url for community access to your Visualforce page is /my_community/apex/YourPageName
. (You can ignore the base of the URL and use relative URLs only.)
Below is a sample method for getting the relative URL dynamically from the apex controller:
@AuraEnabled
public static String getVisualforceRelativeURL(String communityName, String pageName) {
String siteUrl = Site.getMasterLabel();
String returnUrl = '/apex/' + pageName;
if (siteUrl != null && siteUrl.contains(communityName) ) {
returnUrl = '/' + communityName + '/apex/' + pageName;
}
return returnUrl;
}