diff --git a/packages/path_provider/lib/path_provider.dart b/packages/path_provider/lib/path_provider.dart index 08e35d68dd63..c0b45df8e905 100644 --- a/packages/path_provider/lib/path_provider.dart +++ b/packages/path_provider/lib/path_provider.dart @@ -41,8 +41,8 @@ Future getTemporaryDirectory() async { /// /// On Android, this function throws an [UnsupportedError]. Future getApplicationSupportDirectory() async { - if (!Platform.isIOS) - throw UnsupportedError("getApplicationSupportDirectory requires iOS"); + if (!(Platform.isIOS || Platform.isMacOS)) + throw UnsupportedError("getApplicationSupportDirectory requires iOS or macOS"); final String path = await _channel.invokeMethod('getApplicationSupportDirectory'); if (path == null) { @@ -80,6 +80,8 @@ Future getApplicationDocumentsDirectory() async { Future getExternalStorageDirectory() async { if (Platform.isIOS) throw UnsupportedError("Functionality not available on iOS"); + if (Platform.isMacOS) + throw UnsupportedError("Functionality not implemented on macOS"); final String path = await _channel.invokeMethod('getStorageDirectory'); if (path == null) { diff --git a/packages/path_provider/macos/Classes/PathProviderPlugin.swift b/packages/path_provider/macos/Classes/PathProviderPlugin.swift new file mode 100644 index 000000000000..a61571e24d62 --- /dev/null +++ b/packages/path_provider/macos/Classes/PathProviderPlugin.swift @@ -0,0 +1,51 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import FlutterMacOS +import Foundation + +public class PathProviderPlugin: NSObject, FlutterPlugin { + public static func register(with registrar: FlutterPluginRegistrar) { + let channel = FlutterMethodChannel( + name: "plugins.flutter.io/path_provider", + binaryMessenger: registrar.messenger) + let instance = PathProviderPlugin() + registrar.addMethodCallDelegate(instance, channel: channel) + } + + public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + let method = call.method + if method == "getTemporaryDirectory" { + result(getDirectory(ofType: FileManager.SearchPathDirectory.cachesDirectory)) + } else if method == "getApplicationDocumentsDirectory" { + result(getDirectory(ofType: FileManager.SearchPathDirectory.documentDirectory)) + } else if (method == "getApplicationSupportDirectory") { + var path = getDirectory(ofType: FileManager.SearchPathDirectory.applicationSupportDirectory) + if let basePath = path { + let basePathURL = URL.init(fileURLWithPath: basePath) + path = basePathURL.appendingPathComponent(Bundle.main.bundleIdentifier!).path + do { + try FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: true) + } catch { + result(FlutterError( + code:"directory_creation_failure", + message: error.localizedDescription, + details: "\(error)")) + return + } + } + result(path) + } else { + result(FlutterMethodNotImplemented) + } + } +} + +private func getDirectory(ofType directory: FileManager.SearchPathDirectory) -> String? { + let paths = NSSearchPathForDirectoriesInDomains( + directory, + FileManager.SearchPathDomainMask.userDomainMask, + true) + return paths.first +} diff --git a/packages/path_provider/macos/path_provider.podspec b/packages/path_provider/macos/path_provider.podspec new file mode 100644 index 000000000000..32f0d1da9bee --- /dev/null +++ b/packages/path_provider/macos/path_provider.podspec @@ -0,0 +1,21 @@ +# +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html +# +Pod::Spec.new do |s| + s.name = 'path_provider' + s.version = '0.0.1' + s.summary = 'A Flutter plugin for getting commonly used locations on the filesystem.' + s.description = <<-DESC +A Flutter plugin for getting commonly used locations on the filesystem. + DESC + s.homepage = 'https://github.com/flutter/plugins/tree/master/packages/path_provider' + s.license = { :file => '../LICENSE' } + s.author = { 'Flutter Team' => 'flutter-dev@googlegroups.com' } + s.source = { :path => '.' } + s.source_files = 'Classes/**/*' + s.dependency 'FlutterMacOS' + + s.platform = :osx + s.osx.deployment_target = '10.12' +end + diff --git a/packages/path_provider/pubspec.yaml b/packages/path_provider/pubspec.yaml index 62bab1030fac..fdf179bf615a 100644 --- a/packages/path_provider/pubspec.yaml +++ b/packages/path_provider/pubspec.yaml @@ -9,6 +9,7 @@ flutter: plugin: androidPackage: io.flutter.plugins.pathprovider iosPrefix: FLT + macosPrefix: FLT pluginClass: PathProviderPlugin dependencies: