2016-09-17 10 views

cevap

233

Tek ihtiyacınız olan: cevap Üstü

guard let url = URL(string: "http://www.google.com") else { 
    return //be safe 
} 

if #available(iOS 10.0, *) { 
    UIApplication.shared.open(url, options: [:], completionHandler: nil) 
} else { 
    UIApplication.shared.openURL(url) 
} 
+0

URL'imde '+' işlecini kullanırsam ne olur? Örneğin: "https://xxxxx.com./xxxxxxxxxxxxxx="+userName+"xxxxxxx="+userPass+"&xxxxxxxxx" bunun gibi. Bu dize bana bir hata verilen yerine 'URL' –

+0

"Hayır '+' adaylar beklenen bağlamsal sonuç türü 'URL üretmek": yapmak kalkmayın Bu: UIApplication.shared.openURL (URL (string:) "burada url eklemek"!). XCode 8'deki derleyici karışık olacak ve düzgün bir şekilde yapılamayacak. Yani sadece bu çözümü olduğu gibi kullanın. Harika çalışıyor! Teşekkürler. –

+0

Yan Not üzerinde' String' üzerinde + operatörünü kullanmak zorunda – Joel

19

doğruysa, ancak size canOpenUrl kontrol etmek veya böyle deneyin isterseniz.

let url = URL(string: "http://www.facebook.com")! 
if UIApplication.shared.canOpenURL(url) { 
    UIApplication.shared.open(url, options: [:], completionHandler: nil) 
    //If you want handle the completion block than 
    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in 
     print("Open url : \(success)") 
    }) 
} 

Not: tamamlama işlemek için istemezse da böyle yazabilir. Bunun yanı completionHandler yazmak için

UIApplication.shared.open(url, options: [:]) 

gerek yok daha fazla ayrıntı için apple documentation kontrol varsayılan değeri nil içeriyor. Swift

6

versiyonu

import UIKit 

protocol PhoneCalling { 
    func call(phoneNumber: String) 
} 

extension PhoneCalling { 
    func call(phoneNumber: String) { 
     let cleanNumber = phoneNumber.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "-", with: "") 
     guard let number = URL(string: "telprompt://" + cleanNumber) else { return } 

     UIApplication.shared.open(number, options: [:], completionHandler: nil) 
    } 
} 
+0

, 'replacecingOccurrences' ile düzenli bir ifade kullanabilirsiniz. – Sulthan

2

Burada Swift 3.0.1 ve ViewController.swift benim için çalıştı ne MacOS Sierra (v10.12.1) Xcode V8.1 kullanıyorum:

// 
// ViewController.swift 
// UIWebViewExample 
// 
// Created by Scott Maretick on 1/2/17. 
// Copyright © 2017 Scott Maretick. All rights reserved. 
// 

import UIKit 

class ViewController: UIViewController { 

    //added this code 
    @IBOutlet weak var webView: UIWebView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Your webView code goes here 
     let url = URL(string: "https://www.google.com") 
     if UIApplication.shared.canOpenURL(url!) { 
      UIApplication.shared.open(url!, options: [:], completionHandler: nil) 
      //If you want handle the completion block than 
      UIApplication.shared.open(url!, options: [:], completionHandler: { (success) in 
       print("Open url : \(success)") 
      }) 
     } 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


}; 
6

Uygulamayı bırakmak yerine uygulamanın içinde açmak istiyorsanız, SafariServices ürününü içe aktarıp çalışabilirsiniz.

import UIKit 
import SafariServices 
let url = URL(string: "https://www.google.com") 
     let vc = SFSafariViewController(url: url!) 
     present(vc, animated: true, completion: nil) 
İlgili konular