Flutter - IOS'a Özel Kod Yazma

İOS'a özel koda erişim, iOS'a özgü dilleri kullanması dışında Android platformundakine benzer - Objective-C veya Swift ve iOS SDK. Aksi takdirde, konsept Android platformunun konseptiyle aynıdır.

İOS platformu için de bir önceki bölümde olduğu gibi aynı uygulamayı yazalım.

  • Android Studio (macOS) flutter_browser_ios_app'da yeni bir uygulama oluşturalım

  • Önceki bölümde olduğu gibi 2-6. Adımları izleyin.

  • XCode'u başlatın ve tıklayın File → Open

  • Flutter projemizin ios dizini altındaki xcode projesini seçin.

  • AppDelegate.m dosyasını açın Runner → Runner path. Aşağıdaki kodu içerir -

#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
  • Tarayıcıyı belirtilen url ile açmak için openBrowser adlı bir yöntem ekledik. Tek bağımsız değişken olan url'yi kabul eder.

- (void)openBrowser:(NSString *)urlString { 
   NSURL *url = [NSURL URLWithString:urlString]; 
   UIApplication *application = [UIApplication sharedApplication]; 
   [application openURL:url]; 
}
  • DidFinishLaunchingWithOptions yönteminde denetleyiciyi bulun ve denetleyici değişkenine ayarlayın.

FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
  • DidFinishLaunchingWithOptions yönteminde, tarayıcı kanalını flutterapp.tutorialspoint.com/browse olarak ayarlayın -

FlutterMethodChannel* browserChannel = [
   FlutterMethodChannel methodChannelWithName:
   @"flutterapp.tutorialspoint.com/browser" binaryMessenger:controller];
  • Bir değişken oluşturun, zayıf Kendini ve mevcut sınıfı ayarlayın -

__weak typeof(self) weakSelf = self;
  • Şimdi setMethodCallHandler'ı uygulayın. Call.method ile eşleştirerek openBrowser'ı çağırın. Call.arguments'ı çağırarak url alın ve openBrowser'ı çağırırken iletin.

[browserChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
   if ([@"openBrowser" isEqualToString:call.method]) { 
      NSString *url = call.arguments[@"url"];   
      [weakSelf openBrowser:url]; 
   } else { result(FlutterMethodNotImplemented); } 
}];
  • Kodun tamamı aşağıdaki gibidir -

#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
  • Proje ayarını açın.

  • Git Capabilities ve etkinleştir Background Modes.

  • Ekle *Background fetch ve Remote Notification**.

  • Şimdi uygulamayı çalıştırın. Android sürümüne benzer şekilde çalışır ancak Chrome yerine Safari tarayıcısı açılacaktır.