【发布时间】:2011-09-24 19:40:23
【问题描述】:
如果用户无法通过 WI-FI 或 WWAN 连接到 google.com,我创建了一条错误消息。我没有收到任何编码错误,所以似乎没有任何问题。我正在使用访问 twitter.com 的 UIWebview。问题可能是我试图在视图加载而不是 UIWebview 时显示错误,但我不确定。我遇到的另一个问题是我无法在 TwitterViewController.h 文件中将“可达性”识别为名称。这是代码:
TwitterViewController.h
#import <UIKit/UIKit.h>
#import "Reachability.h"
@interface TwitterViewController : UIViewController {
Reachability *reachability;
}
@end
TwitterViewController.m
#import "TwitterViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#include <netinet/in.h>
#import "TwitterView.h"
@implementation TwitterViewController
- (void)viewDidLoad {
[super viewDidLoad];
Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
UIAlertView *NEalert = [[UIAlertView alloc] initWithTitle: @"Error"
message: @"Could not load the webpage. Please check your internet connection."
delegate: nil
cancelButtonTitle: @"Dismiss"
otherButtonTitles: nil];
[NEalert show];
[NEalert release];
}
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
[super dealloc];
}
#pragma mark -
#pragma mark Initialisation
- (id)init {
self = [super init];
if (self) {
self.hidesBottomBarWhenPushed = YES;
UINavigationBar objects
self.title = @"Twitter";
UIView *twitterView = [[TwitterView alloc] initWithParentViewController:self];
self.view = twitterView;
[twitterView release];
}
return self;
}
#pragma mark -
#pragma mark Action Methods
- (void)twitterBUTTONAction {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"NFAGaming's Twitter"
message: @"To follow NFAGaming on twitter, go to: twitter.com/NFAGaming"
delegate: nil
cancelButtonTitle: nil
otherButtonTitles: @"Ok", nil];
[alert show];
[alert release];
}
#pragma mark -
#pragma mark UIViewController Delegates
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
@end
编辑: 这就是最终对我来说完美的工作
{{Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if (internetStatus == NotReachable) {
UIAlertView *tNEalert = [[UIAlertView alloc] initWithTitle: @"Error"
message: @"Internet Connection Required"
delegate: self
cancelButtonTitle: @"Dismiss"
otherButtonTitles: nil];
[tNEalert show];
[tNEalert release];
}
else {
UIView *twitterView = [[TwitterView alloc] initWithParentViewController:self];
self.view = twitterView;
[twitterView release];
}
}}
【问题讨论】:
标签: iphone ios xcode networking reachability