iOS Swift NSURL Web Address Not Working - ios

I'm attempting to get data from a server. My URL is (for example): http://dommain.com/action
When I execute
let nsURL = NSURL(fileURLWithPath: url)
the URL gets translated into http:/domain.com/action. Then when I execute this:
let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) {
(data, response, error) in
print("data=\(data) response=\(response) error=\(error)")
}
the error property says it can't find "file:///http:/domain.com/action
I'm not sure what I'm doing wrong.

Replace
let nsURL = NSURL(fileURLWithPath: url)
with
let nsURL = NSURL(string: url)
An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.
NSURL(fileURLWithPath: url) initializes and returns a newly created NSURL object as a file URL with a specified path. For network URL use NSURL(string: url) instead.
For Swift 3 and upwards, use URL instead of NSURL:
let u = URL(string: url)

Related

Add parameters to URL (resource from path, WKWebView)

I am using Swift 4 & WKWebView. The HTML pages are included in the app (not downloaded from server).
For configuration purpose I would like to add parameters to the URL (e.g. ...index.html?debug=true).
Currently I am using the following approach to load the page:
let indexHTMLPath = Bundle.main.path(forResource: "f7Shoma/index", ofType: "html")
let url = URL(fileURLWithPath: indexHTMLPath!)
let request = URLRequest(url: url)
...
appWebView!.load(request)
How can parameters be added/passed to the page?
You can use URLComponents to create a URL with a query component:
var components = URLComponents(string: indexHTMLPath)
components?.queryItems = [URLQueryItem(name: "debug", value: "true")]
if let result = components?.url {
let request = URLRequest(url: url)
appWebView!.load(request)
}

crash uiwebview without http or https in swift [duplicate]

This question already has answers here:
Add http:// to NSURL if it's not there
(5 answers)
Closed 5 years ago.
I want show the URL in Web View
here is my code
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
if user forget to write http or https the app crashed how can I resolve this error
Just use starts(with:) on string to detect if the url string starts with http/https, and if not, add the "http://" yourself (also, use safe if let instead of force unwrap):
var urlString: String = "www.apple.com"
if !urlString.starts(with: "http://") && !urlString.starts(with: "https://") {
urlString = "http://\(urlString)"
}
if let url: URL = URL(string: urlString) {
let urlRequest: URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
}

How to make CONNECT call in iOS?

NSURLSession supports POST and GET; does it also support CONNECT?
If so, what is the exact syntax?
Thanks!
Use NSMutableURLRequest to set whatever HTTP method you want
let session = NSURLSession()
guard let url = NSURL(string: "http://www.google.com") else {
fatalError("Error creating NSURL for http://www.google.com")
}
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "CONNECT"
let task = session.dataTaskWithRequest(request)
task.resume()

How to save UIWebView content for offline display?

I'm using Xcode 7.2 and Swift to create an iOS app, on this app I display the content of my website, however if I was offline the content will not be shown. So I want to cache the webpage and display for offline.
After I declared everything I'm using the following code :
var URLPATH="http://google.com"
let requestURL = NSURL(string: URLPATH)
let request = NSURLRequest(URL: requestURL!)
WB.loadRequest(request)
HTML to Data
if let url = URL(string: urlString) {
person.setValue(try? Data(contentsOf: url), forKey: "content_article")
}
Data to WebView
if let savedObject = fetchedObjects?.first,
let data = savedObject.content_article as? Data,
let baseStringUrl = savedObject.content_url,
let baseURL = URL(string: baseStringUrl) {
webView.load(
data, mimeType: "text/html",
textEncodingName: "",
baseURL: baseURL
)
}

How to download the file from Parse.com using the REST API?

I found this question at Parse.com file download question, however, it only mentions that the file can be downloaded from the Url. Also, the Parse.com REST Documentation only discuss upload file and associated with an Object.
I tried to access the URL only, but it returns an error.
Can anyone help with an example in Swift and using the REST API, how to download the actual file once you get the URL after querying the object?
This is the error I get:
Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x7fa39940dcc0 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "(null)"}
This is my code in Swift 2.0:
func downloadFile(){
let str = "http://files.parsetfss.com/c426b506-44da-447d-91d0-93f13980758b/tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg"
let request = NSMutableURLRequest()
request.HTTPMethod = "GET"
request.addValue(appID, forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue(apiKey, forHTTPHeaderField: "X-Parse-REST-API-Key")
let url = NSURL(fileURLWithPath: str)
request.URL = url
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {
(data, response, error) in
if (error == nil) {
do {
let image = try NSJSONSerialization.dataWithJSONObject(data!, options: [])
} catch {
}
}
})
task.resume()
}
This is the JSON response I get when I query the object, and the url is what needs to be used in order to get the file:
"picture": {
"__type" = File;
name = "tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg";
url = "http://files.parsetfss.com/c426b506-44da-447d-91d0-93f13980758b/tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg";
I bet your problem is in this line:
let url = NSURL(fileURLWithPath: str)
"str" is a remote URL and not a local file path, and what that API is trying to do is create a local "file:///" url from the string you provided it.
Why not do:
let url = NSURL(string: str)
and see if that works better?

Resources