I made an app, I want to add function that notify user when app's internet reachability is changed.
I use Ashley Mills' Reachability.swift file.
now I understand how it works, So I put code that when internet reachability is changed, it will print it's status in appDelegate.
However when I tried to put in function that alert user there isn't internet connection, It gets an error.
here is my code in app delegate.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var reachability : Reachability?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
do {
let reachability = try Reachability.reachabilityForInternetConnection()
self.reachability = reachability
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
}
catch {}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
do {
try reachability?.startNotifier()
} catch {}
return true
}
func reachabilityChanged(notification: NSNotification) {
let reachability = notification.object as! Reachability
if reachability.isReachable() {
print("reached")
} else {
print("not reached")
}
}
This works well.
However the code in Viewcontroller,
class ViewController: UIViewController {
var reachability : Reachability?
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "HeyUserInternetDoesntWork", name: ReachabilityChangedNotification, object: nil)
//get call from appDelegate Notification.
}
func HeyUserInternetDoesntWork() {
if reachability!.isReachable() {
print("notify User working")
} else {
print("Notify user not working")
}
}
unexpectedly found nil while unwrapping an Optional value
It gets this error.
I am going to put code for alerting user after it works.
Question here,
How Can I make it this work?
It doesn't have to be use that method, but I want to keep using NSNotification.
Actually I am a new guy for coding, So please explain details.
Where do you init reachability property? this variable is always nil.
In func HeyUserInternetDoesntWork you try to use reachability and of course it gets error. You need to init property like this:
private let reachability = Reachability.reachabilityForInternetConnection()
After use func HeyUserInternetDoesntWork with 'dynamic' keyword like this:
dynamic func HeyUserInternetDoesntWork() {
if reachability!.isReachable() {
print("notify User working")
} else {
print("Notify user not working")
}
}
Because NSNotificationCenter observer selector should be dynamic.
Related
I am using Xcode 9.2/iOS 10 and Swift to detect user inactivity in iOS applications using information based on an excellent tutorial example from https://blog.gaelfoppolo.com/detecting-user-inactivity-in-ios-application-684b0eeeef5b The enhancements I am making are to start the timer when viewDidLoad is called and adding a snooze function in applicationDidTimeout. At compile time, the following error message is displayed: "Instance member 'snoozeTimer' cannot be used on type 'TimerApplication'; did you mean to use a value of this type instead?"
applicationDidTimeout will use snoozeTimer to control whether it needs to have another timer alarm depending on additional logic to be added to the method.
What is the correct way to call startTimer and snoozeTimer?
A code listing is shown below:
main.swift
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
NSStringFromClass(TimerApplication.self),
NSStringFromClass(AppDelegate.self)
)
AppDelegate.swift
import UIKit
import Foundation
extension Notification.Name {
static let appTimeout = Notification.Name("appTimeout")
}
//#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var backgroundTaskRunCount : Int = 0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
NotificationCenter.default.addObserver(self,
selector: #selector(AppDelegate.applicationDidTimeout(notification:)),
name: .appTimeout,
object: nil
)
return true
}
. //other func
.
.
#objc func applicationDidTimeout(notification: NSNotification) {
backgroundTaskRunCount = backgroundTaskRunCount + 1
print("application did timeout, perform actions. Count=", backgroundTaskRunCount)
TimerApplication.snoozeTimer()
}
}
TimerApplication.swift
import UIKit
class TimerApplication: UIApplication {
private var wakeupInSeconds: TimeInterval {
// 2 minutes for for timer to fire first time
return 2 * 60
}
private let snoozeInSeconds: TimeInterval = 5
// the timeout in seconds, after which should perform custom actions
// such as disconnecting the user
private var timeoutInSeconds: TimeInterval = 120
private var idleTimer: Timer?
// resent the timer because there was user interaction
private func resetIdleTimer() {
if timeoutInSeconds > 0 {
if let idleTimer = idleTimer {
idleTimer.invalidate()
}
idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds,
target: self,
selector: #selector(TimerApplication.timeHasExceeded),
userInfo: nil,
repeats: false
)
} else {
stopTimer()
}
}
// if the timer reaches the limit as defined in timeoutInSeconds, post this notification
#objc private func timeHasExceeded() {
NotificationCenter.default.post(name: .appTimeout,
object: nil
)
}
override func sendEvent(_ event: UIEvent) {
super.sendEvent(event)
if idleTimer != nil {
self.resetIdleTimer()
}
if let touches = event.allTouches {
for touch in touches where touch.phase == UITouchPhase.began {
self.resetIdleTimer()
}
}
}
func startTimer() {
self.timeoutInSeconds = wakeupInSeconds
self.resetIdleTimer()
}
func stopTimer() {
if let idleTimer = idleTimer {
idleTimer.invalidate()
}
}
func snoozeTimer() {
self.timeoutInSeconds = snoozeInSeconds
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
TimerApplication.startTimer()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You want to use TimerApplication.shared.snoozeTimer() instead of TimerApplication.snoozeTimer() because snoozeTimer() is an instance method, not class or static method.
You should verify that TimerApplication.shared returns the instance of TimerApplication you're expecting because I've never subclassed a system singleton like this. If that doesn't work, you'll need a way to get a reference to the TimerApplication instance you want.
I'm trying to implement Google Sign-in to an iOS app but the app crashes on clicking on the Sign-in button with the following error:
reason: 'uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.'
I'm not sure where I'm going wrong. I've followed the sample code from Google's iOS github exactly. I also can't get Google's sample to compile. If anyone could point me in the right direction, that would be great. Most of the SO questions are based on dated code.
Viewcontroller
import UIKit
import GoogleSignIn
#objc(ViewController)
class ViewController: UIViewController, GIDSignInUIDelegate {
// Viewcontroller buttons
#IBOutlet weak var signInButton: GIDSignInButton!
#IBOutlet weak var signOutButton: UIButton!
#IBOutlet weak var disconnectButton: UIButton!
#IBOutlet weak var statusText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
// Sign in automatically
GIDSignIn.sharedInstance().signInSilently()
// Something to do with notifications
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.receiveToggleAuthUINotification(_:)),
name: NSNotification.Name(rawValue: "ToggleAuthUINotification"),
object: nil)
statusText.text = "Initialized Swift App..."
toggleAuthUi()
}
// Sign out tapped
#IBAction func didTapDisconnect(_ sender: AnyObject) {
GIDSignIn.sharedInstance().disconnect()
statusText.text = "Disconnecting"
}
// Toggle auth
func toggleAuthUi() {
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
signInButton.isHidden = true
signOutButton.isHidden = false
disconnectButton.isHidden = false
} else {
signInButton.isHidden = false
signOutButton.isHidden = true
disconnectButton.isHidden = true
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
deinit {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name(rawValue: "ToggleAuthUINotification"),
object: nil)
}
#objc func receiveToggleAuthUINotification(_ notification: NSNotification) {
if notification.name.rawValue == "ToggleAuthUINotification" {
self.toggleAuthUi()
if notification.userInfo != nil {
guard let userInfo = notification.userInfo as? [String:String] else {return}
self.statusText.text = userInfo["statusText"]!
}
}
}
}
AppDelegate:
import UIKit
import GoogleSignIn
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
// Did Finished Launching
func application (_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Init Sign in
GIDSignIn.sharedInstance().clientID = "XXXXXXXXX"
GIDSignIn.sharedInstance().delegate = self
return true
}
// Open URL
func application (_app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool {
return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
NotificationCenter.default.post(
name: Notification.Name(rawValue: "ToggleAuthUINotificiation"), object: nil, userInfo: nil)
} else {
// User Stuff
let userID = user.userID
let idToken = user.authentication.idToken
let fullName = user.profile.name
let givenName = user.profile.givenName
let familyName = user.profile.familyName
let email = user.profile.email
NotificationCenter.default.post(
name: Notification.Name(rawValue: "ToggleAuthUINotification"),
object: nil,
userInfo: ["statusText": "Signed in user:\n\(fullName)"])
}
}
public func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Disconnect the user
NotificationCenter.default.post(
name: Notification.Name(rawValue: "ToggleAuthUINotification"),
object: nil,
userInfo: ["statusText": "User has disconnect."])
}
}
tl;dr: If using a non-UIViewController as a uiDelegate - check if Xcode is warning you that "Instance method [...] nearly matches optional requirement [...]" and use the Fix-Its. Make sure your functions match the required signature exactly.
I had the same error after following the sample code.
In my case the setup was a bit different, so I am not sure if my solution is applicable to your question - but because this StackOverflow answer shows up in Google Search, I thought I'd provide my resolution here for any people stumbling upon it:
I needed a non-UIVIewController subclass to be the Google Sign In uiDelegate. As the documentation and the error state, this means you need to implement some methods. I had implemented them, so it was strange that things were crashing.
In my case, the issue was that the copy-pasted code from Google's documentation was not exactly matching the Swift function signatures. In fact, I had warnings in Xcode, saying "Instance method [...] nearly matches optional requirement [...]". I used the auto Fix-Its by Xcode (it was stuff such as Error instead of NSError, or _ before a function's argument.)
Then it all worked.
I am working on a test project in Swift 3. I am trying to pass textField string from one class to another class using NotificationCenter. I am trying to workout the answer from this link: pass NSString variable to other class with NSNotification and how to pass multiple values with a notification in swift
I tried few answers from the above link but nothing worked.
My code:
//First VC
import UIKit
extension Notification.Name {
public static let myNotificationKey = Notification.Name(rawValue: "myNotificationKey")
}
class ViewController: UIViewController {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func sendData(_ sender: AnyObject) {
let userInfo = [ "text" : textView.text ]
NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo)
}
}
//SecondVC
import Foundation
import UIKit
class viewTwo: UIViewController {
#IBOutlet weak var result: UILabel!
override func viewDidLoad() {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.notificationReceived(_:)), name: .myNotificationKey, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .myNotificationKey, object: nil)
}
func notificationReceived(_ notification: Notification) {
guard let text = notification.userInfo?["text"] as? String else { return }
print ("text: \(text)")
result.text = text
}
}
I am not sure whats wrong with the code. Above, code originally marked as a answered which, I found from the first link. Code been converted to Swift.
Don't use object parameter to pass data. It is meant to filter notifications with the same name, but from a particular object. So if you pass some object when you post a notification and another object when you addObserver, you won't receive it. If you pass nil, you basically turn off this filter.
You should use userInfo parameter instead.
First, it is better to define notification's name as extension for Notification.Name. This approach is much safer and more readable:
extension Notification.Name {
public static let myNotificationKey = Notification.Name(rawValue: "myNotificationKey")
}
Post notification:
let userInfo = [ "text" : text ]
NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo)
Subscribe:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.notificationReceived(_:)), name: .myNotificationKey, object: nil)
}
Unsubscribe:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .myNotificationKey, object: nil)
}
Method to be called:
func notificationReceived(_ notification: Notification) {
guard let text = notification.userInfo?["text"] as? String else { return }
print ("text: \(text)")
}
Pass text using userInfo which is a optional Dictionary of type [AnyHashable:Any]? in Swift 3.0 and it is [NSObject : AnyObject]? in swift 2.0
#IBAction func sendData(_ sender: UIButton) {
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: ["text": textValue.text])
print(textValue) // textValue printing
}
in viewDidLoad
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(self. incomingNotification(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
and in incomingNotification
func incomingNotification(_ notification: Notification) {
if let text = notification.userInfo?["text"] as? String {
print(text)
// do something with your text
}
}
In your sendData method pass textField.text into Notification object and in your incomingNotification do this:
guard let theString = notification.object as? String else {
print("something went wrong")
return
}
resultLabel.text = theString
You can also use blocks to pass data between controllers.
Use dispatchQueue because your notification is posting before your view load. Therefore just give delay in your notification Post.
#IBAction func sendData(_ sender: AnyObject) {
let userInfo = [ "text" : textView.text ]
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo) }
}
Just use NotificationCenter to send and receive the notification that state changed. Pass the data through some a data model such as an ObservableObject (particularly if you're bridging between SwiftUI and UIKit). Here's are a couple of extension that make it pretty simple for lightweight inter-component signaling without the cumbersome forgettable semantics of NotificationCenter. (Of course you define your own Notification.Name constants to be meaningful to your purpose).
extension Notification.Name {
static let startEditingTitle = Notification.Name("startEditingTitle")
static let stopEditingTitle = Notification.Name("stopEditingTitle")
}
extension NotificationCenter {
static func wait(_ name : Notification.Name) async {
for await _ in NotificationCenter.default.notifications(named: name) {
break;
}
}
static func post(_ name : Notification.Name) {
NotificationCenter.default.post(name: name, object: nil)
}
#discardableResult static func postProcessing(_ name: Notification.Name, using block: #escaping (Notification) -> Void) -> NSObjectProtocol {
NotificationCenter.default.addObserver(forName: name, object: nil, queue: OperationQueue.main, using: block)
}
}
To post a notification is as simple as:
NotificationCenter.post(.startEditingTitle)
And to receive the notification elsewhere:
NotificationCenter.postProcessing(.startEditingTitle) (_ in {
print("Started editing title")
}
Or to just wait for the notification instead of asynchronously handling it:
NotificationCenter.wait(.startEditingTitle)
I have an app using Reachability library to detect when wifi is turned on while the app is active. I have the following code sample below in my view controller. The view controller is loaded when the app becomes active. When I run this code on my iPhone the selector method is not called. What's wrong with this code and how do I fix it?
let reachability = Reachability.reachabilityForInternetConnection()
override func viewDidLoad() {
super.viewDidLoad()
println("view did load")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: "ReachabilityChangedNotification", object: reachability)
reachability.startNotifier()
}
func reachabilityChanged(note: NSNotification) {
// I don't see this line printed out!!!
println("selector method called")
let reachability = note.object as! Reachability
if reachability.isReachable() {
println("yay internet is on")
} else {
println("internet is off")
}
}
I'd like to know when dictation has end (ideally also when it started).
My UIViewController which includes the UITextView conforms to the UITextInputDelegate protocol.
To make it work I had to subscribe to the UITextInputCurrentInputModeDidChangeNotification
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
}
and add the delegate there (it didn't work simply adding it to the viewDidLoad())
func changeInputMode(sender : NSNotification) {
textView.inputDelegate = self
}
Starting and stopping dictation the UITextInput now correctly calls the required delegate methods:
func selectionWillChange(textInput: UITextInput){ }
func selectionDidChange(textInput: UITextInput){ }
func textWillChange(textInput: UITextInput){ }
func textDidChange(textInput: UITextInput){ }
However what doesn't get called is
func dictationRecordingDidEnd() {
println("UITextInput Dictation ended")
}
Why? How can I get notified/call a method on dictation having ended?
Okay, here's what worked for me not using the UITextInput protocol but the UITextInputCurrentInputModeDidChangeNotification instead.
func changeInputMode(sender : NSNotification) {
var primaryLanguage = textView.textInputMode?.primaryLanguage
if primaryLanguage != nil {
var activeLocale:NSLocale = NSLocale(localeIdentifier: primaryLanguage!)
if primaryLanguage == "dictation" {
// dictation started
} else {
// dictation ended
}
}
}