Flutter - Écriture de code spécifique à IOS

L'accès au code spécifique à iOS est similaire à celui de la plate-forme Android, sauf qu'il utilise des langages spécifiques à iOS - Objective-C ou Swift et iOS SDK. Sinon, le concept est le même que celui de la plateforme Android.

Écrivons également la même application que dans le chapitre précédent pour la plateforme iOS.

  • Créons une nouvelle application dans Android Studio (macOS), flutter_browser_ios_app

  • Suivez les étapes 2 à 6 comme dans le chapitre précédent.

  • Démarrez XCode et cliquez sur File → Open

  • Choisissez le projet xcode dans le répertoire ios de notre projet Flutter.

  • Ouvrez AppDelegate.m sous Runner → Runner path. Il contient le code suivant -

#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
  • Nous avons ajouté une méthode, openBrowser pour ouvrir le navigateur avec l'URL spécifiée. Il accepte un seul argument, url.

- (void)openBrowser:(NSString *)urlString { 
   NSURL *url = [NSURL URLWithString:urlString]; 
   UIApplication *application = [UIApplication sharedApplication]; 
   [application openURL:url]; 
}
  • Dans la méthode didFinishLaunchingWithOptions, recherchez le contrôleur et définissez-le dans la variable du contrôleur.

FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
  • Dans la méthode didFinishLaunchingWithOptions, définissez le canal du navigateur sur flutterapp.tutorialspoint.com/browse -

FlutterMethodChannel* browserChannel = [
   FlutterMethodChannel methodChannelWithName:
   @"flutterapp.tutorialspoint.com/browser" binaryMessenger:controller];
  • Créez une variable, lowSelf et définissez la classe courante -

__weak typeof(self) weakSelf = self;
  • Maintenant, implémentez setMethodCallHandler. Appelez openBrowser en faisant correspondre call.method. Obtenez l'URL en appelant call.arguments et transmettez-la en appelant openBrowser.

[browserChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
   if ([@"openBrowser" isEqualToString:call.method]) { 
      NSString *url = call.arguments[@"url"];   
      [weakSelf openBrowser:url]; 
   } else { result(FlutterMethodNotImplemented); } 
}];
  • Le code complet est le suivant -

#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
  • Ouvrir les paramètres du projet.

  • Aller à Capabilities et activer Background Modes.

  • Ajouter *Background fetch et Remote Notification**.

  • Maintenant, exécutez l'application. Cela fonctionne de la même manière que la version Android, mais le navigateur Safari sera ouvert à la place de Chrome.