2

我正在尝试从 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 解决了它。现在看来,这行不通!

任何的想法?

4

2 回答 2

3

kReachabilityChangedNotification您可以像这样为通知添加观察者:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

然后启动通知器:

[[Reachability reachabilityForLocalWiFi] startNotifier];

在您的通知侦听器中,您可以测试 wifi 是否可用:

- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    if(netStatus == ReachableViaWiFi) {
        // wifi available
        [self askForDownloadContentsAtFirstStart];
    }
}
于 2012-10-25T01:34:03.223 回答
1

我不认为应用程序启动后可达性就保证是正确的。我们的解决方案是监控通知,等到它报告连接成功,然后从那里处理事情。

我们的应用程序还在internetReachable上调用startNotifier,然后在-(void)reachabilityChanged:(NSNotification )theNotification* 回调中注意可达性。我们在管理器类中执行此操作,然后我们可以检查当前的可达性,并且当可达性发生变化时,它还可以向应用程序的其余部分(如果需要)发送通知。

于 2012-10-24T15:29:06.093 回答