简体   繁体   中英

How check WatchOS 2 connected (reachability) to the internet?

How to Apple Watch (WatchOS 2) check the connection to the internet?

In AFNetworkReachabilityManager have: #if !TARGET_OS_WATCH

watchOS 2

In watchOS 2, the Apple Watch can communicate directly with the Internet.

So you could use:

1- Classes like NSURLSession to communicate with an URL.

2- Frameworks from social networking size

And many other things to connect to the Internet.

When you use them, two things happen:

1- The connection succeeds , which means the Apple Watch is connected to the Internet, either directly (Only Wi-Fi) or indirectly by iPhone (Wi-Fi or Cellular). Then you could use the Internet to do whatever you want.

2- The connection fails , which means there is no Internet, so you must prevent further attempts. You could use the following ways to catch errors like this:

A) In Obj-C, you could use NSError and pass an NSError object for the last parameter, which the error is saved in. Then you could access its description.

B) In Swift:

*) Swift 2: The new Do-Try-Catch structure makes it easy to catch errors, like this:

do {

    let result = try theFunction()

} catch let error as NSError {

    // print error description

}

*) Swift 1: The only way is like Obj-C and the use of optionals. To keep this answer more simple, I don't describe this outdated version anymore.

watchOS 1

In this version of watchOS, the only way of connecting to the Internet is by iPhone, as Apple Watch can't stand as an Internet communicator on its own, so you could use the functions that you need to communicate to iPhone with to determine the chance of being connected, and if it succeeds, you can try the other ways described before. To keep this answer more simple, I don't describe this outdated version anymore.

Conclusion

1- In watchOS 2, the Apple Watch can communicate directly with the Internet.

2- You could use NSURLSession or other frameworks to connect online.

3- There are different ways to catch errors on different versions of Swift and Obj-C, as described earlier in this answer.

4- In watchOS 1, the only way of connecting to the Internet is by iPhone, as Apple Watch can't stand as an Internet communicator on its own. This helps us a lot in this matter.

More Resources

1- NSURLSession Class Reference

2- NSError Class Reference

3- New Error Handling in Swift 2 Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM