我正在尝试从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解决了这个问题。现在,似乎没有用!
有什么想法吗?
答案 0 :(得分: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];
}
}
答案 1 :(得分:1)
我不认为只要应用程序启动,Reachability就会保证正确。我们的解决方案是监控通知,等到报告连接成功,然后从那里处理事情。
我们的应用程序还会在 internetReachable 上调用 startNotifier ,然后记下 - (void)reachabilityChanged:(NSNotification )theNotification * callback中的可访问性。我们在一个管理器类中执行此操作,然后我们可以检查当前的可访问性,并且当可达性发生更改时,它还可以向应用程序的其余部分发送通知(如果需要)。