Flutter - 编写 IOS 特定代码


访问 iOS 特定代码与 Android 平台上的类似,只是它使用 iOS 特定语言 - Objective-C 或 Swift 和 iOS SDK。除此之外,概念与Android平台相同。

让我们也为 iOS 平台编写与前一章相同的应用程序。

  • 让我们在 Android Studio (macOS) 中创建一个新应用程序flutter_browser_ios_app

  • 按照前一章中的步骤 2 - 6 进行操作。

  • 启动 XCode 并单击“文件”→“打开”

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

  • 在Runner → Runner 路径下打开 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.tutorialspoint.com/browse -

FlutterMethodChannel* browserChannel = [
   FlutterMethodChannel methodChannelWithName:
   @"flutterapp.tutorialspoint.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.tutorialspoint.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); } 
   }]; 
   // custom code ends 
   [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。