Cross-platform Compatibility Issues
This page collects common compatibility issues and solutions in uni-app cross-platform development.
Basic Compatibility Issues
Q: Inconsistent style appearance across platforms
Problem Description: The same style code displays differently on different platforms (e.g., WeChat Mini Program, H5, App).
Solutions:
- Use flex layout to improve cross-platform consistency
- Avoid using platform-specific style properties
- Use conditional compilation to handle platform differences:css
/* #ifdef H5 */ .box { width: 200px; } /* #endif */ /* #ifdef MP-WEIXIN */ .box { width: 200rpx; } /* #endif */
- Use built-in style variables provided by uni-app
Q: Inconsistent font and icon display
Problem Description: Fonts and icons display differently across platforms.
Solutions:
- Use generic font families (sans-serif, serif, etc.)
- Use icon fonts (like iconfont) instead of image icons
- Set different font sizes and line heights for specific platforms
- Use icon components from uni-app component libraries
Q: Inconsistent page scrolling behavior
Problem Description: Page scrolling and elastic effects behave differently across platforms.
Solutions:
- Use
<scroll-view>
component instead of native scrolling - Uniformly configure scroll parameters and behavior
- Avoid using platform-specific scroll effects
- Use conditional compilation to set different scroll parameters for different platforms
Functional Compatibility Issues
Q: APIs unavailable on certain platforms
Problem Description: Some APIs are unavailable or behave differently on specific platforms.
Solutions:
- Use conditional compilation to handle platform differences:js
// #ifdef APP-PLUS // Code executed only on App platform // #endif // #ifdef H5 // Code executed only on H5 platform // #endif
- Use API fallback handling:js
// Prefer platform-specific API, fallback to common implementation function getLocation() { // #ifdef APP-PLUS return useAppLocationAPI(); // #endif // #ifndef APP-PLUS return useCommonLocationAPI(); // #endif }
- Consult documentation to understand API differences across platforms
- Use polyfills or custom implementations to fill missing functionality
Q: Components unavailable on certain platforms
Problem Description: Some components are unavailable or behave differently on specific platforms.
Solutions:
- Use conditional compilation to load different component implementations for different platforms
- Use cross-platform component libraries (like uni-ui)
- Create custom components for cross-platform compatibility
- Design different interaction methods for different platforms
Q: Event handling differences
Problem Description: Event triggering and handling differ across platforms.
Solutions:
- Use uni-app's unified event handling approach
- Avoid using platform-specific events
- Provide unified handling logic for touch and mouse events
- Use conditional compilation to handle platform-specific event differences
Platform-Specific Issues
Q: WeChat Mini Program specific issues
Problem Description: Specific compatibility issues encountered in WeChat Mini Program.
Solutions:
- Follow WeChat Mini Program specifications and limitations
- Pay attention to syntax differences between wxs and vue
- Use conditional compilation to handle WeChat Mini Program specific logic
- Understand and handle WeChat Mini Program page lifecycle differences
Q: H5 specific issues
Problem Description: Specific compatibility issues encountered on H5 platform.
Solutions:
- Handle browser compatibility issues, especially for older browsers
- Pay attention to CORS issues in H5
- Use responsive design to adapt to different screen sizes
- Handle H5-specific touch and mouse event differences
Q: App specific issues
Problem Description: Specific compatibility issues encountered on App platform.
Solutions:
- Handle Android and iOS system differences
- Pay attention to native component layering and occlusion issues
- Handle App permission and system capability differences
- Use conditional compilation to distinguish Android and iOS platforms:js
// #ifdef APP-PLUS-ANDROID // Android specific code // #endif // #ifdef APP-PLUS-IOS // iOS specific code // #endif
Layout and Style Compatibility
Q: rpx unit behavior across platforms
Problem Description: Inconsistent conversion and behavior of rpx units across platforms.
Solutions:
- Understand rpx design principle: 1rpx = screen width/750
- Avoid mixing rpx with other units
- Consider using px units for elements requiring precise control
- Use media queries to handle extreme screen sizes
Q: Safe area adaptation issues
Problem Description: Safe area adaptation issues for full-screen devices, notched screens, etc.
Solutions:
- Use CSS variables to adapt to safe areas:css
.container { padding-bottom: env(safe-area-inset-bottom); padding-top: env(safe-area-inset-top); }
- Use uni-app's system info API to get safe area dimensions
- Avoid placing important content in areas that might be occluded
- Design different layout schemes for different device types
Q: Flex layout compatibility issues
Problem Description: Flex layout behaves differently across platforms.
Solutions:
- Use basic flex properties, avoid advanced features
- Explicitly set dimensions and scaling behavior for flex children
- Avoid complex nested flex layouts
- Test and adjust flex layouts for specific platforms
Performance Compatibility Issues
Q: Large performance differences across platforms
Problem Description: Application runs smoothly on high-performance devices but lags on low-end devices.
Solutions:
- Optimize rendering performance for low-end devices:
- Reduce number of page elements
- Optimize large list rendering
- Reduce unnecessary animation effects
- Use conditional compilation to provide different implementations for different performance levels
- Implement performance degradation strategies, disable high-consumption features on low-end devices
- Use
uni.getSystemInfo()
to get device information and adjust functionality accordingly
Q: Animation effect compatibility issues
Problem Description: Animation effects are inconsistent or have large performance differences across platforms.
Solutions:
- Use simple CSS animations instead of complex animations
- Avoid using extensive JS animations
- Reduce or disable animations for low-end devices
- Use conditional compilation to provide different animation implementations for different platforms
Debugging and Testing Techniques
Multi-platform Testing Strategy
- Establish testing matrix covering main target platforms and devices
- Prioritize testing platforms with largest user base
- Use real device testing, don't rely solely on simulators
- Design different test cases for different platforms
Effective Use of Conditional Compilation
- Use conditional compilation to handle platform differences, not runtime judgment
- Encapsulate platform-specific code as independent modules
- Avoid overusing conditional compilation leading to unmaintainable code
- Use unified interfaces to encapsulate platform differences
Compatibility Issue Troubleshooting Process
- Determine specific platform and conditions where problem occurs
- Create minimal reproduction example
- Consult documentation to understand platform limitations and differences
- Find alternative implementations or fallback solutions
- Test solutions on all target platforms
Best Practices
Design Phase
- Understand design specifications and limitations of each target platform
- Consider cross-platform compatibility during design, avoid platform-specific interaction patterns
- Design alternative solutions for different platforms
- Prioritize using common components and interaction patterns
Development Phase
- Follow uni-app development specifications
- Use cross-platform component libraries
- Avoid direct DOM manipulation
- Encapsulate platform differences, provide unified interfaces
Testing Phase
- Test on multiple devices and platforms
- Focus on edge cases and extreme conditions
- Test performance under different network conditions
- Collect user feedback for continuous improvement
Release Phase
- Develop different release strategies for different platforms
- Use gradual rollout to reduce risks
- Establish monitoring systems to detect issues promptly
- Prepare contingency plans for platform-specific issues
If you cannot find a solution to your problem on this page, please check the uni-app official documentation or ask questions on the uni-app official forum.