Flutter-IOS固有のコードの記述
iOS固有のコードへのアクセスは、iOS固有の言語(Objective-CまたはSwiftとiOS SDK)を使用することを除いて、Androidプラットフォームのコードと似ています。それ以外の点では、コンセプトはAndroidプラットフォームのコンセプトと同じです。
iOSプラットフォームについても前章と同じアプリケーションを書いてみましょう。
Android Studio(macOS)で新しいアプリケーションflutter_browser_ios_appを作成しましょう
前の章と同様に、手順2〜6に従います。
XCodeを起動し、をクリックします File → Open
flutterプロジェクトのiosディレクトリの下にあるxcodeプロジェクトを選択します。
下のAppDelegate.mを開きます Runner → Runner path。次のコードが含まれています-
#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 
    指定されたURLでブラウザを開くためのメソッドopenBrowserを追加しました。単一の引数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 
    プロジェクト設定を開きます。
に移動 Capabilities 有効にします Background Modes。
追加 *Background fetch そして Remote Notification**。
次に、アプリケーションを実行します。Androidバージョンと同様に機能しますが、Chromeの代わりにSafariブラウザが開きます。