It's quite easy to open a URL in SwiftUI, e.g. as I've mentioned in this answer, however, I'm interested in opening the following strings in the web browser (e.g. Safari) with the default search engine:
samplestring
8883notaurl
Is there an option to open the query in a default search engine, instead of just composing a URL like this:
https://www.google.com/search?q=query
And then opening it as a regular URL?
You can use x-web-search:// scheme with x-web-search://?[SearchValue].
Sample code:
let urlStr = "x-web-search://?[SearchValue]"
if let url = URL(string: urlStr) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { didEnd in
print("Did End: \(didEnd)")
}
} else {
print("Can't open URL")
}
} else {
print("Isn't URL")
}
Related
I have an image that I want it's data to be saved in this fileUri generated by ("react-native-fs"). LibraryDirectoryPath/saved_images/{filename}:
/Users/macbookpro/Library/Developer/CoreSimulator/Devices/9CBD2F1E-7330-418D-81BE-108C064DEA7E/data/Containers/Data/Application/C26348CC-3463-43EF-9B26-B7E31641E2EA/Library/saved_images/6B3A6A3A-8DE3-488B-AF43-A54775545B38.jpg
And below is my implementation:
do {
let url = URL(string: fileUri)
let fileExisted = FileManager().fileExists(atPath: url!.path)
if (fileExisted) {
try decryptedData.write(to: url!)
} else {
let handle = try FileHandle(forWritingTo: url!)
handle.write(data) // data is type Data
handle.closeFile()
}
} catch {
reject("FileError", "Failed to write file", error)
}
I also tried let url = URL(fileURLWithPath: fileUri) with and without file:// prepending to fileUri
do {
let url = URL(fileURLWithPath: fileUri)
let fileExisted = FileManager().fileExists(atPath: url.path)
if (fileExisted) {
try decryptedData.write(to: url)
} else {
let handle = try FileHandle(forWritingTo: url)
handle.write(data)
handle.closeFile()
}
} catch {
reject("FileError", "Failed to write file " + error.localizedDescription, error)
}
it says:
You are using the wrong API.
let url = URL(string: fileUri)
is for strings representing a full – even encoded - URL starting with a scheme like file:// or https://.
On the other hand fileUri is actually a path without a scheme, so you have to use
let url = URL(fileURLWithPath: fileUri)
This returns a non optional URL by adding the file:// scheme.
fileUri should be renamed as filePath.
This question already has answers here:
Swift - encode URL
(19 answers)
How to use stringByAddingPercentEncodingWithAllowedCharacters() for a URL in Swift 2.0
(6 answers)
Closed 3 years ago.
Here i trying to open URL with .pdf extension in Safari Browser. But code is not working I'm getting nil in URL. But when i tried URL like "www.google.com" it's working fine
Code
let cerificateURl = "https://file-examples.com/wp-content/uploads/2017/10/file-sample_150kB.pdf"
guard let url = URL(string: cerificateURl) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
Thanks in Advance
Try this one:
guard let url = URL(string: "your web pdf file link here or you can set from local directory path")
else { return }
UIApplication.shared.open(url)
Try This one.
let cerificateURl = "https://file-examples.com/wp-content/uploads/2017/10/file-sample_150kB.pdf"
if let myURL = URL(string: url) {
if let data = try? Data(contentsOf: myURL) {
webView.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL
}
}
I am converting base64 to pdf file in local.
func saveBase64StringToPDF(_ base64String: String)
{
guard
var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last,
let convertedData = Data(base64Encoded: base64String)
else {
//handle error when getting documents URL
return
}
documentsURL.appendPathComponent("yourFileName.pdf")
do {
try convertedData.write(to: documentsURL)
} catch {
//handle write error here
}
print(documentsURL)
}
From above method I am getting a path pdf file
file:///Users/captivatesoft/Library/Developer/CoreSimulator/Devices/63D1814B-1010-4694-883E-D2EE75C386DD/data/Containers/Data/Application/1F39B567-F470-4B7E-9A9C-4723ADFDC428/Documents/yourFileName.pdf
Is there any way to download this pdf file or to show pdf in safari?
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent( "example.pdf") as URL
let data = try! Data(contentsOf: pdfURL)
let webView = WKWebView(frame: CGRect(x:20,y:20,width:view.frame.size.width-40, height:view.frame.size.height-40))
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
view.addSubview(webView)
To open the PDF in Safari you could just do
UIApplication.shared.openURL(URL(string: <the link for the PDF>))
Now to download a PDF or anything from a URL, you could just do
let pdfData = try? Data(contentsOf: URL(string: <the link for the PDF>))
NOTE: try Data(contentsOf: url) will block the thread, so you might want to do it on a different thread like this :
DispatchQueue.global.async { [weak self] in
if let pdfData = try? Data(contentsOf: url) {
// Now you can do anything with the data
}
}
First, you can not download pdf file in iOS, it means you cannot save pdf file in iBook or Album.
You can fetch pdf file data from file URL then you can do follows:
Open in Safari browser from the app.
You can show pdf file in WKWebView within the app.
You can use "QuickLook" framework to show the pdf file.
Using WebView:
if let url = URL(string:string) {
webView.load(URLRequest(url: url))
}
I would like to open my app's settings page inside the Settings app with Swift 4 in iOS 11. Just like the picture shows:
The following codes doesn't work, it will only open the Settings app:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
The app shows in the above picture is able to do this. So I wonder if there is any way to custom the URL Scheme to make it happen?
Oops, it seems it works in iOS 11.4.1:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Just an update because UIApplicationOpenSettingsURLString changed.
guard let url = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
You can open your app settings screen using it's bundle id, for example for CAMERA permission, you can use
if let bundleId = /* your app's bundle identifier */
let url = URL(string: "\(UIApplication.openSettingsURLString)&path=CAMERA/\(bundleId)"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Reference: https://stackoverflow.com/a/61383270/4439983
It seems that Apple has moved a lot of the app configurations to the App path with iOS 11, how to open the app path programmatically in Settings? I tried "App-Prefs:root=\(Bundle.main.bundleIdentifier!)" but this doesn't seem to work.
Please note that my question is specific to: How to open the app path in settings: NOT how to open the settings
Here is the code you're looking for, I guess:
if let url = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
And in addition, the updated version for swift 5 :
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Swift 4.2, iOS 12
Opening just the settings is possible with the function below:
extension UIApplication {
...
#discardableResult
static func openAppSettings() -> Bool {
guard
let settingsURL = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(settingsURL)
else {
return false
}
UIApplication.shared.open(settingsURL)
return true
}
}
Usage: UIApplication.openAppSettings()
But be careful to NOT use "non-public URL scheme", such as: prefs:root= or App-Prefs:root, because otherwise your app will be rejected. This happened to me recently since I was trying to have a deeplink to the wifi section in the settings.
And if you want to make it work for both, older and newer iOS-versions, then do:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
openURL has been deprecated since iOS 10, so I would advise you to use:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: { success in
log.debug("Open app settings success: \(success)")
})
}
}
UIApplicationOpenSettingsURLString has been renamed to UIApplication.openSettingsURLString.
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
SWift 5
In some case we can not open App's setting after trying all the above. To solve this problem 100% just make sure these two step are followed
Step 1.
Right click on the project file -> Add New File -> Add Settings.Bundle in project and edit according to your requirements.
Step 2. Now add some code in your buttons action.
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Note: Using "prefs:Root" is forbidden by apple and your app will be rejected. So, avoid using this api.