在 Objective C 中检查 Internet 连接 [英] Checking For Internet Connectivity in Objective C

查看:26
本文介绍了在 Objective C 中检查 Internet 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习斯坦福 iPhone 编码课程,目前正在使用 Twitter API.我想做的是准确处理两种错误情况:一种是用户名无效时,另一种是设备当前未连接到互联网时.不幸的是,就目前而言,我所能猜测的最好结果是 API 的返回值是否为零 - 这两种情况都是如此.

I've been working through the Stanford iPhone Coding course and currently hooking into the Twitter API. What I'd like to do is accurately handle two error conditions: One for when the username is invalid, and another for when the device is not currently connected to the internet. Unfortunately, as it stands, the best I can surmise is whether or not the return from the API is nil or not - which is the case for both conditions.

我正在寻找的是一两行代码,可以在尝试获取任何远程数据之前检查连接.我可以仔细阅读 Apple 文档,但我想:为什么不为了我和其他人的利益向你们提出这个问题?

What I'm looking for is a line or two of code that can check for a connection before attempting any fetch of remote data. I could sift through the Apple documentation but I figured: Why not put the question to you guys for my benefit and perhaps that of others?

附加信息:在 XCode 中使用 Objective-C 和 iPhone SDK.

Additional info: Using Objective-C and the iPhone SDK in XCode.

推荐答案

查看 Apple 的示例代码.Reachability 项目展示了如何检测连接.

Take a look at Apple's sample code. The Reachability project shows how to detect a connection.

http://developer.apple.com/iphone/library/示例代码/Reachability/index.html

这篇关于在 Objective C 中检查 Internet 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆