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 を保持することで解決しました。今、それはうまくいかないようです!
何か案が?