Flutter – Viết mã cụ thể cho iOS


Truy cập mã cụ thể trên iOS tương tự như trên nền tảng Android ngoại trừ việc nó sử dụng ngôn ngữ cụ thể của iOS – Objective-C hoặc Swift và iOS SDK. Mặt khác, khái niệm này giống với khái niệm của nền tảng Android.

Chúng ta hãy viết ứng dụng tương tự như trong chương trước cho nền tảng iOS.

  • Hãy để chúng tôi tạo một ứng dụng mới trong Android Studio (macOS), Flagship_browser_ios_app

  • Thực hiện theo các bước 2 – 6 như trong chương trước.

  • Khởi động XCode và nhấp vào Tệp → Mở

  • Chọn dự án xcode trong thư mục ios của dự án Flagship của chúng tôi.

  • Mở AppDelegate.m trong Á hậu → Đường chạy. Nó chứa đoạn mã sau:

#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
  • Chúng tôi đã thêm một phương pháp, openBrowser để mở trình duyệt với url được chỉ định. Nó chấp nhận một đối số, url.

- (void)openBrowser:(NSString *)urlString { 
   NSURL *url = [NSURL URLWithString:urlString]; 
   UIApplication *application = [UIApplication sharedApplication]; 
   [application openURL:url]; 
}
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); } 
}];
#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
  • Mở thiết lập dự án.

  • Đi đến Năng lực và kích hoạt Chế độ nền.

  • cộng * Tìm nạp nền và Thông báo từ xa **.

  • Bây giờ, hãy chạy ứng dụng. Nó hoạt động tương tự như phiên bản Android nhưng trình duyệt Safari sẽ được mở thay vì chrome.