Flutter开发 for iOS

2020-07-21  本文已影响0人  朝雨晚风

(一)、mac 配置Flutter环境

1、获取 Flutter SDK

注:此地址为清华大学开源软件镜像站的下载地址清华大学开源软件镜像站

2、将文件解压放到目标路径,比如:文稿路径下/Users/leiou/Documents

3、终端输入open ~/.bash_profile 然后编辑内容如下:没有就touch该文件

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=/Users/leiou/Documents/flutter/bin:$PATH

4、运行 source ~/.bash_profile 刷新当前终端窗口.

source ~/.bash_profile 

6、运行 flutter doctor 命令,这个命令来查看当前环境是否需要安装其他的依赖(如果想查看更详细的输出,增加一个 -v 参数即可):

$ flutter doctor
截屏2020-07-20下午8.39.06.png

(二)、cocoapod方式 集成 Flutter

1、将flutter项目创建在iOS项目同目录中,先cd 到你要混编的项目路径下 ,执行如下:
注意:此处创建的是flutter module,不是flutter project

panyuqing@leiou iOS_Demo % pwd
/Users/leiou/Documents/Mix/iOS_Demo
panyuqing@leiou iOS_Demo % flutter create -t module flutter_lab
Creating project flutter_lab...
Running "flutter pub get" in flutter_tools...                       5.4s
  flutter_lab/test/widget_test.dart (created)
  flutter_lab/flutter_lab.iml (created)
  flutter_lab/.gitignore (created)
  flutter_lab/.metadata (created)
  flutter_lab/pubspec.yaml (created)
  flutter_lab/README.md (created)
  flutter_lab/lib/main.dart (created)
  flutter_lab/flutter_lab_android.iml (created)
  flutter_lab/.idea/libraries/Flutter_for_Android.xml (created)
  flutter_lab/.idea/libraries/Dart_SDK.xml (created)
  flutter_lab/.idea/modules.xml (created)
  flutter_lab/.idea/workspace.xml (created)
Running "flutter pub get" in flutter_lab...                         2.1s
Wrote 12 files.

All done!
Your module code is in flutter_lab/lib/main.dart.
1.png

2、在podfile文件中加入以下脚本,然后pod install

platform :ios, '9.0'
#注意路径和文件夹名字正确无误 最后有一个反斜杠
flutter_application_path = '/Users/leiou/Documents/Mix/flutter_lab/'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')


target 'iOS_Demo' do
pod 'YYKit' , '1.0.9'

 install_all_flutter_pods(flutter_application_path)

end

\color{red}{注意,如下错误:[!] InvalidPodfilefile: No such file or directory @ rb_sysopen - ./flutter_yyframework/.ios/Flutter/podhelper.rb.}

需要在flutter_yyframework文件夹下执行以下命令,把.ios和.android等flutter配置生成出来。

panyuqing@leiou iOS_Demo % pod install                               

[!] Invalid `Podfile` file: cannot load such file -- /Users/leiou/Documents/Mix/flutter_lab/iOS_Demo/Flutter/podhelper.rb.

 #  from /Users/leiou/Documents/Mix/iOS_Demo/Podfile:4
 #  -------------------------------------------
 #  flutter_application_path = '/Users/leiou/Documents/Mix/flutter_lab/'
 >  load File.join(flutter_application_path, 'iOS_Demo', 'Flutter', 'podhelper.rb')
 #  
 #  -------------------------------------------
panyuqing@leiou iOS_Demo % cd /Users/leiou/Documents/Mix/flutter_lab 
panyuqing@leiou flutter_lab % open -a Simulator
panyuqing@leiou flutter_lab % flutter run
Launching lib/main.dart on iPhone 11 in debug mode...
 
Running Xcode build...                                                  
                                                   
 └─Compiling, linking and signing...                        39.6s
Xcode build done.                                           54.4s
Syncing files to device iPhone 11...                               122ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h Repeat this help message.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).
An Observatory debugger and profiler on iPhone 11 is available at: http://127.0.0.1:51763/P5TDvgAG42s=/

Application finished.

shift+command+. 快捷键显示隐藏的.文件夹


2.png

3、编译iOS项目,关闭Enable Bitcode 需要,因为Flutter混合开发不支持Bitcode

3.png

4、改造AppDelegate

#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate

@property (nonatomic,strong) FlutterEngine *flutterEngine;

@end


#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Only if you have Flutter Plugins
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];

    [self.flutterEngine runWithEntrypoint:nil];

    [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}


#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

@end
4.png
上一篇 下一篇

猜你喜欢

热点阅读