Flutter - Escribir código específico de IOS

El acceso al código específico de iOS es similar al de la plataforma Android, excepto que utiliza lenguajes específicos de iOS: Objective-C o Swift y SDK de iOS. De lo contrario, el concepto es el mismo que el de la plataforma Android.

Escribamos también la misma aplicación que en el capítulo anterior para la plataforma iOS.

  • Creemos una nueva aplicación en Android Studio (macOS), flutter_browser_ios_app

  • Siga los pasos 2 a 6 como en el capítulo anterior.

  • Inicie XCode y haga clic en File → Open

  • Elija el proyecto xcode en el directorio ios de nuestro proyecto flutter.

  • Abra AppDelegate.m debajo Runner → Runner path. Contiene el siguiente código:

#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
  • Hemos agregado un método, openBrowser para abrir el navegador con la URL especificada. Acepta un solo argumento, url.

- (void)openBrowser:(NSString *)urlString { 
   NSURL *url = [NSURL URLWithString:urlString]; 
   UIApplication *application = [UIApplication sharedApplication]; 
   [application openURL:url]; 
}
  • En el método didFinishLaunchingWithOptions, busque el controlador y configúrelo en la variable del controlador.

FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
  • En el método didFinishLaunchingWithOptions, configure el canal del navegador como flutterapp.tutorialspoint.com/browse -

FlutterMethodChannel* browserChannel = [
   FlutterMethodChannel methodChannelWithName:
   @"flutterapp.tutorialspoint.com/browser" binaryMessenger:controller];
  • Cree una variable, débilSelf y establezca la clase actual -

__weak typeof(self) weakSelf = self;
  • Ahora, implemente setMethodCallHandler. Llame a openBrowser haciendo coincidir call.method. Obtén la URL invocando call.arguments y pásala mientras llamas a openBrowser.

[browserChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
   if ([@"openBrowser" isEqualToString:call.method]) { 
      NSString *url = call.arguments[@"url"];   
      [weakSelf openBrowser:url]; 
   } else { result(FlutterMethodNotImplemented); } 
}];
  • El código completo es el siguiente:

#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
  • Configuración de proyecto abierto.

  • Ir Capabilities y habilitar Background Modes.

  • Añadir *Background fetch y Remote Notification**.

  • Ahora, ejecute la aplicación. Funciona de manera similar a la versión de Android, pero se abrirá el navegador Safari en lugar de Chrome.