2013-11-04 33 views
1

didFinishLaunchingWithOptions定時器循環每隔1分鐘調用一個函數httpRequestUILocalNotification在背景10分鐘後沒有提示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //rest of code 

    NSTimer *notifyTimer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(httpRequest) userInfo:nil repeats:YES];//7200.0 
    [[NSRunLoop mainRunLoop] addTimer:notifyTimer forMode:NSDefaultRunLoopMode]; 

    return YES; 
} 

按home鍵的應用程序將背景和調用函數applicationDidEnterBackground經過這麼一個後臺任務正在啓動。

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    __block UIBackgroundTaskIdentifier bgTask; 
    UIApplication *app = [UIApplication sharedApplication]; 
    expirationHandler = ^{ 
     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
     bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; 
    }; 
    bgTask = UIBackgroundTaskInvalid; 
    bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; 

} 

通過httpRequest功能我從Web服務器每秒鐘經過這麼一個UILocalNotification火災每1分鐘間隔之後歌廳Y

-(NSString *)httpRequest { 

    NSURL *url = [NSURL URLWithString:@"http://192.168.10.67/t.php"]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

    NSString *userAgent = [NSString stringWithFormat:@"bgTaskTest-IOS"]; 
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; 

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

    [request setHTTPMethod:@"GET"]; 

    [request setTimeoutInterval:25]; 

    NSURLResponse *response; 

    NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 

    NSString *stringReply = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding]; 

    if ([stringReply isEqualToString:@"Y"]) { 
     [self showLocalNotification:nil]; //calling UILocalNotification 
    } else { 
     NSLog(@"%@",stringReply); 
    } 

    return stringReply; 
} 

功能showLocalNotification基於httpRequest函數的響應每1分鐘後調用。

-(void)showLocalNotification { 

    NSString *msg = @"test message"; 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    UILocalNotification *_localNotification = [[UILocalNotification alloc]init]; 

    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; 

    _localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

    _localNotification.alertBody = msg; 

    _localNotification.soundName = UILocalNotificationDefaultSoundName; 

    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:_localNotification]; 
    //[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification]; 

} 

一切正常,當應用程序在後臺時每通知1個提示後會提示。

但我的問題是後臺任務的生命時間是10分鐘,所以在10分鐘後沒有通知提示。出於這個原因,我在beginBackgroundTaskWithExpirationHandler中再次啓動後臺任務,但我的應用程序在此時重新啓動後臺任務。

當應用程序處於後臺時,我無法使用超過10分鐘的通知。

請有人幫助我。

回答

1

在背景中運行任意代碼的時間超過10分鐘(正如您已經注意到的),沒有辦法(在應用商店指南中)運行任意代碼。

10分鐘後,您的應用將被暫停。有幾種解決方法,註冊其他背景模式(如背景音頻,連續播放無聲音文件)或背景音頻或背景位置服務。

這些hacky工作將保持您的應用程序未掛起但是,您的應用程序不會被批准的商店。

在iOS7中有進步的代碼在後臺運行,但沒有什麼會做你想做的。

因此,如果這是一個適合您自己使用的應用程序,請使用私有API或上述建議的方法,但是如果您想在商店中獲得此應用程序,恐怕您的運氣不佳。