現在、外部 API から XML を読み取る作業を行っています。
次のコードは正常に動作します。
NSError *error = nil;
NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
ただし、エラー処理 (「サーバーにアクセスできません」、「インターネット接続がありません」など) を追加したかったのです。そこで、Reachability のサンプル コードを少し参考にして、このブロックを配置しました。
if ([error code] == kCFURLErrorNotConnectedToInternet) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Internet connection",@"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
[self handleError:noConnectionError];
} else {
// otherwise handle the error generically
[self handleError:error];
}
handleError メソッドは、エラーの詳細を含む UIAlertView を表示するだけです。接続せずにアプリを実行すると、エラー メッセージが表示されるはずでした。ただし、「操作を完了できませんでした (Cocoa エラー 256)」というメッセージしか表示されません。これは、収集できる一般的な読み取りエラーです。
最初は単に TouchXML のメソッドかと思ったのですが、ご覧のとおり、これを NSString の initWithCONtentsOfURL に変更しました。
誰でもこの問題に光を当てることができますか?
ありがとう、
リッチ