I am calling API wih 'Alamofire'. In my response I get one weblink. I store that weblink in to one variable. Now I want to store that weblink into the local database. so I use 'userdefaults'. But when I retrive that weblink into the other 'viewcontroller' at that time my weblink changed and web page didnot open.
let weblink = datastring["Web_Link"] as! String
UserDefaults.standard.set(weblink, forKey: "Link")
for these I use this
UserDefaults.standard.set(url: URL?, forKey: String)
and in another 'viewcontroller'
let url = UserDefaults.standard.url(forKey: "Link")
for these I used
let url = UserDefaults.standard.url(forKey: String)
and my other code is
let request = URLRequest.init(url: url!)
self.webview.load(request)
my url example is "https://example.com/"
but when I retrive at that time url is
'https:/example.com'
so my webpage cannot open. I am using wkwebview.
You store the url as string so retrieve it like this.
let url = UserDefaults.standard.string(forKey: "Link")
It looks like you are storing String value in user defaults and trying to get URL from UserDefaults. So Please try as like below.
let weblink = datastring["Web_Link"] as! String
if let url = URL(string: weblink){
UserDefaults.standard.set(url, forKey: "Link")
}
Related
Im having a big trouble with URl
I want to use url for almofire , but unforutnately it always return nil
or ortherwise , this solutuin return valid URL but with weird $$*%&$( in front of https:// an resulting to always got nil response
let req = "​https://api-staging.xx.oo/v1/s-locations/"
guard let percentReq = req.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed ) else { return nil }
let urlReq = URL(string: percentReq)!
// work , url got , but fetch nothing
let urlReq = URL(string: req)!
// error FATAL
Honestly, looks like you managed to get some trash into the string by copy-pasting, I'm assuming it's encoding or something. This code from below works just fine:
let test = "https://api-staging.xx.oo/v1/s-locations/"
let url = URL(string: test)
I want to add my userid in my link but link shoud be same and clickable.
Here's my code:
buo.getShortUrl(with: lp) { (url, error) in
//url = "myrmndtest5.test-app.link/aLyM9DI2cS"
let domain = url
let parameter:AnyObject = "?id=5" as AnyObject
let testurl:NSURL = NSURL(string: "\(domain)) \(parameter)")!
print(testurl)
}
it showing me nil value. is there any code for append both
Use url components instead. You can add query items to a string that has already the scheme, host, and path, like so:
let url = "https://myrmndtest5.test-app.link/aLyM9DI2cS"
var components = URLComponents(string: url)!
let queryItemId = URLQueryItem(name: "id", value: "5")
components.queryItems = [queryItemId]
print(components.url!)
(PS: I am force-unwrapping for brevity)
I have generated a URL with the following function below. I would like to save this to the local folder for use later on. However, when I save it to the local folder, and then retrieve it, the URL is truncated. Please can someone advise on how I would be able to save and extract the full URL?
func createPhotoURL() -> URL {
let fileName = "tempImage_wb.jpg"
let documentsDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = documentsDirectories.first!
let pdfPageURL = documentDirectory.appendingPathComponent("\(fileName)")
return pdfPageURL
}
When I call the function I get the full length URL:
let imageURL = createPhotoURL() // CORRECT URL FOR FILE UPLAOD
print("imageURL: \(imageURL)")
Console:
file:///var/mobile/Containers/Data/Application/CDDE2FED-5AAB-4960-9ACF-33E7B42D05AE/Documents/tempImage_wb.jpg
Save above URL to local folder and the retrieve it:
UserDefaults.standard.set(imageURL, forKey: "URL_IMAGE")
guard let retrievedURL = UserDefaults.standard.string(forKey: "URL_IMAGE") else{return}
print("retrievedURL: \(retrievedURL)")
Console:
retrievedURL: ~/Documents/tempImage_wb.jpg
To answer your question, use UserDefaults.standard.url instead of UserDefaults.standard.string
guard let retrievedURL = UserDefaults.standard.url(forKey: "URL_IMAGE") else{return}
But this will work only if you save the path and retrieve it in the same session. If you use the above code and run the app multiple times, you will get non-truncated urls (like you want). But if you check it properly you can see you are actually getting different urls in different sessions.
So you shouldn't save it as a URL, instead you should save it as string. For that you can use absoluteString property of URL
let imageURL = createPhotoURL()
print("imageURL: \(imageURL)")
UserDefaults.standard.set(imageURL.absoluteString, forKey: "URL_IMAGE")
And to retrieve, you will first retrieve the saved string from UserDefaults and then convert that into a URL
if let urlString = UserDefaults.standard.string(forKey: "URL_IMAGE"),
let retrievedURL = URL(string: urlString) {
print("retrievedURL: \(retrievedURL)")
}
I don't want to write the link " https://stackoverflow.com/questions/ask" as shown in this line. I want url to receive the last copied URL from clipboard, what should I add?
let url = URL(string: "https://stackoverflow.com/questions/ask")
Copy
let url = URL(string: "https://stackoverflow.com/questions/ask")
UIPasteboard.general.string = url
Paste
if let url = UIPasteboard.general.string {
url = url
}
I'm saving a UIImage to Core Data. So first, I convert it to NSData, then save it.
I need to get the URL for the image after it's saved. I'm doing this because I want to schedule a local notification with an attachment, and the only way to do it, AFAIK, is to with a URL.
Here is my code:
//my image:
var myImage: UIImage?
var imageData: NSData?
if let image = myImage {
imageData = UIImageJPEGRepresentation(image, 0.5)! as NSData
}
myEntity.setValue(imageData, forKey: "image")
And that's how I should add an attachment to the notification:
UNNotificationAttachment.init(identifier: String, url: URL>, options: [AnyHashable : Any]?)
I'm saving the image and scheduling the notification manually when the user taps on a button to save the image.
Please let me know if you need extra info.
You can't get the URL. If you configured this property to use external storage then yes, technically there could be a file URL. Maybe. But there's no documented way to get it, and anyway it might not exist after all-- because the external storage setting doesn't require Core Data to use external storage, it just allows it to do so.
If you didn't use that setting then there's never any URL since the image is saved as part of the SQLIte file.
If you need a file URL for the image, save the image to a file separately from Core Data and save the file name as an entity property. Then the file URL is wherever you saved the file.
And an implementation of how I saved it and then got the URL in practice when I had the same challenge:
Swift 5:
func getImageURL(for image: UIImage?) -> URL {
let documentsDirectoryPath:NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let tempImageName = "tempImage.jpg"
var imageURL: URL?
if let image = image {
let imageData:Data = image.jpegData(compressionQuality: 1.0)!
let path:String = documentsDirectoryPath.appendingPathComponent(tempImageName)
try? image.jpegData(compressionQuality: 1.0)!.write(to: URL(fileURLWithPath: path), options: [.atomic])
imageURL = URL(fileURLWithPath: path)
try? imageData.write(to: imageURL!, options: [.atomic])
}
return imageURL!
}