Google Maps for Flutter iOS Swift setup - ios

The instructions for setting up the official Google Maps for Flutter plugin include adding the Google API key to the AppDelegate.m file:
Specify your API key in the application delegate ios/Runner/AppDelegate.m:
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:#"YOUR KEY HERE"];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
#end
My flutter project has an AppDelegate.swift file instead of an AppDelegate.m file and I'm not sure how to add the required key, as the syntax is different:
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Can anyone help me out?

You can add your API key as follows:
AppDelegate.swift:
import UIKit
import Flutter
import GoogleMaps // Add this line!
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
GMSServices.provideAPIKey("YOUR_API_KEY") // Add this line!
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

One more thing, Dont forget to add below line on ios/Runner/Info.plist
<key>io.flutter.embedded_views_preview</key>
<true/>

Related

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

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?
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

simple_auth_flutter on ios AppDelegate.swift import change fails to start

I would like to use simple_auth_flutter into my FLutter app to authenticate user against instagram. So I followed the documentation page and applied all changes. I try to add the mentioned lines into my AppDelegate.swift as below:
import UIKit
import Flutter
import SimpleAuth;
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any]?) -> Bool{
return SimpleAuth.CheckUrl(url);
}
}
When I try to execute flutter run I see the error:
no such module 'SimpleAuth' import SimpleAuth;
So what should I do to solve the import in the AppDelegate? My pubspec.yml uses:
simple_auth: ^2.0.7
simple_auth_flutter: ^2.0.7
I think i've found a solution that allows my app to build.
import UIKit
import Flutter
import simple_auth_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)
}
override func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any]?) -> Bool{
return SimpleAuthFlutterPlugin.check(url);
}
}

Confusion connecting iOS portion of Flutter App to Firebase

I'm attempting to connect the iOS portion of my Flutter app to Firebase. And as I go through the steps on Firebase - "Add Firebase to your iOS app" - I hit a step that says "Add the initialization code below to your main AppDelegate class" (Swift version):
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
FirebaseApp.configure()
return true
}
}
But my AppDelegate class already has this code:
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Not sure what to do. Do I replace the existing code with the code provided by Firebase or do I reconcile the two somehow?
In the given (predefined) AppDelegate class, there are 2 things you need to do additionally.
They are
import Firebase // <-- 1st add
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FirebaseApp.configure() // <-- 2nd add
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Merge both code together:
import UIKit
import Flutter
import Firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FirebaseApp.configure()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

Objective-C AppAuth OIDAuthorizationService class in Swift, and Objective-C -> Swift translation

I need to declare a variable of type OIDAuthorizationService, from the AppAuth (https://github.com/openid/AppAuth-iOS) library, in Swift while translating the following Objective-C to Swift to use an Objective-C library in a project.
Pre-translated objective-c
.h
+ #protocol OIDAuthorizationFlowSession;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
+ #property(nonatomic, strong, nullable)
id<OIDAuthorizationFlowSession> currentAuthorizationFlow;
#property (nonatomic, strong) UIWindow *window;
#end
.m
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<NSString *, id> *)options {
if ([_currentAuthorizationFlow resumeAuthorizationFlowWithURL:url]) {
_currentAuthorizationFlow = nil;
return YES;
}
return NO;
}
So far I have the following translation:
var currentAuthorizationFlow: OIDAuthorizationFlowSession?
...
func application(_ application: UIApplication, openURL: NSURL,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if currentAuthorizationFlow!.resumeAuthorizationFlow(with: openURL as URL) {
return true
}
return false
}
Is this code correct?
Error is: Use of undeclared type: 'OIDAuthorizationFlowSession', as espected, how do I do this?
Much appreciated
I understand that OIDAuthorizationFlowSession protocol is defined in Objective-C and you're trying to use it in Swift, if that's the case then you need a Bridging-Header.h, there you can import the corresponding header for your OIDAuthorizationFlowSession.
How do you create a Bridging Header file? Well it should be created automatically when you create a new swift file, if that's not the case, take a look here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
If the easy way doesn't work, see this one: Xcode not automatically creating bridging header?
LE: if you integrated the AppAuth-iOS as a pod, here's what's working 100%:
import UIKit
import AppAuth
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var currentAuthorizationFlow: OIDAuthorizationFlowSession?
func application(_ application: UIApplication, openURL: NSURL,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if currentAuthorizationFlow!.resumeAuthorizationFlow(with: openURL as URL) {
return true
}
return false
}
So don't forget the "import AppAuth" part.

Unresolved identifier "PFFacebookUtils" in swift in Xcode6

I am trying to have users login to facebook with parse but am receiving the compiler error "Use of unresolved identifier" for PFFacebookUtils, Parse, FBAppCall, and an invalid DidBecomeActive
This question has been posted before and the solution was to add the parsefacebookutils framework into your project and import it into the header file. However, I have already added the framework to my project and imported it into my header file by adding the following to the my header.
#import <Parse/Parse.h>
#import <ParseFacebookUtils/PFFacebookUtils.h>
#import <FacebookSDK/facebookSDK.h>
I am still getting the error. The following is the code from my appdelegate file:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
PFFacebookUtils.initializeFacebook()
Parse.setApplicationId("parseAppId", clientKey:"parseClientKey")
}
return true
}
func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String,
annotation: AnyObject?) -> Bool {
return FBAppCall.handleOpenURL(url, sourceApplication:sourceApplication,
withSession:PFFacebookUtils.session())
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
FBAppCall.handleDidBecomeActiveWithSession(PFFacebookUtils.session())
}
Could you please take a look at my code above? Not sure what the solution is.
Figured it out. You have to go into the Build Settings - Objective C Header and then type the path to your header in the debug name. For example ProjectName/HeaderName.h as according to where I had my file within my project. Normally you wouldn't have to do this, but it didn't populate for me when I created the header.

Resources