20

アプリがバックグラウンドにあるときにデバイスがインターネットに戻るかどうかを知る可能性を実装するさまざまな方法をテストしてきたので、最初にテストしたコードはAppleの到達可能性のサンプルコードhttp://developer.apple.com/library/iosでした/#samplecode/Reachability/Introduction/Intro.html

ただし、このコードは、アプリがバックグラウンドにあるときにインターネットの状態を通知しません。したがって、次のコードも試してみましたが、アプリがバックグラウンド状態からフォアグラウンドに起動されたときに機能します (Apple の到達可能性のサンプル コードと同じです)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(checkNetworkStatus:)
                                             name:kReachabilityChangedNotification object:nil];

// Set up Reachability
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

 ...
 }


 - (void)applicationDidEnterBackground:(UIApplication *)application {

// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(checkNetworkStatus:)
                                             name:kReachabilityChangedNotification object:nil];

// Set up Reachability
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

}



- (void)checkNetworkStatus:(NSNotification *)notice {
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
    case NotReachable:
    {
        NSLog(@"The internet is down.");
        break;
    }
    case ReachableViaWiFi:
    {
        NSLog(@"The internet is working via WIFI");

        //Alert sound in Background when App have internet again
        UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
        if (notification) {
            [notification setFireDate:[NSDate date]];
            [notification setTimeZone:[NSTimeZone defaultTimeZone]];
            [notification setRepeatInterval:0];
            [notification setSoundName:@"alarmsound.caf"];
            [notification setAlertBody:@"Send notification internet back"];
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }


        break;
    }
    case ReachableViaWWAN:
    {
        NSLog(@"The internet is working via WWAN!");


        //Alert sound in Background when App have internet again
        UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
        if (notification) {
            [notification setFireDate:[NSDate date]];
            [notification setTimeZone:[NSTimeZone defaultTimeZone]];
            [notification setRepeatInterval:0];
            [notification setSoundName:@"alarmsound.caf"];
            [notification setAlertBody:@"Send notification internet back"];
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }

        break;
    }
}
}

私の質問は次のとおり です。アプリがバックグラウンドのときにインターネットの状態が変化したときに通知を受け取る方法は何ですか?

4

2 に答える 2

9

ライブネットワーク接続が変更された場合、変更することはできません。それを機能させるためにできる最善Background Fetchの方法は、からモードを使用することですCapabilities。まず、バックグラウンド モードのチェックボックスをオンにする必要があります。 ここに画像の説明を入力

次に、できるだけ頻繁に時間間隔を尋ねる必要があるので、私が提案し、次のapplication:didFinishLaunchingWithOptions:行を入力する必要があります。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

それUIApplicationBackgroundFetchIntervalMinimumは可能な限り頻繁ですが、ドキュメントからのフェッチ間の正確な秒数ではありません:

システムでサポートされている最小のフェッチ間隔。

そして、バックグラウンド フェッチが開始されたら、次AppDelegateのメソッドでチェックインできます。

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    NetworkStatus status = [reachability currentReachabilityStatus];

    switch (status) {
        case NotReachable: {
            NSLog(@"no internet connection");
            break;
        }
        case ReachableViaWiFi: {
            NSLog(@"wifi");
            break;
        }
        case ReachableViaWWAN: {
            NSLog(@"cellurar");
            break;
        }
    }
    completionHandler(YES);
}

これらはすべてiOS 7.0以降で動作します。

于 2016-02-24T23:33:13.137 に答える
2

バックグラウンドで到達可能性通知を受け取る方法はないと思います。これを処理する正しい方法は、AppDelegate の - (void)applicationWillEnterForeground:(UIApplication *) アプリケーションで到達可能性を確認することです。

バックグラウンド アプリが反応する唯一のバックグラウンド イベントは、プッシュ通知の受信です。これは、ユーザーが要求した場合にのみ OS がアプリを起動するためです。

于 2012-10-14T09:35:53.183 に答える