創(chuàng)建iOS項(xiàng)目
這里我們先創(chuàng)建一個(gè)空的iOS項(xiàng)目來(lái)模擬已有的項(xiàng)目柠逞,取名叫iOS_demo
創(chuàng)建flutter模塊
進(jìn)入你的項(xiàng)目同一層目錄,創(chuàng)建flutter模塊
flutter create -t module flutter_lab
文件目錄.png
關(guān)閉Bitcode
Flutter混合開(kāi)發(fā)還不支持Bitcode,所以在iOS工程檢查項(xiàng)目并關(guān)閉Bitcode
Bitcode.png
iOS項(xiàng)目相關(guān)配置
image.png
Flutter.xcconfig 內(nèi)容
#include "../flutter_lab/.ios/Flutter/Generated.xcconfig"
ENABLE_BITCODE=NO
Debug.xcconfig 內(nèi)容 (對(duì)應(yīng)的名字換成自己)
#include "Flutter.xcconfig"
// 如果使用了Cocoapods聂受,那么需要引入 cocoapods 的config文件饥悴,因?yàn)槿绻远x了config坦喘,那么cocoapods 的 config 就不會(huì)自動(dòng)指定了盲再。
#include "Pods/Target Support Files/Pods-iOS_demo/Pods-iOS_demo.debug.xcconfig"
Release.xcconfig
#include "Flutter.xcconfig"
FLUTTER_BUILD_MODE=release
// 如果使用了Cocoapods,那么需要引入 cocoapods 的config文件瓣铣,因?yàn)槿绻远x了config答朋,那么cocoapods 的 config 就不會(huì)自動(dòng)指定了。
#include "Pods/Target Support Files/Pods-iOS_demo/Pods-iOS_demo.release.xcconfig"
指定 config 文件棠笑,Debug 對(duì)應(yīng) Debug梦碗,Release 對(duì)應(yīng) Release
image.png
增加 Flutter 的腳本
image.png
image.png
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
修改Flutter腳本
在下載的flutter SDK中找到xcode_backend文件
image.png
注釋這段腳本
image.png
編譯iOS項(xiàng)目,在項(xiàng)目目錄下會(huì)生成一個(gè)flutter文件夾
image.png
右鍵項(xiàng)目 - Add Files to 'xxx' 【Options先選Create groups】蓖救,選擇Flutter目錄
image.png
但是flutter_assets 并不能使用Create groups 的方式添加洪规,只能使用Creat folder references 的方式添加進(jìn)Xcode項(xiàng)目?jī)?nèi),否則跳轉(zhuǎn)flutter會(huì)頁(yè)面渲染失斞唷(頁(yè)面空白)斩例。
image.png
文件夾再Add Files to 'xxx',選擇Creat folder references 巨柒;最終如下圖
image.png
image.png
iOS項(xiàng)目改造
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate <UIApplicationDelegate, FlutterAppLifeCycleProvider>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
{
FlutterPluginAppLifeCycleDelegate *_lifeCycleDelegate;
}
- (instancetype)init {
if (self = [super init]) {
_lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
}
return self;
}
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
[_lifeCycleDelegate applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication*)application {
[_lifeCycleDelegate applicationWillEnterForeground:application];
}
- (void)applicationWillResignActive:(UIApplication*)application {
[_lifeCycleDelegate applicationWillResignActive:application];
}
- (void)applicationDidBecomeActive:(UIApplication*)application {
[_lifeCycleDelegate applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication*)application {
[_lifeCycleDelegate applicationWillTerminate:application];
}
- (void)application:(UIApplication*)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {
[_lifeCycleDelegate application:application
didRegisterUserNotificationSettings:notificationSettings];
}
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
[_lifeCycleDelegate application:application
didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication*)application
didReceiveRemoteNotification:(NSDictionary*)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
[_lifeCycleDelegate application:application
didReceiveRemoteNotification:userInfo
fetchCompletionHandler:completionHandler];
}
- (BOOL)application:(UIApplication*)application
openURL:(NSURL*)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
return [_lifeCycleDelegate application:application openURL:url options:options];
}
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
return [_lifeCycleDelegate application:application handleOpenURL:url];
}
- (BOOL)application:(UIApplication*)application
openURL:(NSURL*)url
sourceApplication:(NSString*)sourceApplication
annotation:(id)annotation {
return [_lifeCycleDelegate application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
- (void)application:(UIApplication*)application
performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
completionHandler:(void (^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) {
[_lifeCycleDelegate application:application
performActionForShortcutItem:shortcutItem
completionHandler:completionHandler];
}
- (void)application:(UIApplication*)application
handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
completionHandler:(nonnull void (^)(void))completionHandler {
[_lifeCycleDelegate application:application
handleEventsForBackgroundURLSession:identifier
completionHandler:completionHandler];
}
- (void)application:(UIApplication*)application
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
[_lifeCycleDelegate application:application performFetchWithCompletionHandler:completionHandler];
}
- (void)addApplicationLifeCycleDelegate:(NSObject<FlutterPlugin>*)delegate {
[_lifeCycleDelegate addDelegate:delegate];
}
#pragma mark - Flutter
// Returns the key window's rootViewController, if it's a FlutterViewController.
// Otherwise, returns nil.
- (FlutterViewController*)rootFlutterViewController {
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([viewController isKindOfClass:[FlutterViewController class]]) {
return (FlutterViewController*)viewController;
}
return nil;
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
// Pass status bar taps to key window Flutter rootViewController.
if (self.rootFlutterViewController != nil) {
[self.rootFlutterViewController handleStatusBarTouche
測(cè)試
#import "ViewController.h"
#import <Flutter/FlutterViewController.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
flutterViewController.navigationItem.title = @"Flutter";
[self presentViewController:flutterViewController animated:YES completion:nil];
}
@end