Flutter - การเขียนรหัสเฉพาะของ IOS
การเข้าถึงรหัสเฉพาะของ iOS จะเหมือนกับบนแพลตฟอร์ม Android ยกเว้นว่าจะใช้ภาษาเฉพาะของ iOS - Objective-C หรือ Swift และ iOS SDK มิฉะนั้นแนวคิดจะเหมือนกับของแพลตฟอร์ม Android
ให้เราเขียนแอปพลิเคชันเดียวกันกับในบทก่อนหน้าสำหรับแพลตฟอร์ม iOS ด้วย
ให้เราสร้างแอปพลิเคชันใหม่ใน Android Studio (macOS), flutter_browser_ios_app
ทำตามขั้นตอนที่ 2 - 6 ตามบทก่อนหน้า
เริ่ม XCode แล้วคลิก File → Open
เลือกโครงการ xcode ภายใต้ไดเร็กทอรี ios ของโครงการ flutter ของเรา
เปิด 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
เราได้เพิ่มวิธีการ openBrowser เพื่อเปิดเบราว์เซอร์ด้วย url ที่ระบุ ยอมรับอาร์กิวเมนต์เดียว 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];
สร้างตัวแปรจุดอ่อนตัวเองและตั้งค่าคลาสปัจจุบัน -
__weak typeof(self) weakSelf = self;
ตอนนี้ใช้ setMethodCallHandler เรียก openBrowser โดยการจับคู่ call.method รับ url โดยเรียกใช้ call.arguments และส่งผ่านขณะเรียก 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 แต่จะเปิดเบราว์เซอร์ Safari แทน Chrome