Change IBOutlets values after applicationWillEnterForeground crashes my app - ios

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.

Related

xmppStreamDidConnect never gets called in Swift

I am trying to connect to my chat server in a Swift app using XMPPFramework, but the didConnect delegate method never gets called.
I have created a basic app in Objective C and I can connect and authenticate in my chat sever without problems.
In the Swift project I tried to connect with the code:
class AppDelegate: UIResponder, UIApplicationDelegate {
var stream:XMPPStream = XMPPStream()
var reconnect:XMPPReconnect = XMPPReconnect()
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
stream.addDelegate(self, delegateQueue: DispatchQueue.main)
stream.myJID = XMPPJID(string: "user#chatserver.net")
reconnect.activate(stream)
do {
try stream.connect(withTimeout: XMPPStreamTimeoutNone)
}
catch let err{
print("error occured in connecting\(String(describing: err.localizedDescription))")
}
return true
}
I’ve debugged XMPPFramework and in the method - (void)handleStreamFeatures a call to the delegate is executed :
[multicastDelegate xmppStreamDidConnect:self];
I’ve watched the multicastDelegateObject and has a node with reference to my delegate, and to OS_dispatch_queue_main, but after execution my xmppStreamDidConnect method isn’t executed.
As it is described in this Github Issue, the problem was the method declaration.
My xmppStreamDidConnect need an underscore, the root problem was that if you don't import the swift extensions the compiler marks that declaration as incorrect, although it works.
So to fix my problem, I need to import the pod 'XMPPFramework/Swift' and change the method declaration to
func xmppStreamDidConnect(_ sender: XMPPStream) {

BLE background processing not working in iOS application

I have an application that needs to interact with peripherals when the app enters a suspended or terminated state. I've added the bluetooth-central to the Info.plist and Capabilities (image) but after i connect to a peripheral it doesn't fire any of the delegate methods while running in the background, but as soon as the app enters the foreground the delegate methods are called! I've also set up CBCenteralManager state restoration
manager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: CoreBluetoothRestorationKey])
let ids = [CBUUID(string: ServiceUUID)]
manager.scanForPeripheralsWithServices(ids, options: nil)
manager.connectPeripheral(peripheral, options: nil)
I've been digging around the internet for hours and haven't found anything. Has anyone had a similar situation before? Any tips? Thanks!
edit: code in context:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let bluetoothManager = CoreBluetoothObserver()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return true
}
}
class CoreBluetoothObserver: NSObject {
let manager: CBCentralManager
override init() {
manager = CBCentralManager(delegate: nil, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: CoreBluetoothRestorationKey])
super.init()
manager.delegate = self
}
}
the CoreBlueToothObserver() initializes CBCenteralManager in its init method and sets self to its delegate. I would assume CoreBlueToothObserver() would be around for the lifetime of the app and not need to be re-initalized in didFinishLaunchingWithOptions? or maybe i need to recreate a CBCenteralManager and inject it in didFinishLaunchingWithOptions? Just stumped as to whats happening.
With the help from #Paulw11 this worked
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var bluetoothManager: CoreBluetoothObserver!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
bluetoothManager = CoreBluetoothObserver()
return true
}
}
app is now receiving the delegate methods!

problems AppDelegate with new Swift 2.2 / Xcode 7.3.1

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 {

Swift - App Delegate not passing data

I'm developing an App that it's data is from a URL, here's a sample code that I'm using
AppDelegate.swift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var fromUrl: String!
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject)-> Bool {
print("Host: \(url.host!)")
self.fromUrl = url.host!
return true
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let appDelegate = AppDelegate()
override func viewDidLoad() {
super.viewDidLoad()
print(appDelegate.fromUrl)
}
It's is logging the url.host from app delegate. But when i try to log the value of fromUrl from the ViewController.swift it's returning nil. What do you think seems to be the problem? Thanks!
When you declare let appDelegate = AppDelegate() in ViewController you are actually instantiating another instance of AppDelegate. That is not the same instance that you are using as you actual ApplicationDelegate. Try getting that reference by using:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
print(appDelegate.fromUrl)
}

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