简体   繁体   English

Objective-C中的异常/错误处理(iPhone应用程序)

[英]Exception/Error handling in Objective-C (iPhone app)

I actually have two questions regarding exception/error handling in the iPhone app that I am making: 我实际上有两个关于我正在制作的iPhone应用程序中的异常/错误处理的问题:

  1. The app uses Internet, but when there's no connection, the app just dies (during launch). 该应用程序使用Internet,但是当没有连接时,应用程序就会死掉(在启动期间)。 How can I handle this to print some infomsg to the user, instead of just getting thrown back to the springboard? 我该如何处理这个问题来向用户打印一些infomsg,而不是仅仅被扔回跳板?

  2. Can someone show me an example of how to handle for instance a "page not found" or "no contact with server" error, so I can give some sort of info to the user in the same way as above? 有人能告诉我一个如何处理例如“找不到页面”或“没有联系服务器”错误的例子,所以我可以用与上面相同的方式向用户提供某种信息吗?

For crashes, the first step is to use error messages and the debugger to figure out what call is causing the problem. 对于崩溃,第一步是使用错误消息和调试器来确定导致问题的呼叫。 If the problem is caused by an uncaught exception, read this Apple article on exception handling . 如果问题是由未捕获的异常引起的,请阅读关于异常处理的 Apple文章。 The specific answer really depends on your code and exactly what is causing the crash, so I won't speculate about a particular solution. 具体答案实际上取决于您的代码以及导致崩溃的确切原因,因此我不会推测特定的解决方案。

As far as detecting server error response codes (such as 404), that's more specific to WebKit. 至于检测服务器错误响应代码(例如404),这更具体到WebKit。 I assume you're using UIWebView on iPhone, and you've probably noticed that none of the primary methods return errors. 我假设你在iPhone上使用UIWebView ,你可能已经注意到没有一个主要方法返回错误。 This is because it uses a delegate model to report progress or errors asynchronously. 这是因为它使用委托模型异步报告进度或错误。 (It makes sense because you don't want your UI code to be at the mercy of a slow-loading (or non-existent) webpage. To be notified of such errors, there are a few steps. (这是有道理的,因为您不希望您的UI代码受到缓慢加载(或不存在)网页的支配。要获得此类错误的通知,有几个步骤。

  1. Adopt the UIWebViewDelegate protocol, usually in the same class that will start the webpage load for convenience. 采用UIWebViewDelegate协议,通常在同一个类中启动网页加载以方便使用。
  2. Set that object as the delegate of the UIWebView instance. 将该对象设置为UIWebView实例的委托。 (It has a delegate property, so you can use something like either uiView.delegate = self or [uiView setDelegate:self] based on what you prefer.) (它有一个delegate属性,因此您可以根据自己的喜好使用uiView.delegate = self[uiView setDelegate:self] 。)
  3. Implement the webView:didFailLoadWithError: method in that class. 在该类中实现webView:didFailLoadWithError:方法。 (You can be notified when the load finishing by implementing webViewDidFinishLoad: as well.) This is where you include the logic of what should happen when an error occurs. (通过实现webViewDidFinishLoad:可以在完成加载时通知您。)这是您包含发生错误时应该发生的事情的逻辑的地方。

I didn't see any detailed documentation on the content of any particular errors handed back via this delegate method, but it's a standard NSError object, and I recommend checking out the contents by calling its methods, such as -localizedDescription and -userInfo . 我没有看到有关通过此委托方法传回的任何特定错误的内容的任何详细文档,但它是标准的NSError对象,我建议通过调用其方法来检查内容,例如-localizedDescription-userInfo

Here is some sample code with #import statements excluded for brevity. 下面是一些示例代码,为简洁起见,排除了#import语句。

MyClass.h MyClass.h

@interface MyClass : NSObject <UIWebViewDelegate> {
  IBOutlet UIWebView* myWebView;
}
-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError *)error;
@end

MyClass.m MyClass.m

@implementation MyClass
- (id) init {
  if ((self = [super init]) == nil)
    return nil;
  // initialize myWebView
  myWebView.delegate = self;
  return self;
}

- (void) webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
  ...
}
@end

Testing for a connection is pretty easy... 测试连接非常简单......

NSString * test = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];
if (test == nil) {
  //display an alertview saying you don't have an internet connection
}

Using a URL to test for a connection is not a good idea, it is not robust enough to determine if the internet connection is down, the website is down or some other network issue etc and above all it adds an overhead to the call as far as network traffic. 使用URL来测试连接不是一个好主意,它不够健壮,无法确定互联网连接是否已关闭,网站是否已关闭或其他网络问题等等,最重要的是它会增加呼叫的开销。作为网络流量。

Look at the Reachability demo on the Apple site, it uses the correct way to determine connectivity, including whether you are on wifi etc. 看看Apple网站上的Reachability演示,它使用正确的方式来确定连接性,包括你是否在wifi等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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