📅  最后修改于: 2020-12-08 04:44:26             🧑  作者: Mango
访问iOS特定代码与Android平台上的代码相似,不同之处在于它使用iOS特定语言-Objective-C或Swift和iOS SDK。否则,该概念与Android平台的概念相同。
让我们也为iOS平台编写与上一章相同的应用程序。
让我们在Android Studio(macOS)中创建一个新的应用程序flutter_browser_ios_app
按照上一章中的步骤2-6进行操作。
启动XCode,然后单击文件→打开
在我们的flutter项目的ios目录下选择xcode项目。
在“跑步者”→“跑步者路径”下打开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方法中,找到控制器并将其设置在控制器变量中。
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。