Skip to content

Commit 58d9ad5

Browse files
committed
Add Swift Package Manager support
1 parent 30b2658 commit 58d9ad5

File tree

9 files changed

+124
-47
lines changed

9 files changed

+124
-47
lines changed

open_file_ios/ios/Assets/.gitkeep

Whitespace-only changes.

open_file_ios/ios/open_file_ios.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
Pod::Spec.new do |s|
55
s.name = 'open_file_ios'
6-
s.version = '0.0.1'
6+
s.version = '1.0.3'
77
s.summary = 'The iOS implementation of open_file.'
88
s.description = <<-DESC
99
A new Flutter project.
@@ -12,10 +12,10 @@ A new Flutter project.
1212
s.license = { :file => '../LICENSE' }
1313
s.author = { 'crazecoder' => '21527312@qq.com' }
1414
s.source = { :path => '.' }
15-
s.source_files = 'Classes/**/*'
16-
s.public_header_files = 'Classes/**/*.h'
15+
s.source_files = 'open_file_ios/Sources/open_file_ios/**/*.{h,m}'
16+
s.public_header_files = 'open_file_ios/Sources/open_file_ios/**/*.h'
1717
s.dependency 'Flutter'
1818

19-
s.ios.deployment_target = '8.0'
19+
s.ios.deployment_target = '12.0'
2020
end
2121

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version: 5.9
2+
3+
// Copyright 2013 The Flutter Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
import PackageDescription
8+
9+
let package = Package(
10+
name: "open_file_ios",
11+
platforms: [
12+
.iOS("12.0")
13+
],
14+
products: [
15+
.library(name: "open_file_ios", targets: ["open_file_ios"])
16+
],
17+
dependencies: [],
18+
targets: [
19+
.target(
20+
name: "open_file_ios",
21+
dependencies: [],
22+
// exclude: ["include/open_file_ios-umbrella.h", "include/OpenFilePlugin.modulemap"],
23+
resources: [
24+
// TODO: If your plugin requires a privacy manifest
25+
// (e.g. if it uses any required reason APIs), update the PrivacyInfo.xcprivacy file
26+
// to describe your plugin's privacy impact, and then uncomment this line.
27+
// For more information, see:
28+
// https://developer.apple.com/documentation/bundleresources/privacy_manifest_files
29+
// .process("PrivacyInfo.xcprivacy"),
30+
31+
// TODO: If you have other resources that need to be bundled with your plugin, refer to
32+
// the following instructions to add them:
33+
// https://developer.apple.com/documentation/xcode/bundling-resources-with-a-swift-package
34+
],
35+
cSettings: [
36+
.headerSearchPath("include/open_file_ios")
37+
]
38+
)
39+
]
40+
)

open_file_ios/ios/Classes/OpenFilePlugin.m renamed to open_file_ios/ios/open_file_ios/Sources/open_file_ios/OpenFilePlugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import "OpenFilePlugin.h"
1+
#import "./include/open_file_ios/OpenFilePlugin.h"
22

33
@interface OpenFilePlugin ()<UIDocumentInteractionControllerDelegate>
44
@end

open_file_mac/macos/open_file_mac.podspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
Pod::Spec.new do |s|
66
s.name = 'open_file_mac'
7-
s.version = '0.0.1'
7+
s.version = '1.0.3'
88
s.summary = 'The MacOS implementation of open_file.'
99
s.description = <<-DESC
1010
A new Flutter plugin project.
@@ -14,7 +14,8 @@ A new Flutter plugin project.
1414
s.author = { 'crazecoder' => '21527312@qq.com' }
1515

1616
s.source = { :path => '.' }
17-
s.source_files = 'Classes/**/*'
17+
s.source_files = 'open_file_mac/Sources/open_file_mac/**/*.swift'
18+
s.resource_bundles = {'open_file_mac_privacy' => ['open_file_mac/Sources/open_file_mac/Resources/PrivacyInfo.xcprivacy']}
1819
s.dependency 'FlutterMacOS'
1920

2021
s.platform = :osx, '10.11'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
// Copyright 2013 The Flutter Authors. All rights reserved.
5+
// Use of this source code is governed by a BSD-style license that can be
6+
// found in the LICENSE file.
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
name: "open_file_mac",
12+
platforms: [
13+
.macOS("10.14")
14+
],
15+
products: [
16+
.library(name: "open_file_mac", targets: ["open_file_mac"])
17+
],
18+
dependencies: [],
19+
targets: [
20+
.target(
21+
name: "open_file_mac",
22+
dependencies: [],
23+
resources: [
24+
.process("Resources")
25+
]
26+
)
27+
]
28+
)

open_file_mac/macos/Classes/OpenFilePlugin.swift renamed to open_file_mac/macos/open_file_mac/Sources/open_file_mac/OpenFilePlugin.swift

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
1111
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
1212
switch call.method {
1313
case "open_file":
14-
let arguments = call.arguments as? Dictionary<String,Any>
14+
let arguments = call.arguments as? [String: Any]
1515
let filePath = arguments!["file_path"] as? String
16-
if(filePath==nil){
17-
self.result(result:result,message:"the file path cannot be null",type:-4)
16+
if filePath == nil {
17+
self.result(result: result, message: "the file path cannot be null", type: -4)
1818
return
1919
}
2020
let fileExist = FileManager.default.fileExists(atPath: filePath!)
21-
if(fileExist){
21+
if fileExist {
2222
let documentURL = URL(fileURLWithPath: filePath!)
2323
let fileType = documentURL.pathExtension
24-
if(!canOpen(filePath: filePath!)){
25-
self.openSelectPanel(filePath:filePath!,fileType: fileType ,result: result)
26-
}else{
24+
if !self.canOpen(filePath: filePath!) {
25+
self.openSelectPanel(filePath: filePath!, fileType: fileType, result: result)
26+
} else {
2727
self.open(documentURL: documentURL, result: result)
2828
}
29-
}else{
30-
self.result(result:result,message:"the file does not exist",type:-2)
29+
} else {
30+
self.result(result: result, message: "the file does not exist", type: -2)
3131
}
3232

3333
default:
3434
result(FlutterMethodNotImplemented)
3535
}
3636
}
3737

38-
private func canOpen(filePath: String) ->Bool{
38+
private func canOpen(filePath: String) ->Bool {
3939
return FileManager.default.isReadableFile(atPath: filePath)
4040
}
4141

42-
private func convertDictionaryToString(dict:[String:Any]) -> String {
43-
var result:String = ""
42+
private func convertDictionaryToString(dict: [String: Any])->String {
43+
var result = ""
4444
do {
45-
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.init(rawValue: 0))
45+
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions(rawValue: 0))
4646

4747
if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
4848
result = JSONString
@@ -54,8 +54,8 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
5454
return result
5555
}
5656

57-
private func openSelectPanel(filePath:String,fileType: String,result: @escaping FlutterResult){
58-
let fileUrl = URL(fileURLWithPath:filePath)
57+
private func openSelectPanel(filePath: String, fileType: String, result: @escaping FlutterResult) {
58+
let fileUrl = URL(fileURLWithPath: filePath)
5959
let openPanel = NSOpenPanel()
6060
openPanel.canChooseFiles = false
6161
openPanel.canChooseDirectories = true
@@ -65,42 +65,41 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
6565
openPanel.showsHiddenFiles = true
6666
openPanel.allowsOtherFileTypes = false
6767
let currentLanguage = Locale.current.languageCode
68-
openPanel.prompt = (currentLanguage == "zh") ?"允许":"Accept"
69-
openPanel.beginSheetModal(for: NSApplication.shared.mainWindow!) { (openResult) in
70-
if (openResult.rawValue == NSApplication.ModalResponse.OK.rawValue) {
68+
openPanel.prompt = (currentLanguage == "zh") ?"允许" : "Accept"
69+
openPanel.beginSheetModal(for: NSApplication.shared.mainWindow!) { openResult in
70+
if openResult.rawValue == NSApplication.ModalResponse.OK.rawValue {
7171
let selectedURL = openPanel.url!
72-
var isReadable: Bool = false
72+
var isReadable = false
7373
if FileManager.default.fileExists(atPath: selectedURL.path, isDirectory: nil) && FileManager.default.isReadableFile(atPath: selectedURL.path) {
7474
isReadable = true
7575
}
7676
if isReadable {
7777
self.open(documentURL: fileUrl, result: result)
7878
} else {
79-
self.result(result:result,message:"Operation not permitted",type:-3)
79+
self.result(result: result, message: "Operation not permitted", type: -3)
8080
}
81-
}else{
82-
self.result(result:result,message:"Operation not permitted",type:-3)
81+
} else {
82+
self.result(result: result, message: "Operation not permitted", type: -3)
8383
}
8484
}
8585
}
8686

87-
private func open(documentURL:URL,result: FlutterResult){
87+
private func open(documentURL: URL, result: FlutterResult) {
8888
NSWorkspace.shared.open([documentURL], withAppBundleIdentifier: nil, options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
89-
self.result(result:result,message:"done",type:0)
89+
self.result(result: result, message: "done", type: 0)
9090
}
9191

92-
private func result(result: FlutterResult,message:String,type:Int){
93-
let map = ["message":message, "type":type]as [String : Any]
94-
result(convertDictionaryToString(dict: map))
92+
private func result(result: FlutterResult, message: String, type: Int) {
93+
let map = ["message": message, "type": type] as [String: Any]
94+
result(self.convertDictionaryToString(dict: map))
9595
}
9696

9797
@available(*, deprecated, message: "This method is just test.")
98-
private func requestDiskPermission(onGranted:()->Void,onDenied:()->Void){
99-
98+
private func requestDiskPermission(onGranted: ()->Void, onDenied: ()->Void) {
10099
if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles") {
101100
if NSWorkspace.shared.open(url) {
102101
print("Opened successfully")
103-
}else{
102+
} else {
104103
onDenied()
105104
return
106105
}
@@ -109,7 +108,7 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
109108
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
110109
let status = AXIsProcessTrustedWithOptions(options)
111110

112-
if (status != true) {
111+
if status != true {
113112
print("Prompting for accessibility permissions")
114113
let alert = NSAlert()
115114
alert.messageText = "This app requires disk permissions to function properly. Please grant access in the System Preferences app when prompted."
@@ -126,10 +125,9 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
126125
task.waitUntilExit()
127126
onGranted()
128127
print("Full Disk Access granted successfully")
129-
130128
}
131129

132-
func checkFullDiskAccess() -> Bool {
130+
func checkFullDiskAccess()->Bool {
133131
let fileManager = FileManager.default
134132
let testFilePath = "/Library/Application Support/com.apple.TCC/TCC.db"
135133

@@ -141,34 +139,34 @@ public class OpenFilePlugin: NSObject, FlutterPlugin {
141139
}
142140

143141
@available(*, deprecated, message: "This method is no longer used.")
144-
private func getUTI(fileType:String)->String{
142+
private func getUTI(fileType: String)->String {
145143
var uti = ""
146-
switch fileType.lowercased(){
144+
switch fileType.lowercased() {
147145
case "rtf":
148146
uti = "public.rtf"
149147
case "txt":
150148
uti = "public.plain-text"
151-
case "html","htm":
149+
case "html", "htm":
152150
uti = "public.html"
153151
case "xml":
154152
uti = "public.xml"
155153
case "tar":
156154
uti = "public.tar-archive"
157-
case "gz","gzip":
155+
case "gz", "gzip":
158156
uti = "org.gnu.gnu-zip-archive"
159157
case "tgz":
160158
uti = "org.gnu.gnu-zip-tar-archive"
161-
case "jpg","jpeg":
159+
case "jpg", "jpeg":
162160
uti = "public.jpeg"
163161
case "png":
164162
uti = "public.png"
165163
case "avi":
166164
uti = "public.avi"
167-
case "mpg","mpeg":
165+
case "mpg", "mpeg":
168166
uti = "public.mpeg"
169167
case "mp4":
170168
uti = "public.mpeg-4"
171-
case "3gpp","3gp":
169+
case "3gpp", "3gp":
172170
uti = "public.3gpp"
173171
case "mp3":
174172
uti = "public.mp3"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.app-sandbox</key>
6+
<true/>
7+
<key>com.apple.security.files.user-selected.read-only</key>
8+
<true/>
9+
</dict>
10+
</plist>

0 commit comments

Comments
 (0)