2017-07-07 65 views
0

我在我的UITextView中有一個URL鏈接。而在我的iPhone中,有兩個Safari瀏覽器& Chrome應用程序。每當我點擊URL鏈接時,我想在彈出視圖中列出Safari瀏覽器& Chrome(以及手機中的所有其他瀏覽器),以便用戶可以選擇想要打開該URL的瀏覽器。現在它總是打開safari,因爲它是我手機中的默認瀏覽器。有什麼辦法可以選擇瀏覽器來打開網址嗎?我知道有這個代表在UITextView中點擊網址時選擇瀏覽器

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
     // Got the url here. how can i force this url to open in chrome 
     return true 
    } 

但我怎麼能強制此網址在鉻或任何其他瀏覽器打開?我怎樣才能在視圖中列出所有可用的瀏覽器?

+1

此外t o @Lal Krishna的回答,這裏有一個可能有所幫助的鏈接:https://stackoverflow.com/a/4614190/7019821 – Prashant

回答

1

要包裝它,

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
    let actionSheet = UIAlertController(title: "Where do you want to open it", message: "", preferredStyle: .actionSheet) 

    if UIApplication.shared.canOpenURL(URL(string: "http://www.stackoverflow.com")!) { 
     actionSheet.addAction(UIAlertAction(title: "Safari", style: .default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil) 
     })) 
    } 

    if UIApplication.shared.canOpenURL(URL(string: "googlechrome://www.stackoverflow.com")!) { 
     actionSheet.addAction(UIAlertAction(title: "Chrome", style: .default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      UIApplication.shared.open(URL(string: "googlechrome://www.stackoverflow.com")!, options: [:], completionHandler: nil) 
     })) 
    } 

    if UIApplication.shared.canOpenURL(URL(string: "firefox://open-url?url=http://www.stackoverflow.com")!) { 
     actionSheet.addAction(UIAlertAction(title: "Fire Fox", style: .default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      UIApplication.shared.open(URL(string: "firefox://open-url?url=http://www.stackoverflow.com")!, options: [:], completionHandler: nil) 
     })) 
    } 

    if UIApplication.shared.canOpenURL(URL(string: "opera-http://www.stackoverflow.com")!) { 
     actionSheet.addAction(UIAlertAction(title: "Opera", style: .default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      UIApplication.shared.open(URL(string: "opera-http://www.stackoverflow.com")!, options: [:], completionHandler: nil) 
     })) 
    } 

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
    self.present(actionSheet, animated: true, completion: nil) 

    return false 
} 
1

目前我找不到列出所有瀏覽器的任何API。

您可以使用canOpenURL:方法手動檢查特定應用程序是否已安裝。

- (BOOL)isSafariInstalled { 
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"http://google.com"]]; 
} 

- (BOOL)isChromeInstalled { 
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"googlechrome://"]]; 
} 

- (BOOL)isOperaInstalled { 
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"opera-http://google.com"]]; 
} 

欲瞭解更多關於Chrome - 檢查Docs

+0

感謝您的回覆。我會檢查這一點。但我想列出所有瀏覽器並強制打開它。我會檢查它並讓你知道 – Bali

1

我沒試過,只是檢查,它out.Return 到shouldInteractWith delagte method.You此委託方法爲你的東西

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
     // Got the url here. how can i force this url to open in chrome 

     //here do your stuffs to show popup or something & based on selection launch url with specific browsers 

     return false //to restrict open automatically 
} 
+0

感謝您的回覆。但是這隻會打開url的權利?我想列出所有瀏覽器並使URL打開除默認瀏覽器以外的其他特定瀏覽器。 – Bali

+0

@Bali你可以實現代碼來顯示彈出之前返回false.By選擇瀏覽器加載url –

+0

問題是我如何列出瀏覽器?基本上這是我的問題。如何獲取可用瀏覽器的列表? – Bali