2015-09-30 7 views
7

Mein Code:SFSafariViewController-Absturz: Die angegebene URL hat ein nicht unterstütztes Schema.

if let url = NSURL(string: "www.google.com") { 
    let safariViewController = SFSafariViewController(URL: url) 
    safariViewController.view.tintColor = UIColor.wantoPrimaryOrangeColor() 
    presentViewController(safariViewController, animated: true, completion: nil) 
} 

Dieser stürzt bei der Initialisierung nur mit Ausnahme:

Die angegebene URL ein nicht unterstütztes Schema hat. Nur HTTP und HTTPS-URLs werden

unterstützt

Als ich url = NSURL(string: "http://www.google.com") verwenden, ist alles in Ordnung. Ich lade URLs von API und daher kann ich nicht sicher sein, dass sie http(s):// vorangestellt werden.

Wie wird dieses Problem gelöst? Soll ich immer http:// überprüfen und voranstellen, oder gibt es einen Workaround?

+0

siehe Link Ihnen helfen können http://stackoverflow.com/questions/32577727/uiwebview-does-not-show-images-on-ios-9-and-safariviewcontroller-does-not-load –

+0

Ich habe das überprüft, es ist nicht verwandt. Ich erlaube bereits willkürliches Laden. Dieses Problem erlaubt keine Verbindung und das Laden von lokalem HTML von SFSafariController. –

+1

Art von macht Sie wünschen, es gäbe ein 'SFSafariViewController.canOpen (URL:)' -ist Möglichkeit, unterstützte URLs zu überprüfen. – Jonny

Antwort

3

Sie können die Verfügbarkeit von http in Ihrer url Zeichenfolge überprüfen, bevor Sie NSUrl Objekt erstellen.

Put folgenden Code, bevor Sie Ihren Code und es wird Ihr Problem lösen (Sie können für https überprüfen auch in gleicher Weise)

var strUrl : String = "www.google.com" 
if strUrl.lowercaseString.hasPrefix("http://")==false{ 
    strUrl = "http://".stringByAppendingString(strUrl) 
} 
+1

Ja, das ist eine Lösung, die ich in der Frage selbst erwähnt habe. Ich möchte wissen, ob es einige Workarounds oder einen besseren Weg gibt, um das Problem anzugehen? –

+0

@SahilKapoor Ab sofort kann ich keine bessere Lösung dafür finden, wenn Sie es dann auch für zukünftige Referenz erwähnen könnten – Yuvrajsinh

19

Versuchen Schema URL überprüfen, bevor eine Instanz SFSafariViewController machen.

Swift 3:

func openURL(_ urlString: String) { 
    guard let url = URL(string: urlString) else { 
     // not a valid URL 
     return 
    } 

    if ["http", "https"].contains(url.scheme?.lowercased() ?? "") { 
     // Can open with SFSafariViewController 
     let safariViewController = SFSafariViewController(url: url) 
     self.present(safariViewController, animated: true, completion: nil) 
    } else { 
     // Scheme is not supported or no scheme is given, use openURL 
     UIApplication.shared.open(url, options: [:], completionHandler: nil) 
    } 
} 

Swift 2:

func openURL(urlString: String) { 
    guard let url = NSURL(string: urlString) else { 
     // not a valid URL 
     return 
    } 

    if ["http", "https"].contains(url.scheme.lowercaseString) { 
     // Can open with SFSafariViewController 
     let safariViewController = SFSafariViewController(URL: url) 
     presentViewController(safariViewController, animated: true, completion: nil) 
    } else { 
     // Scheme is not supported or no scheme is given, use openURL 
     UIApplication.sharedApplication().openURL(url) 
    } 
} 
4

Ich habe eine Kombination aus & der Yuvrajsinh hoseokchoi die Antworten.

func openLinkInSafari(withURLString link: String) { 

    guard var url = NSURL(string: link) else { 
     print("INVALID URL") 
     return 
    } 

    /// Test for valid scheme & append "http" if needed 
    if !(["http", "https"].contains(url.scheme.lowercaseString)) { 
     let appendedLink = "http://".stringByAppendingString(link) 

     url = NSURL(string: appendedLink)! 
    } 

    let safariViewController = SFSafariViewController(URL: url) 
    presentViewController(safariViewController, animated: true, completion: nil) 
} 
Verwandte Themen