2012-10-01 13 views
19

में गम्यता का पता लगाने मैं संभावना लागू करने के लिए अलग तरह से परीक्षण किया गया है जब एप्लिकेशन पृष्ठभूमि में है कि उपकरण वापस इंटरनेट प्राप्त यदि ऐसा है तो पहले कोड मैं परीक्षण एप्पल गम्यता नमूना कोड http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.htmlपृष्ठभूमि

था पता करने के लिए लेकिन जब यह पृष्ठभूमि पृष्ठभूमि में ऐप है तो यह कोड इंटरनेट स्थिति को सूचित नहीं करता है। इसलिए मैं भी folowing कोड की कोशिश की और जब अनुप्रयोग अग्रभूमि (एप्पल गम्यता नमूना कोड के रूप में ही)

- (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; 
    } 
} 
} 

मेरा प्रश्न है करने के लिए पृष्ठभूमि राज्य से शुरू की है यह काम:क्या रास्ता ही सूचना प्राप्त करना, जब इंटरनेट जब ऐप पृष्ठभूमि में होता है तो राज्य बदल जाता है?

उत्तर

2

मुझे विश्वास नहीं है कि पृष्ठभूमि में रहते समय अभिगम्यता अधिसूचनाएं प्राप्त करने का एक तरीका है। इसे संभालने का सही तरीका ऐपडिलेगेट्स (शून्य) अनुप्रयोग में पहुंचने की जांच करना होगा। (एंटरप्राइजेशन *) एप्लिकेशन।

पृष्ठभूमि ऐप्स पर प्रतिक्रिया करने वाली एकमात्र पृष्ठभूमि घटना पुश अधिसूचनाओं की प्राप्ति है, और ऐसा इसलिए है क्योंकि ओएस उन्हें ऐसा करने के लिए जागृत करता है, और फिर जब उपयोगकर्ता अनुरोध करता है।

-2

आप ऐप मल्टीटास्किंग (वीओआईपी, स्थान या ऑडियो) होना चाहिए। और इस तरह, पृष्ठभूमि में ऐप होने पर आप नेटवर्क को बदल सकते हैं।

+1

हाय, सबसे पहले आपको ध्यान रखना चाहिए कि ऐप्पल बहुत सख्त है कि किस प्रकार के ऐप्स पृष्ठभूमि निष्पादन करने में सक्षम होंगे। एप्पल द्वारा इस दस्तावेज़ पर Pls पर एक नज़र डालें। https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html। इसके अलावा, कृपया यह बताने की कोशिश करें कि आपका ऐप क्या करने का प्रयास कर रहा है। शायद हम एक बेहतर दृष्टिकोण की पेशकश कर सकते हैं! –

7

लाइव यदि नेटवर्क कनेक्शन बदल आप बदल नहीं सकते, बहुत ही बेहतरीन आप इसे काम करने के लिए क्या कर सकते हैं Capabilities से Background Fetch मोड का उपयोग करने के लिए है। आप समय अंतराल के लिए जितनी बार पूछने के लिए के रूप में आप जितनी जल्दी बेहतर तो मैं सुझाव है application:didFinishLaunchingWithOptions: कर सकते हैं की जरूरत है तो फिर enter image description here

और आप इस लाइन लगाने की जरूरत है: Firstable आप पृष्ठभूमि मोड के लिए चेकबॉक्स को चेक करने की जरूरत है

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

UIApplicationBackgroundFetchIntervalMinimum यह जितनी बार संभव है, लेकिन यह डॉक्स से फ़ेच के बीच सेकंड की सही संख्या नहीं है:

छोटी से छोटी लाने पूर्णांक प्रणाली द्वारा समर्थित erval।

और फिर जब पृष्ठभूमि आप विधि के साथ 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); 
} 

यह सब आईओएस 7.0 या उच्चतर में काम करेंगे।

+0

मैंने समझाया गया है .. लेकिन अभी भी प्रदर्शन करें FetchWithCompletionHandler कॉल नहीं करता है और पहुंच क्षमता की जांच करने में सक्षम नहीं हो सका। – CKT

संबंधित मुद्दे