tree: 0d5ad2de7a99ab1607ec48617f93619a3482495a [path history] [tgz]
  1. example/
  2. img/
  3. ios/
  4. lib/
  5. macos/
  6. src/
  7. .gitattributes
  8. .gitignore
  9. .metadata
  10. analysis_options.yaml
  11. CHANGELOG.md
  12. cupertino_http.iml
  13. DEVELOPMENT.md
  14. ffigen.yaml
  15. LICENSE
  16. pubspec.yaml
  17. README.md
pkgs/cupertino_http/README.md

pub package package publisher

A macOS/iOS Flutter plugin that provides access to the Foundation URL Loading System.

Status: Experimental

NOTE: This package is currently experimental and published under the labs.dart.dev pub publisher in order to solicit feedback.

For packages in the labs.dart.dev publisher we generally plan to either graduate the package into a supported publisher (dart.dev, tools.dart.dev) after a period of feedback and iteration, or discontinue the package. These packages have a much higher expected rate of API and breaking changes.

Your feedback is valuable and will help us evolve this package. For general feedback and suggestions please comment in the feedback issue. For bugs, please file an issue in the bug tracker.

Motivation

Using the Foundation URL Loading System, rather than the socket-based dart:io HttpClient implemententation, has several advantages:

  1. It automatically supports iOS/macOS platform features such VPNs and HTTP proxies.
  2. It supports many more configuration options such as only allowing access through WiFi and blocking cookies.
  3. It supports more HTTP features such as HTTP/3 and custom redirect handling.

Using

The easiest way to use this library is via the the high-level interface defined by package:http Client.

This approach allows the same HTTP code to be used on all platforms, while still allowing platform-specific setup.

late Client client;
if (Platform.isIOS) {
  final config = URLSessionConfiguration.ephemeralSessionConfiguration()
    ..allowsCellularAccess = false
    ..allowsConstrainedNetworkAccess = false
    ..allowsExpensiveNetworkAccess = false;
  client = CupertinoClient.fromSessionConfiguration(config);
} else {
  client = IOClient(); // Uses an HTTP client based on dart:io
}

final response = await client.get(Uri.https(
    'www.googleapis.com',
    '/books/v1/volumes',
    {'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));

package:http runWithClient can be used to configure the package:http Client for the entire application.

void main() {
  late Client client;
  if (Platform.isIOS) {
    client = CupertinoClient.defaultSessionConfiguration();
  } else {
    client = IOClient();
  }

  runWithClient(() => runApp(const MyApp()), () => client);
}

...

class MainPageState extends State<MainPage> {
  void someMethod() {
    // Will use the Client configured in main.
    final response = await get(Uri.https(
        'www.googleapis.com',
        '/books/v1/volumes',
        {'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
  }
  ...
}

You can also use the Foundation URL Loading System API directly.

final url = Uri.https(
    'www.googleapis.com',
    '/books/v1/volumes',
    {'q': 'HTTP', 'maxResults': '40', 'printType': 'books'});
final session = URLSession.sharedSession();
final task = session.dataTaskWithCompletionHandler(URLRequest.fromUrl(url),
    (data, response, error) {
  if (error == null && response!.statusCode == 200) {
    print(data!.bytes);
  }
});
task.resume();