惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - ZhangShengjie

苹果Universal Link 配置 pod 错误 iignoring ffi-1.15.4 because its extensions are not built. Try: gem pristine ffi --version 1.15.4 Xcode 模拟器 运行ipa iOS订阅详解 swift高阶函数 Flutter界面跳转 Flutter 使用inspector 调试UI Android Studio 调试flutter项目 Flutter 新建的Project Type类型对比 Mac上好用的数据库 Flutter常见库使用 iOS项目中加入flutter pod init 报错 /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/user_interface/error_report.rb:34:in `force_encoding': can't modify frozen String (FrozenError) HASH与对称加密详解 Mac 安装 Flutter 详解RSA加密原理 insert_dylib 编译没有product class-dump使用报错 Cannot find offset for address 0x88000000010af973 in stringAtAddress: 终端使用ipatool下载Appstore的Ipa文件到电脑
flutter iOS 使用BasicMessageChannel 通信
ZhangShengjie · 2023-12-12 · via 博客园 - ZhangShengjie

flutter代码

// main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final BasicMessageChannel<String> messageChannel =
  BasicMessageChannel<String>('messageChannel', StringCodec());

  String _message = 'No message from iOS';

  @override
  void initState() {
    super.initState();
    messageChannel.setMessageHandler((message) async {
      print("Received message from iOS: $message");
      setState(() {
        _message = 'iOS Response: $message';
      });
      return 'ACK from Dart';
    });
  }

  Future<void> _sendMessageToiOS() async {
    try {
      final String? result = await messageChannel.send('Hello from Flutter!');
      print("Received response from iOS: $result");
    } on PlatformException catch (e) {
      print("Failed to send message to iOS: ${e.message}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter - iOS Communication Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '$_message',
              style: TextStyle(fontSize: 16),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _sendMessageToiOS,
              child: Text('Send Message to iOS'),
            ),
          ],
        ),
      ),
    );
  }
}

iOS代码

// AppDelegate.swift
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let controller = window?.rootViewController as! FlutterViewController
        let messageChannel = FlutterBasicMessageChannel(
            name: "messageChannel",
            binaryMessenger: controller.binaryMessenger,
            codec: FlutterStringCodec.sharedInstance()
        )

        messageChannel.setMessageHandler { [weak self] (message, reply) in
            guard let strongSelf = self else { return }

            print("Received message from Flutter: \(message ?? "")")
            let response = "Hello from iOS! You said: \(message ?? "")"
            reply(response)
        }

        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}