Send data to server whenever internet connection i

2019-05-03 20:58发布

问题:

I am developing an iOS app,

What i want to do is, whenever user turned on/off his internet from setting app, i want to set some notification to device.

In short, i want to send some data (stored in NSUserDefaults) to server whenever internet connection is available(assuming internet connection appears after 2 3 days)....possible?

Please help and thanks in advance.

回答1:

Try this Apple example: https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

If you want this to work when app in background try background task

If you what your app doesn't suspend in background look at this: http://www.raywenderlich.com/29948/backgrounding-for-ios



回答2:

You may check Internet connectivity via Reachability class while your app is running in foreground or background state.

But if your app is closed then you can't detect network connection within your app. So its not possible to generate any notification by detecting network connection while app is closed.



回答3:

Apple write an demo for this.Just search reachability in Xcode Document and APIs(or in iOS developer library).And choose sample code.

I'm afraid you must use background task.Look up background task guide.But I think it's a little abuse for just check network.