iOS Cordova应用中使用window.open()打开PDF时能否启用旋转?
window.open() in an iOS Cordova App? Yep, this is totally possible—but you can’t rely on just the window.open parameters to make it happen. Since your app has global rotation disabled, you’ll need to use a Cordova plugin to dynamically adjust the screen orientation when opening the PDF, then revert it once the viewer closes. Here’s how to do it:
Step 1: Install the Screen Orientation Plugin
First, you’ll need the cordova-plugin-screen-orientation plugin to control orientation dynamically. Install it via the command line:
cordova plugin add cordova-plugin-screen-orientation
Step 2: Unlock Orientation Before Opening the PDF
Before calling window.open, unlock the screen orientation to allow all rotations. Then, open your PDF as usual:
// Unlock orientation to allow all rotations screen.orientation.unlock(); // Open the local PDF in InAppBrowser const pdfViewer = window.open('path.to.local.pdf', '_blank', 'EnableViewPortScale=yes,location=no,closebuttoncaption=DONE,toolbarposition=top');
Step 3: Revert to Global Orientation When the Viewer Closes
The window.open call creates an InAppBrowser instance—listen for its exit event to lock the orientation back to your app’s global setting (e.g., portrait) once the user closes the PDF:
pdfViewer.addEventListener('exit', () => { // Revert to your app's default locked orientation screen.orientation.lock('portrait'); });
Key Notes
- InAppBrowser Behavior: The InAppBrowser runs in a separate webview, which by default inherits your app’s global orientation settings. The plugin lets you override this temporarily.
- iOS Compatibility: Test this on your target iOS versions—some older iOS versions may have edge cases with orientation locking in webviews.
- Config.xml Context: Make sure your global orientation lock is set in
config.xml(e.g.,<preference name="Orientation" value="portrait" />)—this is what we’re overriding and reverting to.
内容的提问来源于stack exchange,提问作者stg

