Posts

Post marked as solved
2 Replies
3.4k Views
Hi! I am using NWPathMonitor to check the network connection, and only want it to monitor in one of the screens of my app. Hence, I created a NWPathMonitor in the corresponding view controller and attempt to turn on the monitor when the view appears, and turn off when the view disappear. I tried to use the .start and .cancel methods but couldn't get it done. Below are two versions of my experiment codes, and each of them get an issue. Can you help me to look at it?Thank you!Experiment code 1: cancel but network connection is still being checked. Not sure why I can still see the popup message warning the network connection. private let networkMonitor: NWPathMonitor = NWPathMonitor() private var networkMonitorQueue: DispatchQueue = DispatchQueue(label: "NetworkMonitorQueue") override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.networkMonitor.start(queue: networkMonitorQueue) self.networkMonitor.cancel() self.networkMonitor.pathUpdateHandler = { p in if p.status != .satisfied { DispatchQueue.main.sync { Utilities.DisplayMessage(vController: self as UIViewController, mTitle: "Network Connection Error", mText: "This device is not connected to Internet. Stock reports will not be updated.") } } } }Experiment code 2: not getting popup message about network any more, but in the XCode output I got error message when running the second .start command: nw_path_evaluator_set_queue Client error: set queue after startingprivate let networkMonitor: NWPathMonitor = NWPathMonitor() private var networkMonitorQueue: DispatchQueue = DispatchQueue(label: "NetworkMonitorQueue") override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.networkMonitor.start(queue: networkMonitorQueue) self.networkMonitor.cancel() self.networkMonitor.start(queue: networkMonitorQueue) self.networkMonitor.cancel() self.networkMonitor.pathUpdateHandler = { p in if p.status != .satisfied { DispatchQueue.main.sync { Utilities.DisplayMessage(vController: self as UIViewController, mTitle: "Network Connection Error", mText: "This device is not connected to Internet. Stock reports will not be updated.") } } } }
Posted
by ssshhh.
Last updated
.
Post marked as solved
2 Replies
3.7k Views
Hi! I am trying to use the NWPathMonitor to check the network connectivity on my iPhone. However the NWPath object from the monitor is always has status property being "unsatisfied" regardless whether I turn the wifi and cell data on or off. Below is the simple code I tried. For simplicity I make the requiredInterfaceType be just WIFI. However, does matter whether I turn WIFI on or off, the label display is always "Unsatifsfied"Can you help me to find whey?Thank you!class ViewController: UIViewController { var mon: NWPathMonitor = NWPathMonitor(requiredInterfaceType: .wifi) @IBOutlet weak var statusLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func detect(_ sender: Any) { let cp : NWPath = mon.currentPath if cp.status == .satisfied { statusLabel.text = "Satisfied" } else if cp.status == .requiresConnection { statusLabel.text = "Requires Connection" } else if cp.status == .unsatisfied { statusLabel.text = "Unsatisfied" } else { statusLabel.text = "Unknown" } } }
Posted
by ssshhh.
Last updated
.
Post marked as solved
1 Replies
514 Views
Hi! I am trying to learn about allowing display information in pages by observing the out-of-box Page-Based App application template. I found that in this template, a UIPageViewController object is handled programmatically:-- created in viewDidLoad() function of initial UIViewController-- added to the sub views of the initial UIViewControllerI tried to simplify this by directly drag-n-drop a UIPageViewController on to storyboard, and set it as the initial UIViewController. However, when I run the application I found that it cannot even reach the break point I set in the viewDidLoad() function of theUIPageViewController, and the whole app does not display anything other than black screen on my phone.Why using UIPageViewController as initial view controller does not work? I think there must be a reason, otherwise the out-of-box template in XCode should have avoid the complexity of an additional view controller.Thank you!
Posted
by ssshhh.
Last updated
.
Post marked as solved
2 Replies
5.5k Views
Hi Developers,I am curious the difference between IndexPath.row and IndexPath.item. In my experiment result of iOS code using table view, these two properties had identical values at all the time. After some search I found this page telling they are actually the functionally the same, while people usually use "row" for table view and "item" for collection view for style convention.https://stackoverflow.com/questions/14765730/nsindexpath-item-vs-nsindexpath-rowI want to double check: is this true? are they really functionally identical? if identical what made them appear in the API together?Usually everything has a reason to exist ... these two properties together seem strange to me, so I just want to double check.Thank you!
Posted
by ssshhh.
Last updated
.
Post marked as solved
5 Replies
617 Views
Hi! I encountered a way of specifying completion handler that I don't understand. I am trying to find some swift syntax. Can you help me to find a link or some hint to understand how this works?Following the example in https://www.raywenderlich.com/3244963-urlsession-tutorial-getting-startedI got the following code working and printing the expected message. However, the way it specify completion handler is not to pass the closure into the "dataTask" method call as a parameter, but just placing after the method call.var sharedSession : URLSession = URLSession.shared var googUrl:URL = URL(string: "https://www.google.ca/finance")! var task1: URLSessionDataTask = sharedSession.dataTask(with: googUrl) { data, response, error in print("task 1 completed.") }I looked at the reference at https://developer.apple.com/documentation/foundation/urlsessionSeems the right way to specify the handler is passing it as parameter as followvar task1: URLSessionDataTask = sharedSession.dataTask(with: googUrl, completionHandler: { data, response, error in print("task 1 completed.") })Why the former works?Thank you!
Posted
by ssshhh.
Last updated
.
Post marked as solved
4 Replies
668 Views
In the XCode Playground editor, there is result views at the right hand side after the code is executed in playground. I notice that some of the displayed variable values has a description "fixed". What does "fixed" mean in this context?Thank you!
Posted
by ssshhh.
Last updated
.