I recently upgraded Xcode and tried to continue programming. My app can't be built. It says that the problems are in the AppDelegate.
I copy my code:
import UIKit
#UIApplicationMain
AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let defaults = NSUserDefaults.standardUserDefaults()
var rootViewController : UIViewController;
if (defaults.boolForKey("HasBeenLaunched")) {
// This gets executed if the app has ALREADY been launched
rootViewController = storyboard.instantiateViewControllerWithIdentifier("maintabcontroller") as UIViewController
} else {
// This gets executed if the app has NEVER been launched
defaults.setBool(true, forKey: "HasBeenLaunched")
defaults.synchronize()
rootViewController = storyboard.instantiateViewControllerWithIdentifier("setupstory") as UIViewController
}
window?.rootViewController = rootViewController;
window?.makeKeyAndVisible();
UITabBar.appearance().barTintColor = UIColor.whiteColor()
UITabBar.appearance().tintColor = UIColor.blackColor()
return true
}
}
The errors are in the line AppDelegate: UIResponder, UIApplicationDelegate {. They are:
expected declaration
statement cannot begin with a closure expression
expressions are not allowed at the top level
braced block of statements is an unused
expression resolves to an unused function
Before upgrading I didn't get all these errors.
You've accidentally deleted the class keyword:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Related
This question already has answers here:
Opt out of UISceneDelegate/SwiftUI on iOS
(6 answers)
Closed 3 years ago.
I just started a new project of iOS. I created the project using xcode 11 and iOS 13. When I created project i found out that in order to set our own rootController we have to use sceneDelegate instead of AppDelegate. I want to ask if there is any possibility to use old method of setting rootControllers in AppDelegate instead of using sceneDelegate.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let loginController: HomeViewController = HomeViewController(nibName: "HomeViewController", bundle: nil)
// let navController: UINavigationController = UINavigationController(rootViewController: loginController)
self.window?.rootViewController = loginController
self.window?.makeKeyAndVisible()
// Override point for customization after application launch.
return true
}
}
Follow these steps to use AppDelegate and opt-out for SceneDelegate
Go to Info.plist and remove Application Scene Manifest entry from Info.plist.
Remove SceneDelegate.swift
Add var window: UIWindow? in your AppDelegate.swift file
Delete the UISceneSession Lifecycle code from AppDelegate.swift
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
let loginController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController = loginController
self.window?.makeKeyAndVisible()
return true
}
}
Make sure to give "HomeViewController" storyboardID to your view controller.
This is how your AppDelegate.swift file should look now. You are good to go!
Why don't you use the SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
//Make sure to do this else you won't get
//the windowScene object using UIApplication.shared.connectedScenes
self.window?.windowScene = windowScene
let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
window?.rootViewController = storyBoard.instantiateInitialViewController()
window?.makeKeyAndVisible()
}
If you don't want to use sceneDelegate then you can remove all sceneDelegate and also remove 'Application Scene Manifest' from info.plist
set UIWindow variable
var window: UIWindow?
Make sure you have removed Application Scene Manifest this from info.plist and change the background colour of your view controller.
Your device's Dark Mode is enable if you want to remove dark mode from app add this key into your info.plist
User Interface Style = Light
I have installed KYDrawercontroller in my project. it is available in project and can able to import it.any help will be appricated.thanks in advance
As you can check in KYDrawerController docs you have to use DrawerViewController and KYDrawerController, there isn't DrawerController:
import UIKit
import KYDrawerController
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let mainViewController = MainViewController()
let drawerViewController = DrawerViewController()
let drawerController = KYDrawerController()
drawerController.mainViewController = UINavigationController(
rootViewController: mainViewController
)
drawerController.drawerViewController = drawerViewController
/* Customize
drawerController.drawerDirection = .Right
drawerController.drawerWidth = 200
*/
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = drawerController
window?.makeKeyAndVisible()
return true
}
I have created the iOS Project in Xcode 11.1. I need to remove scene delegate from the application.
You need to do the following steps:
Remove Scene delegate methods from App Delegate and delete the Scene delegate file.
You need to remove UIApplicationSceneManifest from Info.plist.
You also need to add var window:UIWindow? if it is not present in AppDelegate
Remove SceneDelegate.swift file
Remove Application Scene Manifest from Info.plist file
Add var window: UIWindow? to AppDelegate.swift
Replace #main with #UIApplicationMain
Remove UISceneSession Lifecycle ( functions ) in AppDelegate
It is a complete solution for empty project generated with Xcode (with storyboard)
Remove SceneDelegate.swift file
Remove Application Scene Manifest from Info.plist file
Paste this code to your AppDelegate.swift file
import UIKit
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window:UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
return true
}
}
Adding on to milzi's answer
Remove SceneDelegate.swift file
Remove Application Scene Manifest from Info.plist file
Remove UISceneSession Lifecycle function from your AppDelegate class
Add var window: UIWindow? in your AppDelegate class as a instance property
Replace #main attribute with #UIApplicationMain attribute (This saves as to manually create and assign window)
Below is how you how your AppDelegate should look like after changes:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
To add to accepted answer: you also need to write this in your AppDelegate:
self.window = UIWindow(frame: UIScreen.main.bounds)
let controller = MyRootViewController()
window?.rootViewController = controller
window?.makeKeyAndVisible()
Just in case if you are developing in Objective-C, add window property in the AppDelegate.h file like this:
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
No need to initialise the window. It will work automatically.
I downloaded sample code from here for a location tracking application https://www.pubnub.com/blog/2015-05-05-getting-started-ios-location-tracking-and-streaming-w-swift-programming-language/. I am trying to run the application but in the AppDelegate class I am getting an error saying "Class AppDelegate has no initializers". What is causing this error and how can I fix it?
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: - Properties
//var window: UIWindow?
var window = UIWindow(frame: UIScreen.mainScreen().bounds)
// MARK: - App Life Cycle
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Adding a Navigation Controller and tool bar
self.window.rootViewController = UINavigationController(rootViewController: MainViewController(nibName: nil, bundle: nil))
// Make window visible
self.window.makeKeyAndVisible()
return true
}
}
I would set the window to be an optional value with no default. just as you originally had then commented out
var window: UIWindow?
then give window a value and programmatically add the root view controller when your app launches
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
Using: XCode 7, iPhone 5 (iOS 8.3), WiFi, Reachability (check the internet connection).
While my app is in background and I click to open my app it checks the conncetion and load some functions and in 1 of the functions I try to sign:
imageView.image = UIImage(named: "imagename")
error: fatal error: unexpectedly found nil while unwrapping an Optional value
This happens only when my app change an IBOutlet value in applicationWillEnterForeground with function to primary view controller
self.viewController.functionName()
class AppDelegate: UIResponder, UIApplicationDelegate {
var viewController:ViewController = ViewController()
func applicationWillEnterForeground(application: UIApplication) {
self.viewController.checkConn()
}
}
checkConn() check the connection with Reachability and change IBOutlets values like .image and .text
Is there any way to fix it?
After a lot of tests I found this method which works great:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var viewController:ViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
viewController = self.window!.rootViewController as? ViewController
return true
}
func applicationWillEnterForeground(application: UIApplication) {
viewController!.checkConn()
}
}
I am assuming that this is where your crash is happening.
imageView.image = UIImage(named: "imagename")
If you are using "Images.xcassets" to manage your image files. Make sure that "imagename" exists.