- 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浏览器。
广告