ios 配置不存在 Runner 文件夹下的 Flutter AppDelegate.m 文件

Flutter AppDelegate.m file under the Runner folder doesn't exist for ios config

我要整合

simple_auth_flutter: ^2.0.11

进入我的 flutter 项目。这应该 运行 通过 keycloak ...

在安装中它说我必须像这样为 ios 修改 AppDelegate.m:

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import <Flutter/Flutter.h>
#import <simple_auth_flutter/SimpleAuthFlutterPlugin.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];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return [SimpleAuthFlutterPlugin checkUrl:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    return [SimpleAuthFlutterPlugin checkUrl:url];
}

@end

但我的问题是,运行ner 文件夹中不存在此文件。

只有AppDelegate.swift 看起来像这样

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

我想我必须解决这个问题。我该如何正确地做到这一点?

AppDelegate.m 被主要使用 Objective-C 语言的 iOS 项目使用。 iOS 使用更新的 Swift 语言的项目有一个 AppDelegate.swift 文件。它们在功能上是相同的。

您或许可以使用以下代码来完成集成。

import UIKit
import Flutter

// Don't forget to add this!
import simple_auth_flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    // Add this
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return SimpleAuthFlutterPlugin.check(url)
    }

    // Then, add this  
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return SimpleAuthFlutterPlugin.check(url)
    }
}

请注意,Objective-C 和 Swift 版本的函数名称相同。由于您的 iOS 项目已经在使用 .swift 文件,因此它已经设置为使用称为 Bridging Header.

的东西

这允许从 Swift.

使用 Objective-C 中编写的代码(在本例中,simple_auth_flutter 包中的 iOS 代码)

因此,您可以导入所需的“simple_auth_flutter”模块并开始使用 Swift 中的 Objective-C 方法,无需额外设置。

参考资料

Simple Auth Code for iOS

Related Stack Overflow Question