Skip to content

Commit

Permalink
Merge pull request #14 from MegaMaddin/ios_appgroups
Browse files Browse the repository at this point in the history
feat(ios): add resolving Apple based app-groups directory
  • Loading branch information
alpha0010 authored Apr 12, 2021
2 parents c4d9d49 + c208ea7 commit 1e15e90
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ type FetchResult = {
`FilesSystem.hash(path: string, algorithm: 'MD5' | 'SHA-1' | 'SHA-224' | 'SHA-256' | 'SHA-384' | 'SHA-512'): Promise<string>`
- Hash the file content.

`FilesSystem.getAppGroupDir(groupName: string): Promise<string>`
- Get the directory for your app group (iOS only).
- App groups are used on iOS/MacOS for storing content, which is shared between apps.
- This is e.g. useful for sharing data between your iOS app and a widget or a watch app.


`FilesSystem.isDir(path: string): Promise<boolean>`
- Check if a path is a directory.

Expand Down
4 changes: 4 additions & 0 deletions ios/FileAccess.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ @interface RCT_EXTERN_REMAP_MODULE(RNFileAccess, FileAccess, NSObject)
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(getAppGroupDir:(NSString *)groupName
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(hash:(NSString *)path withAlgorithm:(NSString *)algorithm
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
Expand Down
10 changes: 10 additions & 0 deletions ios/FileAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ class FileAccess: NSObject {
downloadTask.resume()
}

@objc(getAppGroupDir:withResolver:withRejecter:)
func getAppGroupDir(groupName: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupName) else {
reject("ERR", "Could not resolve app group directory. The group name '\(groupName)' is invalid.", nil)
return
}

resolve(groupURL.path)
}

@objc(hash:withAlgorithm:withResolver:withRejecter:)
func hash(path: String, algorithm: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
guard let data = NSData(contentsOfFile: path) else {
Expand Down
13 changes: 13 additions & 0 deletions jest/react-native-file-access.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global jest */

import { Platform } from 'react-native';
import type {
ExternalDir,
FetchResult,
Expand Down Expand Up @@ -103,6 +104,18 @@ class FileSystemMock {
}
);

/**
* Return the local storage directory for app groups.
*
* This is an Apple only feature.
*/
public getAppGroupDir = jest.fn((groupName: string) => {
if (Platform.OS !== 'ios' && Platform.OS !== 'macos') {
throw new Error('AppGroups are available on Apple devices only');
}
return `${Dirs.DocumentDir}/shared/AppGroup/${groupName}`;
});

/**
* Hash the file content.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NativeModules } from 'react-native';
import { NativeModules, Platform } from 'react-native';
import { FileAccessNative } from './native';
import type {
Encoding,
Expand Down Expand Up @@ -92,6 +92,20 @@ export const FileSystem = {
return FileAccessNative.fetch(resource, init);
},

/**
* Return the local storage directory for app groups.
*
* This is an Apple only feature.
*/
getAppGroupDir(groupName: string) {
if (Platform.OS !== 'ios' && Platform.OS !== 'macos') {
return Promise.reject(
new Error('AppGroups are available on Apple devices only')
);
}
return FileAccessNative.getAppGroupDir(groupName);
},

/**
* Hash the file content.
*/
Expand Down
1 change: 1 addition & 0 deletions src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type FileAccessType = {
path?: string;
}
): Promise<FetchResult>;
getAppGroupDir(groupName: string): Promise<string>;
hash(path: string, algorithm: HashAlgorithm): Promise<string>;
isDir(path: string): Promise<boolean>;
ls(path: string): Promise<string[]>;
Expand Down

0 comments on commit 1e15e90

Please sign in to comment.