- Flutter 教程
- Flutter - 首頁
- Flutter - 簡介
- Flutter - 安裝
- 在Android Studio中建立簡單應用程式
- Flutter - 架構應用程式
- Dart程式設計入門
- Flutter - Widget入門
- Flutter - 佈局入門
- Flutter - 手勢入門
- Flutter - 狀態管理
- Flutter - 動畫
- Flutter - 編寫Android特定程式碼
- Flutter - 編寫iOS特定程式碼
- Flutter - 包入門
- Flutter - 訪問REST API
- Flutter - 資料庫概念
- Flutter - 國際化
- Flutter - 測試
- Flutter - 部署
- Flutter - 開發工具
- Flutter - 編寫高階應用程式
- Flutter - 結論
- Flutter 有用資源
- Flutter - 快速指南
- Flutter - 有用資源
- Flutter - 討論
Flutter - 編寫iOS特定程式碼
訪問iOS特定程式碼與Android平臺類似,只是它使用iOS特定的語言 - Objective-C或Swift和iOS SDK。否則,概念與Android平臺相同。
讓我們為iOS平臺編寫與上一章相同的應用程式。
讓我們在Android Studio(macOS)中建立一個新的應用程式,flutter_browser_ios_app
按照上一章中的步驟2-6操作。
啟動XCode並點選檔案→開啟
選擇Flutter專案ios目錄下的xcode專案。
開啟Runner → Runner路徑下的AppDelegate.m。它包含以下程式碼:
#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方法中,找到控制器並在controller變數中設定它。
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
在didFinishLaunchingWithOptions方法中,將瀏覽器通道設定為flutterapp.tutorialspoint.com/browse:
FlutterMethodChannel* browserChannel = [ FlutterMethodChannel methodChannelWithName: @"flutterapp.tutorialspoint.com/browser" binaryMessenger:controller];
建立一個變數weakSelf並設定當前類:
__weak typeof(self) weakSelf = self;
現在,實現setMethodCallHandler。透過匹配call.method呼叫openBrowser。透過呼叫call.arguments獲取url,並在呼叫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
開啟專案設定。
轉到功能並啟用後臺模式。
新增*後臺獲取和遠端通知**。
現在,執行應用程式。它的工作方式類似於Android版本,但將開啟Safari瀏覽器而不是Chrome瀏覽器。
廣告