2012-09-08 36 views
4

我想用一個操作表來打開一個鏈接的Safari瀏覽器。該變量被設置正確,並相應地顯示鏈接,但由於某些原因,Safari瀏覽器將無法打開,我想不通爲什麼...openURL不能打開Safari

下面的代碼:

-(void)actionSheet { 
    sheet = [[UIActionSheet alloc] initWithTitle:@"Options" 
            delegate:self 
          cancelButtonTitle:@"Cancel" 
         destructiveButtonTitle:nil 
          otherButtonTitles:@"Open in Safari", nil]; 

    [sheet showInView:[UIApplication sharedApplication].keyWindow]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex != -1) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.url]]; 
    } 
} 
+0

你在哪裏創建 「self.url」? –

+0

如果你的NSLog(@「%@」,self.url)'你得到了什麼? –

+0

我得到的鏈接,因爲它應該是。 –

回答

1

要打開鏈接在Safari中,你所要做的就是以下幾點。 urlAddress是一個NSString,您可以在任何需要設置的位置設置它。或者,您可以用@"someString"代替urlAddress

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlAddress]]; 

另外,你檢查你的頭文件實現UIActionSheetDelegate協議?

編輯:

嘗試按照您的來電,看是否會產生一個錯誤:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex != -1) { 

     NSUrl *myURL = [NSURL URLWithString:self.url]; 

     if (![[UIApplication sharedApplication] openURL:myURL]) { 
      NSLog(@"%@%@",@"Failed to open url:",[myURL description]); 
     } 

    } 
} 
+0

是的,我已經實現了委託,讓我使urlAddress成爲一個字符串,看看它是否有效。 –

+0

所以我將它改爲:NSString * urlAddress = self.url; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlAddress]];仍然不起作用。 –

+0

如果你已經實現了協議,在你的actionSheet中放置一個NSLog語句:clickedButtonAtIndex方法調用來查看你的程序是否到達那裏,或者在那個方法中設置一個斷點來查看它是否到達那裏。如果它沒有達到那一點,那麼你將能夠更快地發現問題。 –

15
NSString *strurl = [NSString stringWithFormat:@"http://%@",strMediaIconUrl]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]]; 
use http:// must. 
相關問題