{"record":{"id":"8c9938260efefdfd","repo":"tauri-apps/tauri","slug":"invalid-channel-definition-from-channeldef","errorCode":null,"errorMessage":"Invalid channel definition from \\(channelDef)","messagePattern":"Invalid channel definition from \\\\\\(channelDef\\)","errorType":"exception","errorClass":"DecodingError","httpStatus":null,"severity":"error","filePath":"crates/tauri/mobile/ios-api/Sources/Tauri/Channel.swift","lineNumber":20,"sourceCode":"// SPDX-License-Identifier: Apache-2.0\n// SPDX-License-Identifier: MIT\n\nimport Foundation\n\nlet CHANNEL_PREFIX = \"__CHANNEL__:\"\nlet channelDataKey = CodingUserInfoKey(rawValue: \"sendChannelData\")!\n\npublic class Channel: Decodable {\n  public let id: UInt64\n  let handler: (UInt64, String) -> Void\n\n  public required init(from decoder: Decoder) throws {\n    let container = try decoder.singleValueContainer()\n    let channelDef = try container.decode(String.self)\n\n    let components = channelDef.components(separatedBy: CHANNEL_PREFIX)\n    if components.count < 2 {\n      throw DecodingError.dataCorruptedError(\n        in: container,\n        debugDescription: \"Invalid channel definition from \\(channelDef)\"\n      )\n\n    }\n    guard let channelId = UInt64(components[1]) else {\n      throw DecodingError.dataCorruptedError(\n        in: container,\n        debugDescription: \"Invalid channel ID from \\(channelDef)\"\n      )\n    }\n\n    guard let handler = decoder.userInfo[channelDataKey] as? (UInt64, String) -> Void else {\n      throw DecodingError.dataCorruptedError(\n        in: container,\n        debugDescription: \"missing userInfo for Channel handler. This is a Tauri issue\"\n      )\n    }","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/tauri-apps/tauri/blob/460ec35447493200d64290dd7f015d5a91d0fd58/crates/tauri/mobile/ios-api/Sources/Tauri/Channel.swift#L2-L38","documentation":"The iOS Swift `Channel` type decodes a channel definition string of the form `<CHANNEL_PREFIX><id>` (e.g. `__CHANNEL__:<id>`). If the decoded string lacks the prefix separator or has no ID part, `init(from:)` throws a `DecodingError.dataCorrupted` with this message.","triggerScenarios":"Rust/JS passes a channel identifier string to Swift plugin code that does not match the expected `<prefix><id>` format — empty string, wrong prefix, or only the prefix with no numeric ID.","commonSituations":"Mismatched tauri core and iOS API (Swift Package) versions where the channel prefix format changed; hand-crafted invoke arguments passing raw strings instead of `Channel` objects; channel serialization dropping the ID.","solutions":["Ensure the tauri iOS API Swift package version matches the tauri core version so channel prefixes agree.","Always pass a real `Channel`/`__CHANNEL__` object from JS/Rust rather than an arbitrary string.","Check the invoke payload serialization — the channel definition must include both prefix and numeric ID.","Catch/handle `DecodingError.dataCorrupted` in custom Swift plugin code to log the offending `channelDef`."],"exampleFix":"// before\nplugin.call('watch', { onEvent: '__CHANNEL__' }) // missing id\n// after\nplugin.call('watch', { onEvent: channel }) // real Channel object -> \"__CHANNEL__:<id>\"","handlingStrategy":"type-guard","validationCode":"// Swift: validate channel definition shape before decode\nfunc isValidChannelDef(_ s: String, prefix: String) -> Bool {\n  guard s.hasPrefix(prefix),\n        let id = s.dropFirst(prefix.count).split(separator: \":\").last,\n        UInt64(id) != nil else { return false }\n  return true\n}","typeGuard":"func asValidChannel(_ s: String) -> Channel? {\n  isValidChannelDef(s, prefix: CHANNEL_PREFIX) ? nil : nil // use forced decode in do/catch\n}","tryCatchPattern":"// Swift\ndo {\n  let channel = try JSONDecoder().decode(Channel.self, from: data)\n} catch DecodingError.dataCorrupted(let ctx) {\n  print(\"bad channel definition: \\(ctx.debugDescription)\")\n}","preventionTips":["Keep the tauri iOS Swift package version matched to tauri core","Pass real Channel objects from JS/Rust, never raw strings","Log the offending channelDef when decoding fails to spot version drift"],"tags":["ios","swift","channel","ipc","decoding"],"backgroundTag":"invalid-argument-format","analyzedSha":"460ec35447493200d64290dd7f015d5a91d0fd58","analyzedAt":"2026-09-18T23:55:51.277Z","contentChangedAt":"2026-09-18T23:55:51.277Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}