3

Notes

  • Recently updated to Xcode Version 14.2 (14C18)
  • Flutter 3.7.0 • channel stable

Problem

When running my flutter app for ios, I get this error:

Xcode build done.                                           86.8s
Failed to build iOS app
Could not build the precompiled application for the device.
Swift Compiler Error (Xcode): No such module 'shared_preferences_ios'
/Users/tomasward/Desktop/fredi_new/ios/Runner/AppDelegate.swift:3:7

Which is derived from this piece of code in AppDelegate.swift:

import UIKit
import Flutter
import awesome_notifications
import shared_preferences_ios //error here

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
      
      if #available(iOS 10.0, *) {
           UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
          }
         
       application.registerForRemoteNotifications()
      

            SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
                SwiftAwesomeNotificationsPlugin.register(
                  with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
                FLTSharedPreferencesPlugin.register(
                  with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
            }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

This code is boilerplate for the awesome_notifications plugin setup for ios. See here

I Have tried a million solutions

  • Deleting ios build and running flutter create .
  • Running pod install a million times, deleting podfile, etc.

No solution seems to be consistent. I am looking for a general solution from people working on Flutter projects, not native ios projects.

Also it would be great if someone who worked with awesome_notifications could give me some insight

3 Answers 3

12

For Latest/Current Version do this below

replace shared_preferences_ios with shared_preferences_foundation

and FLTSharedPreferencesPlugin with SharedPreferencesPlugin

as these are the latest native bindings of flutter internal See PR.

AppDelegate.swift contents with new updated solution

import UIKit
import Flutter
import awesome_notifications
import shared_preferences_foundation //replace this here

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

      if #available(iOS 10.0, *) {
          UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
      }

      application.registerForRemoteNotifications()

            SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
                SwiftAwesomeNotificationsPlugin.register(
                  with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
                SharedPreferencesPlugin.register( // replace here
                  with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
            }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

this is the good approach
Life saver. Good job
1

As you're probably already aware by now, shared_preferences_ios went away with shared_preferences version 2.0.16. So the fact you're seeing that means something didn't get updated correctly along the way.

What version of Flutter were you on previously?

FWIW my AppDelegate.swift looks quite different, and notably does not include imports for specific plugins:

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

On the other hand, I'm not using awesome_notifications. Is it possible the setup for it has changed since you first integrated, and the AppDelegate.swift modifications are no longer relevant? And, you're on the latest version of awesome_notifications?

Sorry I don't have an immediate and direct answer, but I hope this helps.

2 Comments

thanks @chuck, although you did not provide me with the exact answer, your first part of the response was key. I did not know that the shared_preferences_ios was deprecated, and with that, I could dig deeper.
Do we still have to use "SharedPreferencesIOS.registerWith()" or how to register now when new lib (foundation) does not have that method?
1

Kudos to the answer on this link:

This is issue created by shared_preferences v2.0.16. Long story short - package/module shared_preferences_ios was renamed to shared_preferences_foundation. Fastest way to work around this is to downgrade shared_preferences to 2.0.15.

shared_preferences: ^2.0.15 # Before
shared_preferences: 2.0.15 # After

You will then see this log when you run flutter pub get:

< shared_preferences 2.0.15 (was 2.0.17) (2.0.17 available)
+ shared_preferences_ios 2.1.1
+ shared_preferences_macos 2.0.5

Also, be careful with spelling mistakes as shared_prefrences_ios (wrong) is very similar to shared_preferences_ios (correct).

1 Comment

Should we still use shared_preferences_ios? as it is deprecated? "SharedPreferencesIOS.registerWith()" how to initiated register with with shared_preferences_foundation lib?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.