2012-03-14 53 views
0

我正在開發越獄iPhone。我正在閱讀開始iOS 5書。目前,我已經使用了本書中的位置代碼並稍微改變了它。我在代碼中所做的更改是我在文本文件中寫入位置。但程序崩潰。雖然沒有任何修改的同一個程序是完美的。嘗試在文本文件中寫入位置

下面是代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = 2.0f; 

    [locationManager startUpdatingLocation]; 
} 






- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    FILE *file = fopen("/usr/locations.txt", "a"); 

    if (!file) 
    { 
     fprintf(file, "Error opening file"); 
    } 

    if (startingPoint == nil) 
     self.startingPoint = newLocation; 

    NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0", 
           newLocation.coordinate.latitude]; 
    latitudeLabel.text = latitudeString; 


    NSNumber *latitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude]; 
    NSNumber *longitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude]; 


    NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0", 
           newLocation.coordinate.longitude]; 
    longitudeLabel.text = longitudeString; 


    NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm", 
              newLocation.horizontalAccuracy]; 
    horizontalAccuracyLabel.text = horizontalAccuracyString; 


    NSString *altitudeString = [NSString stringWithFormat:@"%gm", 
           newLocation.altitude]; 
    altitudeLabel.text = altitudeString; 

    NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm", 
             newLocation.verticalAccuracy]; 
    verticalAccuracyLabel.text = verticalAccuracyString; 


    CLLocationDistance distance = [newLocation 
            distanceFromLocation:startingPoint]; 
    NSString *distanceString = [NSString stringWithFormat:@"%gm", distance]; 
    distanceTraveledLabel.text = distanceString; 


    struct tm *local; 
    time_t t; 

    t = time(NULL); 
    local = localtime(&t); 

    fprintf(file, "Local time and Date: %s ", asctime(local)); 

    //----------------- ------------------------------------------------------------ 

    fprintf(file, "Latitude = %lf, Longitude = %lf \n", 
      newLocation.coordinate.latitude, newLocation.coordinate.longitude); 


    fclose(file); 

} 
+0

您是否閱讀過關於文件系統訪問權限的[Apple文檔](https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/Introduction/Introduction.html)?閱讀。 – JoePasq 2012-03-14 18:30:58

+0

現在,我只想在控制檯上打印位置。但是我還沒有做到這一點。我的開發IDE是Xcode 4.2,iOS版本是5.0.1 – 2012-03-24 04:18:39

回答

0

你對這個問題最近評論說,你要打印到控制檯。要在控制檯上打印,請使用NSLog()。當您使用對象%@的格式說明符時,替換對象的字符串是對象的description方法。

要在控制檯使用

NSLog(@"New location: %@", newLocation); 
NSLog(@"Old location: %@", oldLocation); 

CLLocation返回「「的形式緯度的字符串的描述方法,經度+/-精度米(速度KPH /航向)@日期時間打印位置'。「