首页 > 其他分享 >无涯教程-Flutter - 编写IOS代码

无涯教程-Flutter - 编写IOS代码

时间:2023-09-01 18:32:48浏览次数:43  
标签:url self IOS 无涯 UIApplication application call openBrowser Flutter

访问iOS特定代码与Android平台上的代码相似,不同之处在于它使用iOS特定语言-Objective-C或Swift和iOS SDK。

下表显示了如何在Android和iOS平台上接收Dart值。

dart android ios
null null 无(嵌套时为NSNull)
bool java.lang.Boolean NSNumber numberWithBool:
int java.lang.Integer NSNumber numberWithInt:
double java.lang.Double NSNumber numberWithDouble:
String java.lang.String NSString:
Uint8List byte[] FlutterStandardTypedData typedDataWithBytes:
Int32List int[] FlutterStandardTypedData typedDataWithInt32:
Int64List long[] FlutterStandardTypedData typedDataWithInt64:
Float64List double[] FlutterStandardTypedData typedDataWithFloat64:
List java.util.ArrayList NSArray
Map java.util.HashMap NSDictionary

让无涯教程也为iOS平台编写与上一章相同的应用程序。

  • 让无涯教程在Android Studio(macOS)中创建一个新应用, flutter_browser_ios_app

  • 启动XCode并单击 File→Open

  • 在flutter项目的ios目录下选择xcode项目。

  • 在 Runner→Runner path 下打开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.learnfk.com/browse-

FlutterMethodChannel* browserChannel = [
   FlutterMethodChannel methodChannelWithName:
   @"flutterapp.learnfk.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.learnfk.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); } 
   }]; 
   //自定义代码结束
   [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。

参考链接

https://www.learnfk.com/flutter/flutter-writing-ios-specific-code.html

标签:url,self,IOS,无涯,UIApplication,application,call,openBrowser,Flutter
From: https://blog.51cto.com/u_14033984/7325349

相关文章

  • 无涯教程-Flutter - Dart简介
    Dart是一种开源通用编程语言,它最初是由Google开发的,Dart是一种具有C样式语法的面向对象的语言,它支持诸如接口,类之类的编程概念,与其他编程语言不同,Dart不支持数组,Dart集合可用于复制数据结构,例如数组,泛型和可选类型。以下代码显示了一个简单的Dart程序-voidmain(){print......
  • 没有苹果开发者账号能否创建ios证书-最新
    ​ 摘要:本文介绍了在没有Mac电脑的情况下,使用appuploader工具生成iOS证书和描述文件的方法。随着大前端和H5框架的热门话题,越来越多的人希望将H5应用打包成iOS应用。苹果官方提供的证书申请方法需要Mac电脑,因此本文介绍了使用appuploader工具来生成证书的方便方法。   ......
  • 无涯教程-Flutter - 创建程序
    在本章中,让无涯教程创建一个简单的Flutter应用程序,以了解在AndroidStudio中创建Flutter应用程序的基础。第1步-打开AndroidStudio第2步-创建Flutter项目。为此,请单击File→New→NewFlutterProject第3步-选择FlutterApplication。为此,选择FlutterApplication,然......
  • 无涯教程-Flutter - 安装步骤
    本章将指导您详细在本地计算机上安装Flutter。在Windows中安装在本节中,让无涯教程看看如何在Windows系统中安装FlutterSDK及其要求。第1步-转到URL,https://flutter.dev/docs/get-started/install/windows并下载最新的FlutterSDK。第2步-将zip归档文件解压缩到一个......
  • 无涯教程-Android Online Test函数
    Android在线测试模拟了真正的在线认证考试。您将看到基于Android概念的多项选择题(MCQ),将为您提供四个options。您将为该问题选择最合适的答案,然后继续进行下一个问题,而不会浪费时间。完成完整的考试后,您将获得在线考试分数。总问题数-20最长时间-20分钟StartTest......
  • 无涯教程-Android Online Quiz函数
    以下测验提供与Android相关的多项选择题(MCQ)。您将必须阅读所有给定的答案,然后单击正确的答案。如果您不确定答案,则可以使用显示答案按钮检查答案。您可以使用下一个测验按钮检查测验中的新问题集。Q1-android中的PendingIntent是什么?A-这是一种意图B-用于在活动......
  • Flash开发iOS应用全攻略(五)——如何上传应用到iTunes Connect
    上传IPA到[iTunesConnect](https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/wo/96.0.9.3.3.2.1.1.3.1.1)上一篇我介绍了[如何在iTunesConnect里准备应用](http://www.adobe.com/cn/devnet/flash/articles/ios_tutorial_4_itunes.html)。最后在这篇文章里我会简单......
  • IOS开发之免费证书+不越狱真机调试
    苹果发布Xcode7之后,可以打开正常的AppleID或实机上载,而不是$99或$299,只要你可以在AppStore下载应用程序的AppleID。关于Mac系统和Xcode的安装,如果不请参见原文描述,这里只介绍使用Xcode7和普通AppID创建免费证书、个人资料。文中测试环境是OSX10.10.5+XCode7正式版本。因此,一......
  • 2022年iOS上架及证书最新申请流程
    最近的15年,手机行业无论怎么变,ios系统依然还是占据着行业的榜首位置,而打包一个苹果的app,门槛则比较高。主要的原因在于苹果app的开发,打包时需要p12格式的证书文件和描述文件profile文件(在hbuilder和apicloud这些h5打包平台,ios证书又叫私钥证书。),而这些文件的创建则又需要苹果mac电......
  • App Store上架iOS App的详细流程
    最近负责将公司某个项目的iOS应用上架到AppStore,在上架的过程中,需要做的事情很多,但很多都不涉及到太多的技术问题,但是流程很多,而且对应用有很多要求,在上架的过程中也遇到一些问题,后也顺利解决问题,成功上架了。我觉得有必要记录一下过程及遇到的问题,希望对有需要上架的iOS开发者们......