openURL
में एक यूआरएल कैसे खोलें स्विफ्ट 3 में बहिष्कृत किया गया है। क्या कोई यूआरएल खोलने की कोशिश करते समय प्रतिस्थापन openURL:options:completionHandler:
काम करता है के बारे में कुछ उदाहरण प्रदान कर सकता है?स्विफ्ट 3
उत्तर
आप सभी की जरूरत है:
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)
}
जवाब से ऊपर सही है, लेकिन आप आप canOpenUrl
जाँच करने के लिए या इस तरह की कोशिश नहीं करना चाहते हैं।
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)")
})
}
नोट: आप पूरा होने को संभालने के लिए नहीं चाहता है, तो आप भी इस तरह लिख सकते हैं।
UIApplication.shared.open(url, options: [:])
नहीं completionHandler
इसके बारे में के रूप में लिखने के लिए की जरूरत है और अधिक विस्तार के लिए जाँच apple documentation, डिफ़ॉल्ट मान nil
शामिल हैं।
स्विफ्ट 3 संस्करण
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)
}
}
आप 'replacingOccurrences'' के साथ एक नियमित अभिव्यक्ति का उपयोग कर सकते हैं। – Sulthan
मैं MacOS सिएरा (v10.12.1) Xcode v8.1 का उपयोग कर रहा स्विफ्ट 3.0.1 और यहाँ क्या ViewController.swift में मेरे लिए काम किया है:
//
// 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.
}
};
यदि आप ऐप को छोड़ने के बजाय ऐप के अंदर खोलना चाहते हैं तो आप आयात SafariServices आयात कर सकते हैं और इसे काम कर सकते हैं।
import UIKit
import SafariServices
let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
- 1. स्विफ्ट 3
- 2. स्विफ्ट 3
- 3. स्विफ्ट 3
- 4. स्विफ्ट 3
- 5. स्विफ्ट 3
- 6. स्विफ्ट 3
- 7. स्विफ्ट 3
- 8. स्विफ्ट 3
- 9. स्विफ्ट 3
- 10. स्विफ्ट 3
- 11. स्विफ्ट 3
- 12. स्विफ्ट 3
- 13. स्विफ्ट 3
- 14. स्विफ्ट 3
- 15. स्विफ्ट 3
- 16. स्विफ्ट 3
- 17. स्विफ्ट 3
- 18. स्विफ्ट 3
- 19. स्विफ्ट 3
- 20. स्विफ्ट 3
- 21. स्विफ्ट 3 -
- 22. स्विफ्ट 3
- 23. स्विफ्ट 3
- 24. स्विफ्ट 3
- 25. स्विफ्ट 3
- 26. स्विफ्ट 3
- 27. स्विफ्ट 3
- 28. हैंडलिंग कोशिश और स्विफ्ट 3 से पहले स्विफ्ट 3
- 29. लेखन डाटा स्विफ्ट 3
- 30. अधिसूचना स्विफ्ट 3
यदि मैं अपने यूआरएल में '+' ऑपरेटर का उपयोग करता हूं तो क्या होगा? उदाहरण के लिए: "https://xxxxx.com./xxxxxxxxxxxxxx="+userName+"xxxxxxx="+userPass+"&xxxxxxxxx" इस तरह। उस स्ट्रिंग ने मुझे एक त्रुटि दी "नो '+ उम्मीदवार अपेक्षित प्रासंगिक परिणाम प्रकार' यूआरएल" –
उत्पन्न करते हैं, आपको 'यूआरएल' –
साइड नोट पर + 'ऑपरेटर' पर + ऑपरेटर का उपयोग करना होगा: नोट करने की कोशिश न करें यह: UIApplication.shared.openURL (यूआरएल (स्ट्रिंग: "यहां यूआरएल डालें")!)। एक्सकोड 8 पर कंपाइलर भ्रमित हो जाएगा और ठीक से निर्माण करने में सक्षम नहीं होगा। तो बस इस समाधान का उपयोग करें। बहुत अच्छा काम करता है! धन्यवाद। – Joel