Skip to content

Commit

Permalink
Merge pull request #2 from capacitor-community/feature/enable-ios-pla…
Browse files Browse the repository at this point in the history
…tform

feature: enabled plugin support on iOS platform
  • Loading branch information
ryaa authored Aug 22, 2022
2 parents 5e75064 + fc149f3 commit f10b537
Show file tree
Hide file tree
Showing 23 changed files with 1,662 additions and 23 deletions.
17 changes: 17 additions & 0 deletions CapacitorCommunityFileOpener.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
s.name = 'CapacitorCommunityFileOpener'
s.version = package['version']
s.summary = package['description']
s.license = package['license']
s.homepage = package['repository']['url']
s.author = package['author']
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
s.ios.deployment_target = '13.0'
s.dependency 'Capacitor'
s.swift_version = '5.1'
end
24 changes: 24 additions & 0 deletions MimeTypeConverter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// MimeTypeConverter.swift
// CapacitorCommunityFileOpener
//
// Created by Alex Ryltsov on 19.08.2022.
//

import Foundation
import MobileCoreServices

public struct MimeTypeConverter {

public static func mimeToUti(_ mimeType: String) -> String? {
guard let contentType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil) else { return nil }

return contentType.takeRetainedValue() as String
}

public static func fileExtensionToUti(_ ext: String) -> String? {
guard let contentType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext as CFString, nil) else { return nil }
return contentType.takeRetainedValue() as String
}

}
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,25 @@ Method to open a file.

file open method options

| Prop | Type | Description | Since |
| --------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`filePath`** | <code>string</code> | file path | 1.0.0 |
| **`contentType`** | <code>string</code> | MIME type | 1.0.0 |
| **`openWithDefault`** | <code>boolean</code> | Use the default Android platform chooser. If false, it will show "Open File in.." title of the chooser dialog, the system will always present the chooser dialog even if the user has chosen a default one and if no activity is found to handle the file, the system will still present a dialog with the specified title and an error message No application can perform this action | 1.0.0 |
| Prop | Type | Description | Since |
| --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`filePath`** | <code>string</code> | file path | 1.0.0 |
| **`contentType`** | <code>string</code> | MIME type (optional) | 1.0.0 |
| **`openWithDefault`** | <code>boolean</code> | Use the default platform chooser, if true, otherwise: On Android: it will show "Open File in.." title of the chooser dialog, the system will always present the chooser dialog even if the user has chosen a default one and if no activity is found to handle the file, the system will still present a dialog with the specified title and an error message No application can perform this action On iOS: it will presents a menu restricted to a list of apps capable of opening the current document. This determination is made based on the document type and on the document types supported by the installed apps. To support one or more document types, an app must register those types in its Info.plist file using the CFBundleDocumentTypes key. (optional) default value is true | 1.0.0 |

</docgen-api>

### List of Error Codes and Meanings
When an error is thrown, one of the following codes (as a string value) will be used.

| Code | Description |
|-----:|:---------------------------------|
| '1' | `INTERNAL_ERROR` |
| '2' | `INVALID_ARGUMENT` |
' '8' | `FILE_NOT_SUPPORTED` |
| '9' | `FILE_NOT_FOUND` |
| '10' | `UNKNOWN` |

### Android

If you app needs to open files in the external directories, then within your `AndroidManifest.xml` file, change the following:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

import org.json.JSONObject;

import java.io.File;

@CapacitorPlugin(name = "FileOpener")
Expand Down Expand Up @@ -45,11 +48,13 @@ public void open(PluginCall call) {
getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
}
call.resolve();
} catch (android.content.ActivityNotFoundException exception) {
call.reject("Activity not found: " + exception.getMessage(), "8", exception);
} catch (Exception exception) {
call.reject(exception.getLocalizedMessage(), null, exception);
call.reject(exception.getLocalizedMessage(), "1", exception);
}
} else {
call.reject("File not found");
call.reject("File not found", "9");
}
}

Expand Down
Loading

0 comments on commit f10b537

Please sign in to comment.