2013-11-04 4 views
1

didFinishLaunchingWithOptions에서 타이머 루프는 1 분 간격으로 httpRequest 함수를 호출합니다.백그라운드에서 10 분 후에 UILocalNotification 프롬프트가 표시되지 않습니다.

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

기능 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 기능에 의해 나는 그래서 UILocalNotification 화재 매 초 후 매 1 분 간격 후 웹 서버에서 Y을에 geting하고있다.

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

기능 showLocalNotificationhttpRequest 함수의 응답에 기초하여 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]; 

} 

모든 것이 맞습니다. 응용 프로그램이 백그라운드에있을 때마다 알림 메시지가 표시됩니다.

하지만 내 문제는 백그라운드 작업의 수명이 10 분이므로 10 분 후에 알림이 표시되지 않습니다. 이런 이유로 나는 배경 작업을 beginBackgroundTaskWithExpirationHandler에 다시 시작하지만 내 응용 프로그램은 백그라운드 작업을 다시 시작하는이 시점에서 종료합니다.

응용 프로그램이 백그라운드 일 때 알림을 10 분 이상 사용할 수 없습니다.

제발 아무도 도와주세요.

답변

1

(앱 스토어 가이드 라인 내에서) 10 분 이상 백그라운드에서 임의의 코드를 실행하는 방법은 없습니다.

10 분 후에 앱이 일시 중지됩니다. 다른 배경 모드 (예 : 배경 오디오, 조용한 사운드 파일 연속 재생) 또는 배경 음성 또는 배경 위치 서비스에 등록하는 두 가지 방법이 있습니다.

이러한 해킹 된 작업은 응용 프로그램을 계속 유지합니다. 응용 프로그램은 저장소에 대해 승인되지 않습니다.

iOS7의 경우 백그라운드에서 코드를 실행하는 방법이 있지만 원하는 것은 아무 것도하지 않습니다.

개인 용도로 사용하는 앱 인 경우 위의 권장 API를 사용하십시오.하지만이 앱을 스토어에서 사용하려면 운이 좋지 않을 것입니다.

관련 문제