How to convert this method from Objective-C to Swift? - ios

I'm trying to convert a method from the React-native Firebase documentation, from Objective-c code to Swift. But it's not working, can someone lend me a help?
The doc I'm following: https://rnfirebase.io/docs/v4.3.x/notifications/ios
This is the Object C part I want to convert and add to the Swift file:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}
At the moment, this is my code, see the commented part:
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var bridge: RCTBridge!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
RNFirebaseNotifications.configure()
let jsCodeLocation: URL
jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "Zepplin", initialProperties: nil, launchOptions: launchOptions)
let rootViewController = UIViewController()
rootViewController.view = rootView
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = rootViewController
self.window?.makeKeyAndVisible()
return true
}
/*func application(_ application: UIApplication, didReceiveLocalNotification notification: [UILocalNotification: Any]?) -> Any {
RNFirebaseNotifications.instance(_, didReceiveLocalNotification, notification)
}*/
}
But of corse, it's not working, because the syntax is wrong... I'm not familiar with Swift.
Can someone lend me a hand?
I would be glad! Thankss in before-hand!!!
EDIT:
Sorry if my question was not clear enough. For me, the question was, "how to fix the syntax?", I knew that the syntax was wrong. I had nothing to add to my question, the error Xcode was giving me was expected, obvious because I don't know Objective-C, nor Swift, I was trying in my way. Even if I added the error to the question, I'm sure that something else would error too. That's why I asked if someone could convert it for me, it's only a few lines, so I think I was not asking too much.
Thanks #bsod, for mentionating the tool you posted in the comment!! This is exactly the tool I needed. I didn't know that something like this tool existed.
Thanks for everyone that helped me.

func application(_ application: UIApplication,
didReceive notification: UILocalNotification)
But this method is deprecated. You might want to consider migrating to the "UNUserNotificationCenter" related APIs.

Related

SwiftUI: where to initialize frameworks?

With UIKit and UIApplicationDelegate logic, I used to initialize all the frameworks and stuff in the good old applicationDidFinishLaunching... like this:
#UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Parse.initialize(with: ParseClientConfiguration() {
$0.applicationId = PARSE_APP_ID
// ...
})
FirebaseApp.configure()
// etc...
}
}
I'm now learning how to use SwiftUI, but since there is no more UIApplicationDelegate, and I've never really used UISceneDelegate before, I don't really know where I should do this.
Thank you for your help.

UIApplicationLaunchOptionsKey not found

UIApplicationDelegate method - application(_:didFinishLaunchingWithOptions:) showing an error with Swift 4.2 (Xcode 10).
UIApplicationLaunchOptionsKey not found
What is replacement of UIApplicationLaunchOptionsKey in Swift 4.2?
'UIApplicationLaunchOptionsKey' has been renamed to 'UIApplication.LaunchOptionsKey'. Replace 'UIApplicationLaunchOptionsKey' with 'UIApplication.LaunchOptionsKey'.
Click on error hint, will show you solution:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
}
Xcode would fix-it but UIApplicationLaunchOptionsKey is replaced by a nested type UIApplication.LaunchOptionsKey.
It should be UIApplication.LaunchOptionsKey, Please find following apple documentation
I have tried the below snippet of the code and worked for me.
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication: Any]? = nil) -> Bool {
}
Just provide UIApplication in launchOptions.
Hopes it will work for you as well. :)

UIApplication delegate must be used from main thread only

I have a standard AppDelegate file in my Xcode project, I'm experimenting with Firebase and as soon as I imported it & added
FirebaseApp.configure()
I was flagged with purple warnings: Click me
The whole AppDelegate looks as follows
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
}
These warnings never appeared whilst using Xcode 8, as a novice I am unsure on how to fix this, I have read that adding a DispatchQueue.main.async can fix it but I'm not sure where to add this.
Thanks!

Cannot access AppDelegate

Getting an Error on this line
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
Could not cast value of type 'LLAppDelegateProxy' (0x2bd218) to
'MyAppName.AppDelegate' (0x2bc708).
I was setting a RootViewController in AppDelegate's didFinishLaunchingWithOptions method which suddenly stopped working. Any help will be appreciated. Thanks in advance.
It is possible get access to the original AppDelegate using automatic integration. Add the following static var to your AppDelegate class:
class AppDelegate: UIResponder, UIApplicationDelegate {
static var originalAppDelegate:AppDelegate!
Next, set that variable at the beginning of didFinishLaunchingWithOptions:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
AppDelegate.originalAppDelegate = self
Now anytime you want access to your original AppDelegate you can retrieve it like so:
AppDelegate.originalAppDelegate.someMethod()

Chartboost delegate error

I am integrating chartboost into my sprite kit game and have successfully integrated bridging files into my swift code. Now in my app delegate I have the following:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let kChartboostAppID = "5554c8680d60255c6a0f4af4"
let kChartboostAppSignature = "1d0e271cd054c9a63473d2f1212ca33a2922a19f"
Chartboost.startWithAppId(kChartboostAppID, appSignature: kChartboostAppSignature, delegate: self)
Chartboost.cacheMoreApps(CBLocationHomeScreen)
return true
}
class func showChartboostAds()
{
Chartboost.showInterstitial(CBLocationHomeScreen);
}
func didFailToLoadInterstitial(location: String!, withError error: CBLoadError) {
}
the startWithId line gives me an error that it cant invoke this method with these arguments ( String, String, AppDelegate). This used to work fine on my objective c code.. Anyone knows how to fix this?
class AppDelegate: UIResponder, UIApplicationDelegate, ChartboostDelegate
Edit (from Lalit's comment below)
In startWithAppId the delegate is set to self, so you have to set the class AppDelegate as the delegate, which is done using:
ChartboostDelegate(protocol)

Resources