Skip to content

Commit

Permalink
fix(ios): support excluding privacy sensitive API usage
Browse files Browse the repository at this point in the history
Closes #73
  • Loading branch information
alpha0010 committed Mar 18, 2024
1 parent 928ccf9 commit 4dbe524
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ npm install react-native-file-access
cd ios && pod install
```

Apple restricts usage of certain privacy sensitive API calls. If you do not
use disk space measurements or file timestamps, define the following variable
in your Podfile to exclude restricted API calls.
[More details.](https://github.com/alpha0010/react-native-file-access/issues/73)

```ruby
$RNFANoPrivacyAPI = true
```

If the app does not use autolinking, continue to the [manual install instructions](https://github.com/alpha0010/react-native-file-access/wiki/Manual-Installation) in the wiki.

### Compatibility
Expand Down
7 changes: 7 additions & 0 deletions ReactNativeFileAccess.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ Pod::Spec.new do |s|
s.dependency "React-Core"
s.dependency "ZIPFoundation", "0.9.11"

if defined?($RNFANoPrivacyAPI)
Pod::UI.puts "#{s.name}: Removing privacy sensitive API calls"
s.pod_target_xcconfig = {
"OTHER_SWIFT_FLAGS" => "-DNO_PRIVACY_API"
}
end

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
Expand Down
11 changes: 10 additions & 1 deletion ios/FileAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public class FileAccess : NSObject {
@objc(df:withRejecter:)
public func df(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
#if NO_PRIVACY_API
reject("ERR", "Filesystem stat API disabled via compile time flag.", nil)
#else
do {
let stat = try FileManager.default.attributesOfFileSystem(
forPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
Expand All @@ -138,6 +141,7 @@ public class FileAccess : NSObject {
} catch {
reject("ERR", "Failed to stat filesystem. \(error.localizedDescription)", error)
}
#endif
}
}

Expand Down Expand Up @@ -350,9 +354,14 @@ public class FileAccess : NSObject {
private func statFile(path: String) throws -> [String : Any?] {
let pathUrl = URL(fileURLWithPath: path.path())
let attrs = try FileManager.default.attributesOfItem(atPath: path.path())
#if NO_PRIVACY_API
let lastModified = 0
#else
let lastModified = 1000 * (attrs[.modificationDate] as! NSDate).timeIntervalSince1970
#endif
return [
"filename": pathUrl.lastPathComponent,
"lastModified": 1000 * (attrs[.modificationDate] as! NSDate).timeIntervalSince1970,
"lastModified": lastModified,
"path": pathUrl.path,
"size": attrs[.size],
"type": self.checkIfIsDirectory(path: path).isDirectory ? "directory" : "file"
Expand Down

0 comments on commit 4dbe524

Please sign in to comment.