Where to Place App Delegate Code in App.Swift File

Where to place app delegate code in App.swift file?

You should use .onOpenURL instead, like

@main
struct SampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
// .. do whatever needed here
}
}
}
}

SwiftUI app life cycle iOS14 where to put AppDelegate code?

Here is a solution for SwiftUI life-cycle. Tested with Xcode 12b / iOS 14

import SwiftUI
import UIKit

// no changes in your AppDelegate class
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print(">> your code here !!")
return true
}
}

@main
struct Testing_SwiftUI2App: App {

// inject into SwiftUI life-cycle via adaptor !!!
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
ContentView()
}
}
}

I need to add code to delegate.m file and I only can find delegate.swift in flutter

The code snippet is from objective c. You will either have to write your project in objective c or use a bridging header file to expose objective c code to your swift project. After you have exposed it to your swift project, you use the following example snippet from the authors github repo.

https://github.com/Clancey/simple_auth/blob/master/simple_auth_flutter/example/ios/Runner/AppDelegate.swift

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

Hope this helps you to move forward :)

How to call Swift from an Objective C project App Delegate?

Edit: error when adding @objc to the enum declaration in Constants.swift

Swift enums used as namespace cannot be exposed to Objective-C.
You may need to use class to make it work both for Swift and Objective-C:

@objcMembers
class Constants: NSObject {
static let publishableKey = "pk_live_..."
static let baseURLString = "http://54.33.123.227:1234"
static let defaultCurrency = "usd"
static let defaultDescription = "Receipt" //change to describe actual app & charge

private override init() {}
}

How to add code to AppDelegate using SwiftUI Life Cycle

The init might be too early, try in app delegate as follows

import GoogleMobileAds

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

GADMobileAds.sharedInstance().start(completionHandler: nil) // << here !!
return true
}
}

@main
struct YourApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
ContentView()
}
}
}

How to access a method in a class written in swift using the app delegate

This looks fine.

If you want to call getLink() from your App Delegate, you'll need to import the Auto-generated Swift Bridging Header File that Xcode creates when you compile your code. This will allow the Objective-c AppDelegate.m file to find your exposed swift code.

#import <ProjectName-Swift.h> will allow your Swift to be exposed to the AppDelegate, and you already have marked the class and functions @objc so they will be visible.

Then you just need to either create a singleton or shared instance of your AppLinkModule (React Native Modules are all run statically) or you need to instantiate your module class.

Once you've done so, you can call this function in the AppDelegate like so:

[[[AppLinkModule ] shared ] getLink];

How do I transfer data from the app delegate to another class

Here's a minimal example that explains my comment:

class AppDelegate: UIResponder, UIApplicationDelegate {

var arrayOfData = [Int]()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

arrayOfData.append(1)
arrayOfData.append(2)
arrayOfData.append(3)

return true
}

Then in your VC you can access your array by getting a reference to the app delegate:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Get the data from app delegate
let appDelegate = UIApplication.shared.delegate as? AppDelegate
print(appDelegate?.arrayOfData)
}

}

In your specific code, you can move this line where you create the data source:

let dataSource = DataSource()

to be just above where you declare your view models (theViewModels), so that you can reference it from elsewhere, just as I did with arrayOfData.

You may also want to make your property private and access it with a getter, to adhere to conventional programming practice.

More examples can be found here, although the accepted answer is meant for Objective-C:

Passing Data from App delegate to View Controller

Scroll down and you will see other ways to do what you need, even passing data back and forth between the App Delegate and other VCs.

How to Connect the App Delegate in Facebook SDK for iOS?

As I understand your iOS app is created and implemented using Objective C and so your not able to see AppDelegate.swift, If you want AppDelegate.swift then you have to create your iOS app using Swift.

AppDelegate.h/m files in Objective C are equivalent to AppDelegate.swift file in swift.

The Facebook SDK document you are referencing has given instruction with respect to swift iOS app.

You can refer below link for some Objective C code as well:
https://developers.facebook.com/docs/facebook-login/ios?sdk=fbsdk

There is no mention of Objective C in step 5. Connect Your App Delegate . However, I think you can skip this step and try from Step 6. Add Facebook Login to Your Code.(This step has both Swift and Objective-C code example.



Related Topics



Leave a reply



Submit