2013-08-27 26 views
3

iOS的位置更新我想寫像定位跟蹤應用程序「查找我的iPhone」在前臺,後臺運行,甚至終止(不運行)。位置將定期發送到我的服務器。我已經在谷歌搜索和閱讀許多文件,教程的意見,代碼關於這個話題。然後我找到了this tutorial。但在本教程中,位置被髮送到前臺和後臺的「.txt」文件沒有終止 ...我的意思是,當應用程序被殺害後,它不會在後臺重新發送位置「.txt」文件。 。因此,我添加並更新了一些代碼,以便在它未運行時發送位置信息。但是,我沒有這樣做......我的意思是,當我在多任務處理中(關閉)應用程序(雙擊主頁按鈕)時,它不發送到位置...即使應用程序沒有運行(如查找我的iPhone)

你能幫我嗎,我該如何解決這個問題?

在此先感謝

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    [self log:[NSString stringWithFormat:@"Background location %.06f %.06f %@" , newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp]]; 
} 

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

    id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]; 
    if (locationValue) 
    { 
     // create a new manager and start checking for sig changes 

     [self log:@"didFinishLaunchingWithOptions location key"]; 
     m_locManager = [[CLLocationManager alloc] init]; 

     [self log:@"didFinishLaunchingWithOptions created manager"]; 
     m_locManager.delegate = self; 

     [self log:@"didFinishLaunchingWithOptions set delegate"]; 
     [m_locManager startMonitoringSignificantLocationChanges]; 

     [self log:@"didFinishLaunchingWithOptions monitoring sig changes"]; 

     return YES; 
    } 

    [self log:@"didFinishLaunchingWithOptions"];  
    return YES; 
} 


- (void)applicationWillResignActive:(UIApplication *)application { 
    [self log:@"applicationWillResignActive"]; 
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
    [userDefaults setBool:viewController.m_significantSwitch.on forKey:@"significant"]; 
    [userDefaults synchronize]; 
}     


- (void)applicationDidEnterBackground:(UIApplication *)application { 
    [self log:@"applicationDidEnterBackground"]; 
    [m_locManager startMonitoringSignificantLocationChanges]; 
} 


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

    [self log:@"applicationWillEnterForeground"]; 
    [m_locManager stopMonitoringSignificantLocationChanges]; 

} 


- (void)applicationDidBecomeActive:(UIApplication *)application { 
    [self log:@"applicationDidBecomeActive"]; 

    if (![window.subviews count]) 
    { 
     // Add the view controller's view to the window and display. 
     [window addSubview:viewController.view]; 
     [window makeKeyAndVisible]; 

     NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
     viewController.m_significantSwitch.on = [userDefaults boolForKey:@"significant"]; 
     if (viewController.m_significantSwitch.on) 
      [viewController actionSignificant:nil]; 
    } 
} 


- (void)applicationWillTerminate:(UIApplication *)application { 
    [self log:@"applicationWillTerminate"]; 
    [m_locManager startMonitoringSignificantLocationChanges]; 

} 

回答

3

我的朋友,你錯了。事實上,在iOS中殺死應用程序後,沒有任何辦法可以完成任務。你可以閱讀關於這here in Apple documentation

找到我的iPhone -這是一個蘋果應用程序,他們正在使用私人API來使這成爲可能。這些類型的功能不適用於通用開發人員編程。

所以請不要走在這條路上,一切都將徒勞無功。

希望這能幫到你!

+0

嗯,好吧我的朋友..這個答案可能是我的失望:)我的應用程序只是要在前臺和後臺工作:/所以,可能會發生電池問題...再次感謝.. – xcoder123

+0

好.. :)我是新的在stackoverflow,我不知道這個平臺的規則:)) – xcoder123

1

當應用程序被終止,不能運行任何東西,除非你能接受推送通知或UILocalNotifications。您可以嘗試複製UILocalNotifications功能,並在您的應用程序死亡/在bg /中時執行您的操作。順便說一句,這是一件複雜的事情,你可以做更多的關於ios7後臺任務的東西。

+0

好吧,我的朋友,我在這個應用程序中工作的過程會很困難...謝謝.. – xcoder123

7

這是不正確的,當你的應用程序被終止時,它仍然可以監視重要的位置更新。

+0

你是正確的海報沒有指定他們使用的是哪個版本的iOS,但是如果用戶從應用程序切換器終止應用程序,則在iOS7中不再接收位置更新。 – Brett

+0

這裏是終止的應用程序http:// mobileoop之後的鏈接。COM /獲取-位置更新換IOS -7-和-8-當最APP-IS-killedterminatedsuspended –

1

這可能對你有幫助。 Getting the User’s Location

參見章節Starting the Significant-Change Location Service。它說

如果您讓重大更改的位置服務正在運行,並且您的iOS應用程序隨後被暫停或終止,則服務會在新位置數據到達時自動喚醒您的應用程序。在起牀時間,應用程序被置於後臺,您可以獲得少量時間(大約10秒鐘)以手動重新啓動位置服務並處理位置數據。

相關問題