2013-08-05 2 views
0

두 개의 단추가있는 경고보기가 있지만 단추가 URL을 열지 않습니다. 나는 그 오류를 모른다. 도와주세요. 첫 번째는 사실이 아니다 경우에만 두 번째 발사를 원하기 때문에 당신이 당신의 두 번째 조건에 elseif을 사용해야UIAlertView가 작동하지 않습니다.

-(IBAction)showAlertView { 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Obrir en..." 
          message:@"Es pot requirir la aplicació de Google Maps" 
          delegate:self 
          cancelButtonTitle:@"Millor no..." 
          otherButtonTitles:@"Mapes",@"Google Maps",nil]; 
    [alert show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

    if([title isEqualToString:@"Mapes"]) 
    { 
     UIApplication *ourApplication = [UIApplication sharedApplication]; 
     NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona"; 
     NSURL *ourURL = [NSURL URLWithString:ourPath]; 
     [ourApplication openURL:ourURL]; 
    } 
    if([title isEqualToString:@"Google Maps"]) 
    { 
     UIApplication *ourApplication = [UIApplication sharedApplication]; 
     NSString *ourPath = @"comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking"; 
     NSURL *ourURL = [NSURL URLWithString:ourPath]; 
     [ourApplication openURL:ourURL]; 
    } 
} 
+0

아무것도 분명히 잘못된 보지 않는다. 'alertVew : clickedButtonAtIndex :'함수에 중단 점을 넣어 정확히 무슨 일이 일어나는지 확인하십시오. – bengoesboom

+0

alertView에 중단 점을 설정하십시오 : clickedButtonAtIndex : 메소드와 추적. 반환되는 제목은 무엇입니까? 블록과 if 블록이 일치합니까? – TomSwift

+0

make urre에 공간이 없습니다. –

답변

5

주소의 특수 문자 (ç)가 NSURL을 사용하지 않는 것으로 생각됩니다. NSString의 stringByAddingPercentEscapesUsingEncoding: 메소드를 사용하여 NSURL 초기화 프로그램에 전달하기 전에 인코딩하십시오.

0

첫째 :

여기에 코드입니다. 둘째로, "Mapes"에서 사용하고있는 URL 인 것 같습니다.이 URL을 테스트 한 결과 해당 URL이 범인 것으로 보입니다. 해당 URL을 다른 테스트 URL로 변경하면 작동했습니다.

1

해고 한 URL을 확인하십시오.

if([ourApplication canOpenURL:ourURL]) 
    [ourApplication openURL:ourURL]; 
else 
    NSLog(@"URL is not valid."); 

URL을 모두 확인해도 열 수 없습니다. 위의 코드로 URL을 열어 볼 수 있는지 여부를 확인할 수 있습니다.

1

당신은 URL의를 확인해야합니다,이 시도 :

-(IBAction)showAlertView { 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Obrir en..." 
          message:@"Es pot requirir la aplicació de Google Maps" 
          delegate:self 
          cancelButtonTitle:@"Millor no..." 
          otherButtonTitles:@"Mapes",@"Google Maps",nil]; 
    [alert show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    //0 is cancel 
    if(buttonIndex == 1) 
    { 
     [self openURLWithString:@"https://www.google.com/maps/preview#!q=Pla%C3%A7a+del+Rei+43003+Tarragona&data=!1m4!1m3!1d4618!2d1.2582895!3d41.1168719!4m11!1m10!4m8!1m3!1d26081603!2d-95.677068!3d37.0625!3m2!1i1024!2i768!4f13.1!17b1"];  
    } 
    if(buttonIndex == 2) 
    { 
     [self openURLWithString:@"https://www.google.com/maps/preview#!data=!1m4!1m3!1d18473!2d1.258181!3d41.1168316!4m13!3m12!1m0!1m1!1sPla%C3%A7a+del+Rei+43003+Tarragona!3m8!1m3!1d26081603!2d-95.677068!3d37.0625!3m2!1i1024!2i768!4f13.1&fid=0"]; 

    } 
} 

- (void)openURLWithString:(NSString *)string{ 

    UIApplication *ourApplication = [UIApplication sharedApplication]; 
    NSString *ourPath = string; 
    NSURL *ourURL = [NSURL URLWithString:ourPath]; 

    if([ourApplication canOpenURL:ourURL]) 

     [ourApplication openURL:ourURL]; 
    else 
     NSLog(@"URL is not valid."); 

} 
관련 문제