【问题标题】:My project run only in simulator but do not work in device, why?我的项目只能在模拟器中运行,但不能在设备中运行,为什么?
【发布时间】:2017-02-23 17:40:57
【问题描述】:

我在地图中创建了一条折线。并在地图视图中成功创建折线。 我的问题是,这段代码只运行模拟器。不要在设备上工作。我尝试了很多次,但无法在设备上工作。

生成 IPA 并安装在我的设备中并让我的应用程序崩溃。为什么?并且只是在模拟器中运行,为什么?

I follow this link

为什么会这样?请帮忙 谢谢你

我的代码

-(void) centerMapForCoordinateArray:(CLLocationCoordinate2D *)routes andCount:(int)count{
MKCoordinateRegion region;

CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx <count; idx++)
{
    CLLocationCoordinate2D currentLocation = routes[idx];
    if(currentLocation.latitude > maxLat)
        maxLat = currentLocation.latitude;
    if(currentLocation.latitude < minLat)
        minLat = currentLocation.latitude;
    if(currentLocation.longitude > maxLon)
        maxLon = currentLocation.longitude;
    if(currentLocation.longitude < minLon)
        minLon = currentLocation.longitude;
}


region.center.latitude     = (maxLat + minLat) / 2;
region.center.longitude    = (maxLon + minLon) / 2;
region.span.latitudeDelta  = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;

[self.mapView setRegion:region animated:YES];
 }

- (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString       {
const char *bytes = [encodedString UTF8String];
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger idx = 0;

NSUInteger count = length / 4;
CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
NSUInteger coordIdx = 0;

float latitude = 0;
float longitude = 0;
while (idx < length) {
    char byte = 0;
    int res = 0;
    char shift = 0;
    
    do {
        byte = bytes[idx++] - 63;
        res |= (byte & 0x1F) << shift;
        shift += 5;
    } while (byte >= 0x20);
    
    float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
    latitude += deltaLat;
    
    shift = 0;
    res = 0;
    
    do {
        byte = bytes[idx++] - 0x3F;
        res |= (byte & 0x1F) << shift;
        shift += 5;
    } while (byte >= 0x20);
    
    float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
    longitude += deltaLon;
    
    float finalLat = latitude * 1E-5;
    float finalLon = longitude * 1E-5;
    
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
    coords[coordIdx++] = coord;
    
    if (coordIdx == count) {
        NSUInteger newCount = count + 10;
        coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
        count = newCount;
    }
 }

MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
free(coords);

return polyline;
}


- (void)getDirections {
//Cross country
//37.705553,-122.372074 to 25.883937,-80.223026


//Home to work
//37.7577,-122.4376 to 37.764473,-122.399639

CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:22.7001469 longitude:75.8758194];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
annotation.title = @"You";
[self.mapView addAnnotation:annotation];

CLLocation *keyPlace = [[CLLocation alloc] initWithLatitude:22.6924483 longitude:75.8653895];
MKPointAnnotation *endannotation = [[MKPointAnnotation alloc] init];
endannotation.coordinate = CLLocationCoordinate2DMake(keyPlace.coordinate.latitude, keyPlace.coordinate.longitude);
endannotation.title = @"School";
[self.mapView addAnnotation:endannotation];

CLLocationCoordinate2D endCoordinate;

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=driving", newLocation.coordinate.latitude, newLocation.coordinate.longitude, keyPlace.coordinate.latitude, keyPlace.coordinate.longitude]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData =  [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];



if (!error) {
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];
      NSLog(@"response == %@",responseDict);
    if ([[responseDict valueForKey:@"status"] isEqualToString:@"ZERO_RESULTS"]) {
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:@"Could not route path from your current location"
                                   delegate:nil
                          cancelButtonTitle:@"Close"
                          otherButtonTitles:nil, nil] show];
        return;
    }
    int points_count = 0;
    if ([[responseDict objectForKey:@"routes"] count])
        points_count = [[[[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"] count];
    
    
    if (!points_count) {
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:@"Could not route path from your current location"
                                   delegate:nil
                          cancelButtonTitle:@"Close"
                          otherButtonTitles:nil, nil] show];
        return;
    }
    CLLocationCoordinate2D points[points_count];
    NSLog(@"routes %@", [[[[responseDict objectForKey:@"routes"] objectAtIndex:0]objectForKey:@"overview_polyline"] objectForKey:@"points"]
          );
    MKPolyline *polyline = [self polylineWithEncodedString:[[[[responseDict objectForKey:@"routes"] objectAtIndex:0]objectForKey:@"overview_polyline"] objectForKey:@"points"]];
    [self.mapView addOverlay:polyline];
    int j = 0;
    NSArray *steps = nil;
    if (points_count && [[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] count])
        steps = [[[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
    for (int i = 0; i < points_count; i++) {
        
        double st_lat = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lat"] doubleValue];
        double st_lon = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lng"] doubleValue];
        //NSLog(@"lat lon: %f %f", st_lat, st_lon);
        if (st_lat > 0.0f && st_lon > 0.0f) {
            points[j] = CLLocationCoordinate2DMake(st_lat, st_lon);
            j++;
        }
        double end_lat = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lat"] doubleValue];
        double end_lon = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lng"] doubleValue];
        
        //NSLog(@"lat %f lng %f",end_lat,end_lon);
        //if (end_lat > 0.0f && end_lon > 0.0f) {
        points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
        endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
        j++;
        //}
    }
    NSLog(@"points Count %d",points_count);
    //        MKPolyline *polyline = [MKPolyline polylineWithCoordinates:points count:points_count];
    //        [self.mapView addOverlay:polyline];
    [self centerMapForCoordinateArray:points andCount:points_count];
}
}

#pragma mark - MapKit
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.canShowCallout = YES;
annView.animatesDrop = YES;
return annView;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView
        viewForOverlay:(id<MKOverlay>)overlay {
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithOverlay:overlay];

overlayView.lineWidth = 5;
overlayView.strokeColor = [UIColor purpleColor];
overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5f];
return overlayView;
}

【问题讨论】:

  • 崩溃的原因是什么?检查日志并粘贴。这将有助于追踪问题。
  • 设备未连接到我的系统,因此我创建 IPA 并安装在我的设备中。那怎么找理由呢?
  • @Janmenjaya 请下载参考链接并检查问题。谢谢
  • @DivankKumawat 代码很好,没有崩溃,当我在我的设备 (iPad) 中运行您的代码时,我认为崩溃是其他原因发生的。
  • 您测试的是哪个模拟器?以及哪个 iOS 版本的模拟器

标签: ios objective-c iphone google-maps google-maps-api-3


【解决方案1】:

尝试在 iphone4s 或 5 模拟器中运行,如果它在模拟器中崩溃而不是更改

points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
j++;

if (j<=points_count) {
    points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
    endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
    j++;
}

为什么?因为 我们声明CLLocationCoordinate2D points[points_count]; 所以最大Coordinate2Dpoints_count 但是某些纬度经度j 值高于points_count 所以我们必须为j 设置if 条件

检查一下,让我知道它是否解决了您的问题。

【讨论】:

  • 好的,我检查过了,请稍等,感谢您的宝贵时间:)
  • 不,同样的问题?请推荐另一个教程
  • 我在我的设备中安装了 IPA 并在崩溃后运行?怎么样?
  • 你签入的是iphone4s还是5 Simulator?
  • 是的,我在 iphone4s 或 5 模拟器中检查过,但在设备中无法正常工作。
【解决方案2】:

通过设置开发证书和检查配置文件来尝试证书似乎有问题。

在构建设置/目标中设置正确的证书和配置文件,然后检查。

详细阅读:

https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppStoreDistributionTutorial/LaunchingYourApponDevices/LaunchingYourApponDevices.html

【讨论】:

  • 证书和配置文件以及所有过程都是正确的。首先是我更改了 lat long 然后创建 IPA 并在设备中运行,这样我的应用程序就会崩溃。第二件事是在创建 IPA 并在设备中运行后不要更改 lat。那个时候在设备中正常工作。
  • 请指导和帮助。请建议任何其他教程。
【解决方案3】:

我浏览了你的代码并在模拟器中运行,它会显示警告 CFNetwork SSLHandshake failed (-9824) NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824),所以我认为你错过了 plist 中的 NSAppTransportSecurity 键。

【讨论】:

  • 我在 plist 中添加了 NSAppTransportSecurity 密钥,但没有解决我的问题。 :(
  • 请指导和帮助。请建议任何其他教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 2018-10-16
相关资源
最近更新 更多