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方法中,找到控制器並在controller變數中設定它。

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瀏覽器。

廣告

© . All rights reserved.