教程 > flutter 教程 > 阅读:56

flutter 编写 ios 代码——迹忆客-ag捕鱼王app官网

访问 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 方法中,找到控制器并将其设置在控制器变量中。

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

打开项目设置。

转到功能并启用背景模式。

添加*background fetchremote notification**

现在,运行应用程序。它的工作方式类似于 android 版本,但将打开 safari 浏览器而不是 chrome。

查看笔记

扫码一下
查看教程更方便
网站地图