You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
5.7 KiB

  1. <!---
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. -->
  17. Notes for plugin developers
  18. ===========================
  19. These notes are primarily intended for Android and iOS developers who want to write plugins which interface with the file system using the File plugin.
  20. Working with Cordova file system URLs
  21. -------------------------------------
  22. Since version 1.0.0, this plugin has used URLs with a `cdvfile` scheme for all communication over the bridge, rather than exposing raw device file system paths to JavaScript.
  23. On the JavaScript side, this means that FileEntry and DirectoryEntry objects have a fullPath attribute which is relative to the root of the HTML file system. If your plugin's JavaScript API accepts a FileEntry or DirectoryEntry object, you should call `.toURL()` on that object before passing it across the bridge to native code.
  24. ### Converting cdvfile:// URLs to fileystem paths
  25. Plugins which need to write to the filesystem may want to convert a received file system URL to an actual filesystem location. There are multiple ways of doing this, depending on the native platform.
  26. It is important to remember that not all `cdvfile://` URLs are mappable to real files on the device. Some URLs can refer to assets on device which are not represented by files, or can even refer to remote resources. Because of these possibilities, plugins should always test whether they get a meaningful result back when trying to convert URLs to paths.
  27. #### Android
  28. On Android, the simplest method to convert a `cdvfile://` URL to a filesystem path is to use `org.apache.cordova.CordovaResourceApi`. `CordovaResourceApi` has several methods which can handle `cdvfile://` URLs:
  29. // webView is a member of the Plugin class
  30. CordovaResourceApi resourceApi = webView.getResourceApi();
  31. // Obtain a file:/// URL representing this file on the device,
  32. // or the same URL unchanged if it cannot be mapped to a file
  33. Uri fileURL = resourceApi.remapUri(Uri.parse(cdvfileURL));
  34. It is also possible to use the File plugin directly:
  35. import org.apache.cordova.file.FileUtils;
  36. import org.apache.cordova.file.FileSystem;
  37. import java.net.MalformedURLException;
  38. // Get the File plugin from the plugin manager
  39. FileUtils filePlugin = (FileUtils)webView.pluginManager.getPlugin("File");
  40. // Given a URL, get a path for it
  41. try {
  42. String path = filePlugin.filesystemPathForURL(cdvfileURL);
  43. } catch (MalformedURLException e) {
  44. // The filesystem url wasn't recognized
  45. }
  46. To convert from a path to a `cdvfile://` URL:
  47. import org.apache.cordova.file.LocalFilesystemURL;
  48. // Get a LocalFilesystemURL object for a device path,
  49. // or null if it cannot be represented as a cdvfile URL.
  50. LocalFilesystemURL url = filePlugin.filesystemURLforLocalPath(path);
  51. // Get the string representation of the URL object
  52. String cdvfileURL = url.toString();
  53. If your plugin creates a file, and you want to return a FileEntry object for it, use the File plugin:
  54. // Return a JSON structure suitable for returning to JavaScript,
  55. // or null if this file is not representable as a cdvfile URL.
  56. JSONObject entry = filePlugin.getEntryForFile(file);
  57. #### iOS
  58. Cordova on iOS does not use the same `CordovaResourceApi` concept as Android. On iOS, you should use the File plugin to convert between URLs and filesystem paths.
  59. // Get a CDVFilesystem URL object from a URL string
  60. CDVFilesystemURL* url = [CDVFilesystemURL fileSystemURLWithString:cdvfileURL];
  61. // Get a path for the URL object, or nil if it cannot be mapped to a file
  62. NSString* path = [filePlugin filesystemPathForURL:url];
  63. // Get a CDVFilesystem URL object for a device path, or
  64. // nil if it cannot be represented as a cdvfile URL.
  65. CDVFilesystemURL* url = [filePlugin fileSystemURLforLocalPath:path];
  66. // Get the string representation of the URL object
  67. NSString* cdvfileURL = [url absoluteString];
  68. If your plugin creates a file, and you want to return a FileEntry object for it, use the File plugin:
  69. // Get a CDVFilesystem URL object for a device path, or
  70. // nil if it cannot be represented as a cdvfile URL.
  71. CDVFilesystemURL* url = [filePlugin fileSystemURLforLocalPath:path];
  72. // Get a structure to return to JavaScript
  73. NSDictionary* entry = [filePlugin makeEntryForLocalURL:url]
  74. #### JavaScript
  75. In JavaScript, to get a `cdvfile://` URL from a FileEntry or DirectoryEntry object, simply call `.toURL()` on it:
  76. var cdvfileURL = entry.toURL();
  77. In plugin response handlers, to convert from a returned FileEntry structure to an actual Entry object, your handler code should import the File plugin and create a new object:
  78. // create appropriate Entry object
  79. var entry;
  80. if (entryStruct.isDirectory) {
  81. entry = new DirectoryEntry(entryStruct.name, entryStruct.fullPath, new FileSystem(entryStruct.filesystemName));
  82. } else {
  83. entry = new FileEntry(entryStruct.name, entryStruct.fullPath, new FileSystem(entryStruct.filesystemName));
  84. }