0

I would like to integrate

simple_auth_flutter: ^2.0.11

into my flutter project. This should run via keycloak ...

In the installation it says that I have to modify the AppDelegate.m for ios like this:

#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

but my problem is, there doesn't exist this file in runner folder.

There is only the AppDelegate.swift it look like this

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)
  }
}

I guess I'll have to work with that. How do I do that properly?

Kyrill
  • 265
  • 1
  • 7
  • 19
  • I'm guessing you created a new app iOS app using SwiftUI instead of Storyboard and Objective-C? That or you're using the Xcode beta? I'm on Xcode 12.5.1 and it has the option to build an Objective C AppDelegate.m file. – Waxhaw Jul 21 '21 at 13:21
  • I also have the 12.5.1 version. – Kyrill Jul 21 '21 at 16:48

1 Answers1

3

The AppDelegate.m is used by iOS Projects that primarily use the Objective-C language. iOS Project using the newer Swift language have a AppDelegate.swift file instead. They are identical in functionality.

You might be able to use the following code to complete the integration.

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)
    }
}

Notice that the function names are identical between the Objective-C and Swift versions. Since your iOS project is already using .swift files, it is already setup to use something called a Bridging Header.

This allows code written in Objective-C (in this case, iOS code in the simple_auth_flutter package) to be used from Swift.

Because of this, you can import the required "simple_auth_flutter" module and start using its Objective-C methods from Swift without additional setup.

References

Simple Auth Code for iOS

Related Stack Overflow Question