2016-04-21 27 views
0

我想在獲得「didEnterRegion」調用後進行API調用。當應用程序在前面時,它的效果很好。但是,如果應用程序已停用,或者即使在後臺,該呼叫也不會發生。我試圖調試它,但一旦「reportLocationEventToServerWithType」被稱爲調試器「死亡」,並且調用永遠不會發生。我不認爲它很重要,但我使用Estimote sdk來進行信標監測。 以下是代碼片段:AFNetworking地理圍欄或信標事件後的API調用

- (void)beaconManager:(id)manager didEnterRegion:(CLBeaconRegion *)region{ 


UIApplication *app    = [UIApplication sharedApplication]; 



NSNumber * mallID = [self.dictBeaconsPerMall objectForKey:region.identifier]; 
Mall * mall = [Mall getMallWithID:mallID]; 

[ApplicationManager sharedInstance].locationManager.beaconRecognizedMall=mall; 
[[NSNotificationCenter defaultCenter]postNotificationName:nLocationMallUpdated object:nil]; 
UILocalNotification * not = [[UILocalNotification alloc]init]; 
    not.alertBody = [NSString stringWithFormat:@"DID Enter beacon region at: %@",mall.strTitle]; 
    not.alertAction = @"alertAction"; 
    not.soundName = UILocalNotificationDefaultSoundName; 

    [app presentLocalNotificationNow:not]; 


[[ApplicationManager sharedInstance].crashAndReportsManager reportLocationEventToServerWithType:@"beacon" andSubParam:region.identifier extraInfo:@{@"paramKey":@"key"}]; 
} 


-(void)reportLocationEventToServerWithType: (NSString*)type andSubParam:(NSString*) strID extraInfo: (NSDictionary*)extraInfo{ 

    NSString* udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 
    NSMutableDictionary * params = [[NSMutableDictionary alloc]init]; 
    [params setObject:type forKey:@"type"]; 
    [params setObject:strID forKey:@"subParam"]; 
    [params setObject:udid forKey:@"udid"]; 

    if(extraInfo) 
     [params addEntriesFromDictionary:extraInfo]; 
    UserLogRequest * req = [[UserLogRequest alloc]initWithCallerObject:self andParams:params]; 
    req.showHud=NO; 
    req.showMessage=NO; 
    [[ApplicationManager sharedInstance].requestManager sendRequestForRequest:req]; 

}

請求初始化和請求管理器「sendRequestForRequest」在其他情況下,千萬所以其執行無關工作的罰款。

謝謝!

回答

0

後臺執行在ios中稍有不同。如果沒有來自項目目標下的功能的某些設置,則無法在後臺執行。你應該從後臺執行後臺執行特定的要求,如位置,音頻等,然後你應該相應地實現代碼。

你只有有限的選項像audioplaying,位置更新,長度有限的任務,IP語音等

在後臺執行請參閱本apple documentation更多的細節,你可以在google後臺執行,你會得到一些後臺執行好的結果。

因此,不在後臺工作的原因是您沒有正確配置它。

希望這會有所幫助:)

+0

謝謝!我也嘗試過,但沒有。在plist中提到了位置背景模式,並試圖用後臺任務來完成。仍然沒有成功... – user2162718

+0

你不能從背景調用API。 – Lion