How to stop uiactivityindicator when i connect to internet - ios

i have one app which contains many map view. And i need to check internet connect is true or false. If false one uialert message will show and uiactivity Indicator will show ( will start).... its working fine..
But when i suddenly connect the internet , that uiactivity indicator in not stoping. Still getting run.
Here my code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ActiviyuInc.hidden = true
showmethod()
}
func showmethod () {
if Reachability.isConnectedToNetwork() == false {
print("Internet connection FAILED")
let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
if Reachability.isConnectedToNetwork() == false {
ActiviyuInc.hidden = false
ActiviyuInc.startAnimating()
}
else {
ActiviyuInc.hidden = true
ActiviyuInc.stopAnimating()
}
}
}
When my app in run, that time when i connect to internet , still my uiactivityIndicator is not stoping..
Help me out !!!

Try stopping Activity on main thread,
dispatch_async(dispatch_get_main_queue()) { () -> Void in
ActiviyuInc.hidden = true
ActiviyuInc.stopAnimating()
}

just do this.
if Reachability.isConnectedToNetwork() == false {
print("Internet connection FAILED")
let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
ActiviyuInc.hidden = false
ActiviyuInc.startAnimating()
} else {
ActiviyuInc.hidden = true
ActiviyuInc.stopAnimating()
}

Related

Load local file if no internet connection; function calls itself again and again

This is my code, explanation is below:
func webViewDidStartLoad(homewebview: UIWebView) {
if Reachability.isConnectedToNetwork() == true {
print("Internet connection: OK")
} else {
print("Internet connection: FAILED")
let path: String = NSBundle.mainBundle().pathForResource("file", ofType: "html")!
let url: NSURL = NSURL.fileURLWithPath(path)
let request: NSURLRequest = NSURLRequest(URL: url)
homewebview.loadRequest(request)
}
}
When clicking a hyperlink webViewDidStartLoad will called.
When there's an internet connection it prints Internet connection: OK.
When there's no internet connection it should print Internet connection: FAILED and open file.html.
But when opening the local file it calls the whole function again, and again and again…like a never ending loop.
I want to check for internet connection when clicking a (hyper)link in the WebView. If there's no internet connection a local file should load in this WebView, without calling the function again.
How can I fix it? Does anybody have an idea?
Just rise a flag when you are loading a local page to skip the recursive routine.
var loadingLoacalPage = false
//..//
func webViewDidStartLoad(homewebview: UIWebView) {
if Reachability.isConnectedToNetwork() == true {
print("Internet connection: OK")
} else {
print("Internet connection: FAILED")
let alert = UIAlertView(title: "No internet connection", message: "Check internet connection.", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
if(!loadingLoacalPage){
let path: String = NSBundle.mainBundle().pathForResource("file", ofType: "html")!
let url: NSURL = NSURL.fileURLWithPath(path)
let request: NSURLRequest = NSURLRequest(URL: url)
homewebview.loadRequest(request)
loadingLoacalPage = true
}
}
}

Refresh animation doesn't stop with "self.refreshControl.endRefreshing()" on iOS 10

This is my code:
func pullToRefresh() {
if Reachability().connectionStatus() == .Online {
self.homewebview.reload()
} else {
self.refreshControl.endRefreshing()
let alert = UIAlertView(title: "No Internet connection", message: "Please check your Internet connection.", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
}
}
When internet connection is not available and the user pulls to refresh an alert should be shown and the animation should stop. That works great on iOS 9. But on iOS 10 Beta 2 the animation doesn't disappear. The user have to pull up to make it disappear. It that an iOS 10 bug or am I doing something wrong?

App crashes on Launch in Airplane Mode

I am currently using Ashley Mill's Reachability Class. If the application launches with network connectivity then I am able to toggle between connectivity availability without any issues and able to display a network connectivity Alert Controller properly. However if the application is launched when the app starts without internet connection/on airplane mode it abruptly crashes.
override func viewDidLoad()
{
super.viewDidLoad()
setUpReachability (nil)
}
func setUpReachability(hostName: String?)
{
do
{
let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
self.reachability = reachability
try! self.reachability?.startNotifier()
}
catch ReachabilityError.FailedToCreateWithAddress(let address)
{
print("\(address)")
return
} catch {}
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
}
func reachabilityChanged(notification: NSNotification)
{
let reachability = notification.object as! Reachability
if reachability.isReachable()
{
if reachability.isReachableViaWiFi()
{
connected = true
}
else
{
connected = true
}
}
else
{
let alert = UIAlertController( title: "No Network Connection Available", message:"Try Again", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Will Do!" , style: .Default) { _ in } )
presentViewController ( alert, animated: true ) {}
connected = false
}
}
What can be done to allow the iPhone application to launch and display an alert saying there is no network connection rather than abruptly crash?
Error Message:
fatal error: unexpectedly found nil while unwrapping an Optional value
But I would think that reachability changed would catch this in the else statement and pop the error message up?
Shouldn't the else in the reachability.isReachableViaWiFi() if statement be:connected = false ?
The error was that I was in fact trying to download data at the launch of the app instead of first allowing the initialization of the app to finish to then send a request to the server to access information.

How to reconnect to Openfire server after unlocking the iPhone screen?

I am using XMPPFramework. My XMPP connection is always closed when I locked the iPhone screen.
I need to reconnect to my Openfire server when I unlock the screen.
Here is how I used XMPPReconnect (in Swift):
func xmppStreamDidAuthenticate(sender: XMPPStream) {
let xmppReconnect = XMPPReconnect()
xmppReconnect.activate(sender)
xmppReconnect.addDelegate(self, delegateQueue: dispatch_get_main_queue())
}
However, it seems it never reconnects when I unlock the screen.
Am I using XMPPReconnect correctly?
How do I achieve my target?
You have to write code in your appdelegate in applicationDidBecomeActive. becuase when you unlock the screen this method will call, and in this method you have to call connect method to openfire....
func applicationDidBecomeActive(application: UIApplication) {
self.connect()
}
func connect() -> Bool {
println("connecting")
setupStream()
var jabberID: String? = "YOUR_JID"
var myPassword: String? = "YOUR_PASSWORD"
var server: String? = "YOUR_HOST"
xmppStream!.hostName = "YOURHOST_NAME"
xmppStream!.hostPort = 5222
if let stream = xmppStream {
if !stream.isDisconnected() {
return true
}
if jabberID == nil || myPassword == nil {
println("no jabberID set:" + "\(jabberID)")
println("no password set:" + "\(myPassword)")
return false
}
stream.myJID = XMPPJID.jidWithString(jabberID)
password = myPassword
var error: NSError?
if !stream.connectWithTimeout(XMPPStreamTimeoutNone, error: &error) {
var alertView: UIAlertView? = UIAlertView(title:"Error", message: "Cannot connect to \(error!.localizedDescription)", delegate: nil, cancelButtonTitle: "Ok")
alertView!.show()
return false
}
}
return true
}
Hope this will help!

How to create a segue on condition [duplicate]

This question already has an answer here:
segue transition with condition storyboard
(1 answer)
Closed 7 years ago.
I have a login page backed up by Parse. I want to know how to create a segue only if the login has been confirmed through the Parse database, and then direct the user to a new View Controller.
This is the code for the login button:
#IBAction func logginginAction(sender: AnyObject) {
var username = self.usernameField.text
var password = self.passwordField.text
if (count(username.utf16) < 4 || count(password.utf16) < 5 ) {
var alert = UIAlertView(title: "Invalid", message: "Username/Password is too short!!", delegate: self, cancelButtonTitle: "OK")
alert.show()
}
else {
self.actInd.startAnimating()
PFUser.logInWithUsernameInBackground(username, password: password, block: { (user, error) ->
Void in
self.actInd.stopAnimating()
if ((user) != nil) {
}else {
var alert = UIAlertView(title: "Invalid", message: "Please recheck the information you just entered", delegate: self, cancelButtonTitle: "OK")
alert.show()
}
})
}
}
This is simple.
Connect your button to an IBAction, not directly to a segue.
Connect your segue from the view controller, not from the button. Give it a unique identifier.
In your IBAction method check the conditions you want to check, and if they are met, invoke your segue using performSegueWithIdentifier:sender:

Resources