pod 'GoogleAnalytics' in swift 4 won't run - ios

I read this instructions to use google analytics in my app
https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift
so I installed pod 'GoogleAnalytics' into my app and created a .h file in my swift application
so here is my h file codes in my app
#ifndef analytic_h
#define analytic_h
#import <Google/Analytics.h>
#endif /* analytic_h */
and here is my app Delegate codes But this code does not recognize GAI
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
guard let gai = GAI.sharedInstance() else {
assert(false, "Google Analytics not configured correctly")
}
gai.tracker(withTrackingId: "UA-xxxxxxxxx-x")
// Optional: automatically report uncaught exceptions.
gai.trackUncaughtExceptions = true
// Optional: set Logger to VERBOSE for debug information.
// Remove before app release.
gai.logger.logLevel = .verbose;
return true
}
I received this error for GAI
Use of unresolved identifier 'GAI'

My research on this-
Case 1- Adding Bridging header manually
I added .h file with name "MyProject-Bridging-Header.h" in the project.
And I received a file having content-
#ifndef MyProject-Bridging-Header_h
#define MyProject-Bridging-Header_h
#endif /* MyProject-Bridging-Header_h */
And then I tried adding one of the followings-
#import <GAI.h>
#import <Google/Analytics.h>
But none of them were working.
Case 2 - Adding bridging header automatically
For this, I added "Objective-C File" and it gave a prompt to add bridging header automatically and I accepted. In that bridging header file, I added
#import <GAI.h>
then it built like a charm and after that, I removed the line above and added -
#import <Google/Analytics.h>
It failed saying Google/Analytics.h not found.

Related

Flutter - Firebase iOS App: Build error: Module 'firebase_core' not found

I'm making a Flutter App with Firebase as the backend (I have setup both connections from Android and iOS to Firebase already).
I can build and run my app on both Android Emulator and real Android device, no problem.
But when I try to run my app on an iOS Simulator (haven't tried real iPhone yet), build fails in the file GeneratedPluginRegistrant.h with this error: Module 'firebase_core' not found.
Content of the file GeneratedPluginRegistrant.h:
(if I comment out the line #import cloud_firestore;, it will just throw the same error at the next such #import)
//
// Generated file. Do not edit.
//
// clang-format off
#import "GeneratedPluginRegistrant.h"
#if __has_include(<cloud_firestore/FLTFirebaseFirestorePlugin.h>)
#import <cloud_firestore/FLTFirebaseFirestorePlugin.h>
#else
#import cloud_firestore;
#endif
#if __has_include(<firebase_auth/FLTFirebaseAuthPlugin.h>)
#import <firebase_auth/FLTFirebaseAuthPlugin.h>
#else
#import firebase_auth;
#endif
#if __has_include(<firebase_core/FLTFirebaseCorePlugin.h>)
#import <firebase_core/FLTFirebaseCorePlugin.h>
#else
#import firebase_core;
#endif
#if __has_include(<flutter_tts/FlutterTtsPlugin.h>)
#import <flutter_tts/FlutterTtsPlugin.h>
#else
#import flutter_tts;
#endif
#if __has_include(<shared_preferences_ios/FLTSharedPreferencesPlugin.h>)
#import <shared_preferences_ios/FLTSharedPreferencesPlugin.h>
#else
#import shared_preferences_ios;
#endif
#implementation GeneratedPluginRegistrant
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
[FLTFirebaseFirestorePlugin registerWithRegistrar:[registry registrarForPlugin:#"FLTFirebaseFirestorePlugin"]];
[FLTFirebaseAuthPlugin registerWithRegistrar:[registry registrarForPlugin:#"FLTFirebaseAuthPlugin"]];
[FLTFirebaseCorePlugin registerWithRegistrar:[registry registrarForPlugin:#"FLTFirebaseCorePlugin"]];
[FlutterTtsPlugin registerWithRegistrar:[registry registrarForPlugin:#"FlutterTtsPlugin"]];
[FLTSharedPreferencesPlugin registerWithRegistrar:[registry registrarForPlugin:#"FLTSharedPreferencesPlugin"]];
}
#end
I recently opened this flutter project for the first time in months, so I had to update a lot of things. I have updated all dependencies in pubspec.yaml, upgraded from Flutter 2.?.? to Flutter 3.3.8 etc.
One important thing to mention is that Firebase now suggests you to add the dependencies to the iOS app via the Swift Package Manager (SPM), instead of pasting code into your Podfile. May this be the source of the problem?
This is the content of my AppDelegate.swift:
import UIKit
import Flutter
import FirebaseCore //firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FirebaseApp.configure() //firebase
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
I don't know what other information to add to help you understand where the problem might be. If you think I should add something, please tell me.

Error: `call FirebaseApp.configure() before using Firestore`

Background Info
I have started developing a backend for an simple App, and I have set up a database class (named DBDelegate) that all the files will communicate with.
In my AppDelegate.swift I have this:
static public var dbDelegate:DBDelegate = DBDelegate()
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
Its a static public so I can access the dbDelegate from other files.
In my other files, I have the following to help readability: (because it is a class it will pass by reference)
let dbDelegate = AppDelegate.dbDelegate
In my DBDelegate class:
var db = Firestore.firestore()
init() {
FirebaseApp.configure()
}
Building and Running
When I build my code, it builds fine.
On run, the app promptly crashes with SIGABRT.
The error message is:
Terminating app due to uncaught exception 'FIRAppNotConfiguredException', reason: 'Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore'
What I have tried
I have tried putting a breakpoint on the init function in the DBDelegate class. It does not reach the breakpoint.
I have tried making the all the dbDelegate variables lazy:
I got a compile error for the one in AppDelegate: lazy must not be used on an already-lazy global
Runtime errors for others: please call FirebaseApp.configure() before using Firestore.
I have tried the following (assigning dbDelegate in didFinishLaunchingWithOptions):
static public var dbDelegate:DBDelegate!
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
dbDelegate = DBDelegate()
return true
}
I get Compile error: Static member 'dbDelegate' cannot be used on instance of type 'AppDelegate'
Any help would be great!
Edit: I found a janky solution, see below.
Firstly, I would like to thank #DionzB for suggesting using singletons (which I did). I will reference his/her post in this answer.
Ok, so after some research and playing with breakpoints, I found that my custom class actually executes before the AppDelegate. Knowing such, I created a variable before the following line:
static let shared = FirebaseService()
the name does not matter, because I/you will not call it, and assign it to FirebaseApp.configure()
The FirebaseService class becomes:
class FirebaseService: NSObject {
let constantToNeverTouch = FirebaseApp.configure()
static let shared = FirebaseService()
init() {
}
}
Next, you must make sure that FirebaseApp.configure() is no where else in your code. It should not be in the AppDelegate either. Having multiple FirebaseApp.configure()'s crashes the app.
You can override the AppDelegate init method with FirebaseApp.configure() and make sure it loads before any windows are created.
override init() {
FirebaseApp.configure()
}
It would be better to create a new singleton for Firebase.
class FirebaseService: NSObject {
static let shared = FirebaseService()
init() {
FirebaseApp.configure()
}
}
Then you can access everything via shared like:
FirebaseService.shared.methodName
For configuring in app delegate you would need to call it like:
_ = FirebaseService.shared
Chances are your code are in the wrong order. Make sure your window code are put AFTER the firebaseApp.configure and not before. I was getting the same crash until I simply placed my window creation codes: (window = UIWindow(frame: UIScreen.main.bounds), etc) after the firebaseApp.configure connection .Just swapped their placement:
FirebaseApp.configure()
let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = HomeController()
window?.makeKeyAndVisible()
I got the same issue because my pod version it old. So the solution is
Check your pods by using command pod --version
If pod the version is old or not the same, CocoaPods needs to update. Use comment sudo gem install CocoaPods
After it has completed you need to check your pods again use command pod --version make sure your pod version is upgraded
cd to your project directory then use command pod deintegrate
After the command completed then use pod install
Open your project workspace, build & run

Integrating google analytics into iOS app

I'm following the instructions on here to integrate google analytics into my app: https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift#get-config
I'm at the point where I need to initialize analytics for my app. I've added this code in my AppDelegate.swift file:
import UIKit
import <Google/Analytics.h>
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject: AnyObject]?) -> Bool {
**// Configure tracker from GoogleService-Info.plist.
NSError *configureError;
[[GGLContext sharedInstance] configureWithError:&configureError];
NSAssert(!configureError, #"Error configuring Google services: %#", configureError);
// Optional: configure GAI options.
GAI *gai = [GAI sharedInstance];
gai.trackUncaughtExceptions = YES; // report uncaught exceptions
gai.logger.logLevel = kGAILogLevelVerbose; // remove before app release**
}
I'm getting the following error messages.
For my import <Google/Analytics.h> line, I'm getting this message:'Consecutive statements on a line must be separated by ';'.
For the rest of the code I'm getting several errors, although I simply copied the code in the tutorial into my file. See my screenshot.
enter image description here
You're getting this issue since you're in a Swift project. You'll need to create an Objective-C bridging header, and add the import statement there. Afterwards, everything should work properly. Check out this SO answer for more details:
https://stackoverflow.com/a/24005242/1784384

Exception:Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use

I am trying to implement Google Maps SDK into my project using Swift 2.0. I follow this but when running this, my app is getting the following error:
2015-08-25 19:05:17.337 googleMap[1919:54102] *** Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
*** First throw call stack:
(
0 CoreFoundation 0x00000001058499b5 __exceptionPreprocess + 165
...
...
...
31 UIKit 0x000000010606699e UIApplicationMain + 171
32 googleMap 0x00000001034b720d main + 109
33 libdyld.dylib 0x0000000107fba92d start + 1
34 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I have tried all possible solutions from StackOverflow.
You need to set this both in didFinishLaunchingWithOptions
GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")
Just adding to Rajasekaran Gopal
Remember to add
import GoogleMaps
import GooglePlaces
then
GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")
in didFinishLaunchingWithOptions
I just had the same problem. I created a GMSMapView object and it was initialized before maybe the api key could be read. So I moved it inside the viewDidLoad method and problem solved.
Before :
class ViewController: ..... {
let mapView = GMSMapView()
After :
class ViewController: ..... {
var mapView : GMSMapView?
override viewDidLoad(){
mapView = GMSMapView()
Here is a Google Maps tutorial for Swift:
http://www.appcoda.com/google-maps-api-tutorial/
Below is quoted from Google Maps Documentation:
Step 5: Get an iOS API key
Using an API key enables you to monitor your application's API usage,
and ensures that Google can contact you about your application if
necessary. The key is free, you can use it with any of your
applications that call the Google Maps SDK for iOS, and it supports an
unlimited number of users. You obtain an API key from the Google
Developers Console by providing your application's bundle identifier.
If your project doesn't already have a key for iOS applications,
follow these steps to create an API key from the Google Developers
Console:
In the sidebar on the left, select Credentials.
If your project doesn't already have an iOS API key, create one now by selecting Add credentials > API key > iOS key.
In the resulting dialog, enter your app's bundle identifier. For example: com.example.hellomap.
Click Create.
Your new iOS API key appears in the list of API keys for your
project. An API key is a string of characters, something like this:
AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
Add your API key to your AppDelegate.m as follows:
Add the following import statement:
#import GoogleMaps;
Add the following to your application:didFinishLaunchingWithOptions: method, replacing API_KEY with your API key:
[GMSServices provideAPIKey:#"API_KEY"];
Source
If you already set GMSMapView class to your view in interface builder, make GMSServices.provideAPIKey("API key") in initialization methods of viewController which contains GMSMapView.
You should set up API keys before you set up the UIWindow in AppDelgate, if your initial View Controller uses GoogleMaps.
For example:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//MARK: Google Maps setup
GMSServices.provideAPIKey("YOUR API KEY")
GMSPlacesClient.provideAPIKey("YOUR API KEY")
let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = UIColor.white
window.makeKeyAndVisible()
window.rootViewController = ViewController()
self.window = window
return true
}
In my app i provided key to GMSPlacesClient in AppDelegate -didFinishLaunchingWithOptions
Ex:
#import GooglePlaces;
#import GoogleMaps;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:#"You key"];
[GMSPlacesClient provideAPIKey:#"Your key"];
}
For Swift:
import GoogleMaps
import GooglePlaces
//in application(_:didFinishLaunchingWithOptions:)
GMSServices.provideAPIKey("YOUR_API_KEY")
GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
in my case --didFinishLaunchingWithOptions-- method didn't call because i updated swift version. first make sure this method called normally. and in my case i didn't need to GMSPlacesClient.provideAPIKey("Your key"). however you can initial GMSServices in your initial viewcontroller (if its not contain map or related objet to it)
You are skipping provideAPIKey in your AppDelegate check project
Your_PROJECT -> ios -> Runner -> AppDelegate.m
Replace your file code with below snippet add your APIKEY
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:#"GOOGLE_API_KEY"];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
#end
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...]
XCODE 12.0.1, Swift 5
I was applying constraints to Google Map View by initialising GMSMapView like this:-
private var googleMapsView : GMSMapView = {
let mainView = GMSMapView()
mainView.translatesAutoresizingMaskIntoConstraints = false
mainView.isMyLocationEnabled = true
return mainView
}()
& I was getting this same error, so I tried this making it a lazy property:-
lazy private var googleMapsView : GMSMapView = {
let mainView = GMSMapView()
mainView.translatesAutoresizingMaskIntoConstraints = false
mainView.isMyLocationEnabled = true
return mainView
}()
& voila! It worked like a charm.
Ensure that the lines you added to AppDelegate.m are actually being executed.
Since the integration with Flipper around react-native v0.62, a portion of the AppDelegate.m file is guarded by an ifdef FB_SONARKIT_ENABLED. If Flipper isn't enabled for whatever reason, any code inbetween the ifdef and the closing endif lines will not be executed. Make sure you paste the #import above the ifdef, and the [GMSServices] line in the didFinishLaunchingWithOptions method, immediately after the opening curly brace ({).
More info on Flipper in React Native: https://fbflipper.com/docs/features/react-native/
Added in the necessery functions providing the API Key in your AppDelegate and still getting this issue? Don't forget to ENABLE the specific API you need (i.e. Maps SDK for iOS/Android) in the Google Cloud Console as well:
https://console.cloud.google.com/apis/library
It may take a few minutes for the API to enable/propogate too.

iOS - Open certain view controller with URL scheme

I'm playing around with URL schemes in my app. I easily made one to open my app, just adding the necessary items to info.plist. This current URL "myappname://" takes the user to the initial view controller, FirstTableViewController, but I was wondering if it would be possible to modify that URL scheme so it I can have one that takes the user to a certain view controller, such as ThirdTableViewController. I would use this as a handy feature in something like Launch Center.
This post is a little old but maybe useful for iOS 5 + because the checked answer is not correct.
AppDelegate doesn't have any navigationController property.
Instead you can do in AppDelegate.m :
enter code here
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
[navController presentModalViewController:controller animated:YES];
return YES;
}
Try look at this: Custom Url Schemes
Hope this will be a useful
In ...AppDelegate.m
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
[self.viewController presentModalViewController:controller animated:YES];
[controller release];
return YES;
}
Hi here is my solution.
If you can call your navigation function that called in "application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool" event (with a delay) you can navigate a specific page in the app even is not running before called.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Actived keyboard avoider
changeAppereance()
delay(1) {
deeplink = Deeplink()
self.manageNavigation(launchOptions: launchOptions)
self.navigate()
}
return true
}
private func manageNavigation(launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL { //Deeplink
print(url.absoluteString)
deeplink = Deeplink()
deeplink?.url = url
}
else if let activityDictionary = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] { //Universal link
for key in activityDictionary.keys {
if let userActivity = activityDictionary[key] as? NSUserActivity {
if let url = userActivity.webpageURL {
deeplink = Deeplink()
deeplink?.url = url
}
}
}
}
}
open func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print(url.absoluteString)
deeplink = Deeplink()
deeplink?.url = url
navigate()
return true
}
Posting a new answer for Swift 5. Many of the answers are outdated or address handling custom URLs, but not specifically how to open a UIViewController from within the AppDelegate. Run this code within your AppDelegate:
//Get the view controller that is currently displayed upon launch
let rootViewController = window?.rootViewController
//Initialise the view controller you wish to open
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MyViewController")
//Launch the view controller
rootViewController.present(vc, animated: true)
Yes it is possible to modify URL scheme so that you can jump user to any viewcontroller.I used and implement normal as well as https://hokolinks.com/ deep link.By hoko link deep linking you can modify your URL Scheme ,also you can send data with that URL.
Integrate iOS SDK Using Hoko Link
- Add a URL Scheme to your App
- SDK Setup
To integrate HOKO open source SDK in your app (only iOS 5 and higher) you just have to follow 3 simple steps (either using cocoapods or doing it manually).
Using CocoaPods
1- Install CocoaPods in your system
2- Open your Xcode project folder and create a file called Podfile with the following content:
pod 'Hoko', '~> 2.3.0'
3- Run pod install and wait for CocoaPods to install HOKO SDK. From this moment on, instead of using .xcodeproj file, you should start using .xcworkspace.
Manual integration
1- Download the Hoko SDK.
2- Drag the Hoko folder to your project.
3- Be sure to also add SystemConfiguration.framework and zlib.dylib in case your project does not include it already.
Integrating the SDK with your Swift project
Because the HOKO SDK is written in Objective-C, you’ll have to manually add a Bridging Header file into your project in order to use it with your Swift code:
1- File > New > File... > iOS > Source > Header File
2- Name that header file YourAppName-Bridging-Header.h
3- Inside that header file, import #import
4- Go to your project > Build Settings > Search for Objective-C Bridging Header > Add the path to your bridging header file, from your root folder (e.g. MyApp/MyApp-Bridging-Header.h)
Add a URL Scheme to your App
Next, we need to define your app’s custom URL type, if you don’t have one already. Open your Xcode project settings and under the “Info” tab expand the “URL Types” section. You can skip this step if you already configured a URL type.
If this section is empty, click in the “+” icon to add a new URL type. Let’s say that we want to open the app via “hoko://”. Hence we need to enter “hoko” in URL Schemes.
We also should assign a unique Identifier to this URL type. Apple recommends that you use reverse DNS notation to ensure that there are no name collisions between types. In this example we are going to use “com.hoko.app”.
Take note of your URL Scheme because we will ask you for it, when you are creating an app through the dashboard, e.g. “hoko”.
URL Scheme
Setup Associated Domains (Universal Links) - iOS 9.0+
For your app to fully support the newly introduced Universal Links by Apple you’ll have to enable and add a new entry in the Associated Domains section, inside your application target’s Capabilities tab. Click on the ‘+’ button and add a new entry with the following value: applinks:myapp.hoko.link, being myapp the Hoko subdomain you chose for your app’s Hoko links. You can also have your own link domain (learn more about this on the subdomains section).
URL Scheme
SDK Setup
Add the following line to your applicationDidFinishLaunching method in your AppDelegate class (don’t forget to import the HOKO class by using #import if you’re working with Objective-C).
Objective-C
Swift
#import
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Hoko setupWithToken:#"YOUR-APP-TOKEN"];
// The rest of your code goes here...
return YES;
}
If you are using a custom domain in your smart links, you must setup the iOS SDK using setupWithToken:customDomain: as following:
Objective-C
Swift
#import
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Hoko setupWithToken:#"YOUR-APP-TOKEN"
customDomain:#"your.custom.domain.com"];
// The rest of your code goes here...
return YES;
}
NOTE: make sure to return YES in the application:didFinishLaunchingWithOptions: delegate method to allow incoming deep links that open your app to be processed. Returning NO will block the requests.
Setup your mobile deep linking by using Hoko Link SDK

Resources