Flutter编写IOS代码

/ / Flutter编写IOS代码

访问iOS特定代码与Android平台上的代码相似,不同之处在于它使用iOS特定语言-Objective-C或Swift和iOS SDK。

下表显示了如何在Android和iOS平台上接收Dart值。

dartandroidios
nullnull无(嵌套时为NSNull)
booljava.lang.BooleanNSNumber numberWithBool:
intjava.lang.IntegerNSNumber numberWithInt:
doublejava.lang.DoubleNSNumber numberWithDouble:
Stringjava.lang.StringNSString:
Uint8Listbyte[]FlutterStandardTypedData typedDataWithBytes:
Int32Listint[]FlutterStandardTypedData typedDataWithInt32:
Int64Listlong[]FlutterStandardTypedData typedDataWithInt64:
Float64Listdouble[]FlutterStandardTypedData typedDataWithFloat64:
Listjava.util.ArrayListNSArray
Mapjava.util.HashMapNSDictionary

让无涯教程也为iOS平台编写与上一章相同的应用程序。

  • 让无涯教程在Android Studio(macOS)中创建一个新应用, flutter_browser_ios_app

  • 启动XCode并单击 File→Open

  • 在flutter项目的ios目录下选择xcode项目。

  • 在 Runner→Runner path 下打开AppDelegate.m。它包含以下代码-

#include "AppDelegate.h" 
#include "GeneratedPluginRegistrant.h" 
@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application
   didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      //[GeneratedPluginRegistrant registerWithRegistry:self];
      //Override point for customization after application launch.
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
   } 
@end
  • 无涯教程添加了openBrowser方法来打开具有指定URL的浏览器。它接受单个参数url。

- (void)openBrowser:(NSString *)urlString { 
   NSURL *url = [NSURL URLWithString:urlString]; 
   UIApplication *application = [UIApplication sharedApplication]; 
   [application openURL:url]; 
}
  • 在didFinishLaunchingWithOptions方法中,找到控制器并将其设置在控制器变量中。

FlutterViewController* controller=(FlutterViewController*)self.window.rootViewController;
  • 在didFinishLaunchingWithOptions方法中,将浏览器通道设置为flutterapp.learnfk.com/browse-

FlutterMethodChannel* browserChannel = [
   FlutterMethodChannel methodChannelWithName:
   @"flutterapp.learnfk.com/browser" binaryMessenger:controller];
  • 创建一个变量weakSelf并设置当前类-

__weak typeof(self) weakSelf=self;
  • 现在,实现setMethodCallHandler,通过匹配call.method调用openBrowser,通过调用call.arguments获取URL,并在调用openBrowser时传递它。

[browserChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
   if ([@"openBrowser" isEqualToString:call.method]) { 
      NSString *url = call.arguments[@"url"];   
      [weakSelf openBrowser:url]; 
   } else { result(FlutterMethodNotImplemented); } 
}];
  • 完整的代码如下-

#include "AppDelegate.h" 
#include "GeneratedPluginRegistrant.h" 
@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application 
   didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
   //custom code starts 
   FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController; 
   FlutterMethodChannel* browserChannel = [
      FlutterMethodChannel methodChannelWithName:
      @"flutterapp.learnfk.com/browser" binaryMessenger:controller]; 
   
   __weak typeof(self) weakSelf = self; 
   [browserChannel setMethodCallHandler:^(
      FlutterMethodCall* call, FlutterResult result) { 
      
      if ([@"openBrowser" isEqualToString:call.method]) { 
         NSString *url = call.arguments[@"url"];
         [weakSelf openBrowser:url]; 
      } else { result(FlutterMethodNotImplemented); } 
   }]; 
   //自定义代码结束
   [GeneratedPluginRegistrant registerWithRegistry:self]; 
   
   //Override point for customization after application launch. 
   return [super application:application didFinishLaunchingWithOptions:launchOptions]; 
}
- (void)openBrowser:(NSString *)urlString { 
   NSURL *url = [NSURL URLWithString:urlString]; 
   UIApplication *application = [UIApplication sharedApplication]; 
   [application openURL:url]; 
} 
@end

运行应用程序。它的工作方式类似于Android版本,但将打开Safari浏览器,而不是chrome。

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

DeepSeek-R1与Deep Research复现之旅 -〔老刘〕

DeepSeek-R1前沿入门课 -〔老刘〕

Rust并发编程实战课 -〔晁岳攀(鸟窝)〕

深入浅出可观测性 -〔翁一磊〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

业务开发算法50讲 -〔黄清昊〕

摄影入门课 -〔小麥〕

重学前端 -〔程劭非(winter)〕

AI技术内参 -〔洪亮劼〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者