我正在尝试从 AppDelegate 中的 A 类执行选择器。
我设置了一个警报,提示用户在购买应用程序后不久立即下载所有内容并缓存它们以供以后阅读。
我在 A 类中有确切的东西,它工作得非常好。
现在,当我在首次启动时尝试执行此操作时,可达性显示我处于离线状态,而我实际上在线。
内容只能通过 WiFi 下载,不能通过 3G 下载(数据计划保存),但是,即使我在自己的 WiFi 网络中,它也会说我离线。
我在 App Delegate 中使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"mainLaunch"]){
[self performSelector:@selector(askForDownloadContentsAtFirstStart) withObject:nil afterDelay:0.5];
[defaults setObject:[NSDate date] forKey:@"mainLaunch"];
//...
}
-(void)askForDownloadContentsAtFirstStart{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Download contents" message:@"blahblahblah" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]autorelease];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}else{
aVc = [[AViewController alloc]init];
[aVc performSelector:@selector(offlineDownload:)];
}
}
在我的 aVc 中有以下一个:
- (void)viewWillAppear:(BOOL)animated
{
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(offlineDownload:)
name:kReachabilityChangedNotification object:nil];
}
- (IBAction)offlineDownload:(id)sender{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Blahblahblah" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]autorelease];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
else{
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
NSLog(@"Offline");
}
case (ReachableViaWWAN):
{
NSLog(@"3G");
}
case (ReachableViaWiFi):{
HUD = [[MBProgressHUD showHUDAddedTo:key_Window animated:YES]retain];
HUD.delegate = self;
HUD.dimBackground = YES;
HUD.labelText = NSLocalizedString(@"Connecting",@"Connecting...");
NSURL *URL = [NSURL URLWithString:NSLocalizedString(@"plistUrl",@"")];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
break;
}
default:
break;
}
}
}
这个问题再次发生,但我通过保留 internetReachable 解决了它。现在看来,这行不通!
任何的想法?