美文网首页
ios项目嵌套flutter页面的详细步骤

ios项目嵌套flutter页面的详细步骤

作者: xing_x | 来源:发表于2023-05-28 16:58 被阅读0次
    环境准备:flutter sdk
    cocopods version > 1.10

    创建一个文件夹:Demo
    1.cd Ddemo 之后使用flutter创建flutter工程

    flutter create --template module myflutter
    

    2.相同目录下创建一个ios项目,示例工程SBDemo
    然后按照正常创建podfile的方式创建一个podfile文件

    3.在podfile文件加入flutter工程的路径,因为我们采用的是cocoapods的引入方式,需要导入本地路径

    flutter_application_path = '../my_flutter'
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    

    4.除了加入本地路径,还需要引入,完整的podfile文件

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '13.0'
    use_frameworks!
    flutter_application_path = '../my_flutter'
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    target 'SBDemo' do
        pod 'SVProgressHUD'
        pod 'CTMediator'
      install_all_flutter_pods(flutter_application_path)
      post_install do |installer|
        flutter_post_install(installer) if defined?(flutter_post_install)
      end
    end
    

    5.正常的

    pod install
    

    ,如果控制台显示:


    WechatIMG34783.png

    则是pod成功了。
    6.先运行了一下xcode,看是否运行成功,成功的话就ok,失败就解决问题。完整的pod文件工程,可供参考:


    WechatIMG23.png

    7.最重要的就是flutter加载,在Appdelegate中引入

    #import <UIKit/UIKit.h>
    #import <Flutter/Flutter.h>
    #import <FlutterPluginRegistrant/FlutterPluginRegistrant-umbrella.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate,FlutterAppLifeCycleProvider>
    
    @property (strong, nonatomic) UIWindow * window;
    
    @property (nonatomic,strong) FlutterEngine *flutterEngine;
    
    @end
    

    Appdelegate.m中

    //
    //  AppDelegate.m
    //  SBDemo
    //
    //  Created by admin on 2022/5/5.
    //
    
    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @property (nonatomic, strong) FlutterPluginAppLifeCycleDelegate* lifeCycleDelegate;
    
    
    @end
    
    
    @implementation AppDelegate
    
    @synthesize window = _window;
    
    - (instancetype)init {
        if (self = [super init]) {
            _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
        }
        return self;
    }
    
    - (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 [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
    }
    
    // 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;
    }
    #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.
    }
    - (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 {
        [_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];
    }
    
    @end
    

    8.调用方式有三种

    - (IBAction)jumpFlutter:(id)sender {
        NSLog(@"跳转默认的fluter页面");
        FlutterViewController *flutterViewController =
             [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
        flutterViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:flutterViewController animated:YES completion:nil];
    }
    - (IBAction)jumpJTFunction:(id)sender {
        NSLog(@"跳转指定函数的fluter页面");
        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
        [appDelegate.flutterEngine runWithEntrypoint:@"main" libraryURI:@"main.dart"];
    
    }
    
    - (IBAction)jumpJTRouteAction:(id)sender {
        NSLog(@"跳转指定路由的fluter页面");
    }
    

    相关文章

      网友评论

          本文标题:ios项目嵌套flutter页面的详细步骤

          本文链接:https://www.haomeiwen.com/subject/siededtx.html