write method

  1. @experimental
Uint8List? write({
  1. String? certificatePem,
  2. String? privateKeyPem,
})

Creates a PkPass file. If this instance was created via PkPass.fromBytes it overwrites the signature if possible.

When written to disk, the file should have an ending of .pkpass.

certificatePem is the certificate to be used to sign the PkPass file.

privateKeyPem is the private key PEM file. Right now, it's only supported if it's not password protected.

Read more about signing here.

If either certificatePem or privateKeyPem is null, the resulting PkPass will not be properly signed, but still generated.

Remarks:

  • There's no support for verifying that the signature matches the PkPass
  • There's no support for localization
  • There's no support for personalization
  • Image sizes aren't checked, which means it's possible to create passes that look odd and wrong in Apple wallet or passkit_ui

Implementation

@experimental
Uint8List? write({
  String? certificatePem,
  String? privateKeyPem,
}) {
  final archive = Archive();

  final passContent = utf8JsonEncode(pass.toJson());
  final passFile = ArchiveFile(
    'pass.json',
    passContent.length,
    passContent,
  );
  archive.addFile(passFile);

  if (personalization != null) {
    final personalizationContent = utf8JsonEncode(personalization!.toJson());
    final personalizationFile = ArchiveFile(
      'personalization.json',
      personalizationContent.length,
      personalizationContent,
    );
    archive.addFile(personalizationFile);
  }

  logo?.writeToArchive(archive, 'logo');
  background?.writeToArchive(archive, 'background');
  icon?.writeToArchive(archive, 'icon');
  footer?.writeToArchive(archive, 'footer');
  strip?.writeToArchive(archive, 'strip');
  thumbnail?.writeToArchive(archive, 'thumbnail');
  personalizationLogo?.writeToArchive(archive, 'personalizationLogo');

  final translationEntries = languageData?.entries;
  if (translationEntries != null && translationEntries.isNotEmpty) {
    // TODO(any): Ensure every translation file has the same amount of key value pairs.

    for (final entry in translationEntries) {
      final name = '${entry.key}.lproj/pass.strings';
      final localizationFile =
          ArchiveFile.string(name, toStringsFile(entry.value));
      archive.addFile(localizationFile);
    }
  }

  final manifestFile = archive.createManifest();

  if (certificatePem != null && privateKeyPem != null) {
    final signature = writeSignature(
      certificatePem,
      privateKeyPem,
      manifestFile,
      pass.passTypeIdentifier,
      pass.teamIdentifier,
      true,
    );

    final signatureFile = ArchiveFile(
      'signature',
      signature.length,
      signature,
    );
    archive.addFile(signatureFile);
  }

  final pkpass = ZipEncoder().encode(archive);
  return pkpass == null ? null : Uint8List.fromList(pkpass);
}