I want to load web view with linkedin authorization url but before loading url was not created stating that it was nil and web view not show any specific page.
This is the url string that was passed to URLRequest.
https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=*******&redirect_uri=****&state=linkedin1575812567&scope=r_liteprofile
below mention is the code for that.
let responseType = "code"
let state = "linkedin\(Int(NSDate().timeIntervalSince1970))"
var authorizationURL = "\(authorizationEndPoint)?"
authorizationURL += "response_type=\(responseType)&"
authorizationURL += "client_id=\(linkedInConfig.linkedInKey)&"
authorizationURL += "redirect_uri=\(linkedInConfig.redirectURL)&"
authorizationURL += "state=\(state)&"
authorizationURL += "scope=\(scope)"
print(authorizationURL)
let url = URL(string: authorizationURL)
let request = URLRequest(url: url!)
webView.load(request)
URLs can only contain a certain set of characters. Using other characters can cause such errors. Or even the allowed characters can also sometimes cause this, for example, an '&' is used as a delimiter and cannot be directly used in a URL query parameter value.
You can use this for encoding those chaotic characters :
let newURL = YOUR_URL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: newURL)
Related
In order to make HTTP request call using URLSession,I need to convert string to URL first so I have tried everything but just can’t convert this piece of string to URL(string: ).
This is the string:
“http://api-aws-eu-qa-1.abc-cde.com/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”
I can’t share the exact Url but format is all same.
The actual url with same format works fine in browser but no way in Swift, also that works fine in POSTMAN
I have also tried URLComponents which hepls me create the URL from components to URL but that fails with 400 status code response error.
I really appreciate the help, I am completely bogged down with this isuue and can’t proceed with my assignment.
Update: Tested with fiddler and request was not going through with this URL - "“http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99”"
But when removed this %E2%80%8B in fiddler and resend it worked.
Any thoughts ..?
String to URL
let url = URL(string: "the url string goes here")
URL to String
let url = URL(string: "the url string goes here")
let urlString = url.absoluteString
Creating URL from URLComponents
var url: URL? {
var components = URLComponents()
components.scheme = "https"
components.host = "domain goes here" //example: twitter.com
components.path = "/path/goes/here"
components.queryItems = [
URLQueryItem(name: "item1", value: string1),
URLQueryItem(name: "item2", value: string2)
]
return components.url
}
try to add HEADER to you request:
let url = URL(string : urlString)
var request = URLRequest(url : url)
request.setValue("Application/json", forHTTPHeaderField : "Content-Type")
Your URL is:
http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99`
The "host" portion is:
api-aws-eu-qa-1.abc-cde.com%E2%80%8B
%E2%80%8B encodes a ZERO WIDTH SPACE. This is a perfectly valid URL:
URL(string: "http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99")
=> Optional(http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99)
However, there is no such host as api-aws-eu-qa-1.abc-cde.com%E2%80%8B, so you should expect the actual connection to fail.
I expect that whatever is generating your URL strings has a bug.
If you are having trouble creating the URL itself (if URL(string:) return nil, then I expect this is not your actual URL string.
If you construct an URLComponents, the results are correct but may be slightly misleading:
URLComponents(string: "http://api-aws-eu-qa-1.abc-cde.com%E2%80%8B/v1/car-types/manufacturer?page=0&pageSize=10&wa_key=abc-cde-efg-44ccd99")?.host
Optional("api-aws-eu-qa-1.abc-cde.com")
While this looks like "api-aws-eu-qa-1.abc-cde.com", the string actually has a ZERO WIDTH SPACE at the end, which is invisible (but still part of the host field, which will correctly fail if you try to connect to it).
I am trying this code and my desired URL is www.tapaway.com.au but what I am getting is https://%02tapaway.com.au. I have write the first URL www.tapaway.com.au on my NFC tag so whenever someone scans the tag it opens my webpage. My code is
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
var result = ""
for payload in messages[0].records{
print("-> \(payload)") //Prints the Entire NFC Payload on the Tag.
result += String.init(data: payload.payload, encoding: .utf8) ?? ""
}
DispatchQueue.main.async {
var components = URLComponents()
components.scheme = "https"
components.host = "\(result)"
components.path = "/"
let url = components.url
print(url)
UIApplication.shared.open(url!)
}
}
So a URL is stored in an encoded form in NDEF records to save space.
The %02 is the code for the URI Identifier, "https://www." in this case
The tapaway.com.au is the URI payload
So when correctly decode the URL is "https://www.tapaway.com.au" to which you would be adding "https" on the front again.
So you need to decode the payload correctly before you can use it as a URL.
The 1st byte is the URI Identifier.
All other bytes are the shortened URL
See the Specification document for this type of NDEF record at https://github.com/haldean/ndef/blob/master/docs/NFCForum-TS-RTD_URI_1.0.pdf
It seems there is a convenience function for this
https://developer.apple.com/documentation/corenfc/nfcndefpayload/3153117-wellknowntypeuripayload
so probably (I'm not a swift programmer)
var url = payload.wellKnownTypeURIPayload()
I use the code to load URL path hash fragment (such as: index.html#/some-hash-path; but WKWebview not load url.
let path = "www/index.html"
let page = "#/some-hash-path"
let url = Bundle.main.bundleURL.appendingPathComponent(path + page)
self.webView.loadFileURL(url, allowingReadAccessTo: Bundle.main.bundleURL)
It seem that it convert # -> %23 which make invalid path
I got stuck too trying to build the url in an apparently correct way for local files.
The url must be created with URL(string:) initializer, prefixing it manually with file://, and loaded with loadFileURL().
let url = URL(string: "file://\(indexPath)#\(section)")
webView.loadFileURL(url, allowingReadAccessTo: Bundle.main.bundleURL)
Construct URL from string work as expected:
let path = "www/index.html"
let page = "#/some-hash-path"
guard let parsedUrl = URL(string: Bundle.main.bundleURL.absoluteString + path + page) else {
return
}
self.webView.loadFileURL(parsedUrl, allowingReadAccessTo: Bundle.main.bundleURL)
I am trying to open an Excel document that is located on a server. I wrote the following code but it always returns false for UIApplication.shared.canOpenURL(url as URL)
I think I am missing some requirement for deep linking to Excel. Why is iOS not able to understand ms-excel:ofe|u| format?
#objc static func openExcel() {
let originalString = "http://s000.tinyupload.com/download.php?file_id=23290165129849240725&t=2329016512984924072514118"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let encodedURLString = "ms-excel:ofe|u|" + encodedString! + "|n|TestDoc.xlsx|a|App"
if let url = NSURL(string: encodedURLString),
UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
} else if let itunesUrl = NSURL(string: "https://itunes.apple.com/us/app/microsoft-excel/id586683407?mt=8&uo=4"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
UIApplication.shared.openURL(itunesUrl as URL)
}
}
I have analyzed your code and found some mistakes. First, your URL was redirecting to somewhere, as per Microsoft documentation it can't handle redirecting URL's
The URL has to be encoded and must be a direct link to the file (not a
redirect). If the URL is in a format that Office cannot handle, or the
download simply fails, Office will not return the user to the invoking
application.
Here is Microsoft Documentation Link
The second mistake was you are only encoding the URL string containing site URL, you should consider the part after the scheme ms-excel: as a URL and should be encoded.
Because of improper encoding the let url = URL(string: encodedURLString) results nil that's why it is not working as expected.
Here is an example working code:
#objc static func openExcel() {
//replace the below url with yours. may be this one dosen't work
let originalString = "ofe|u|https://pgcconline.blackboard.com/webapps/dur-browserCheck-bb_bb60/samples/sample.xlsx"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let encodedURLString = "ms-excel:" + encodedString!
if let url = URL(string: encodedURLString),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
} else if let itunesUrl = NSURL(string: "https://itunes.apple.com/us/app/microsoft-excel/id586683407?mt=8&uo=4"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
UIApplication.shared.openURL(itunesUrl as URL)
}
}
Note: From iOS 9 you must whitelist any URL schemes your App wants to query in Info.plist under the LSApplicationQueriesSchemes key (an array of strings):
For example in our case:
When i try to open the URL in the question above I get redirected to this URL, so my guess would be that your code is fine, it just might be that your excel file you're trying to open is really an HTML page since tinyupload apparently blocks direct links to the files.
Maybe try opening a direct excel file download link, https://pgcconline.blackboard.com/webapps/dur-browserCheck-bb_bb60/samples/sample.xlsx (it was the first google result for 'xlsx file sample download')
Currently I have an iOS app that pulls prices and data from websites. So far its been working well, but I want to make it more accurate. To do so, I need to set the cookies for the URL request that I'm currently using String(contentsOf: _) for.
Current Process
let requestUrl: URL = URL(string: "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple")!
var content: String?
do {
content = try String(contentsOf: requestUrl)
} catch {
print("Error while converting an NSURL to String: \(error)")
}
if content != "" {
// I do things with the content of the requestUrl...
}
Could Use?
I thought that maybe I should use Alamofire instead to pull those website, and then parse the data.
I need to set the cookie that changes the store number to search, but have been unable to find a way to do so. Bellow is the code I have for pulling the websites data without setting a cookie.
let requestUrl: String = "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple"
Alamofire.request(requestUrl, method: .post).responseString { response in
if let content: String = response.result.value {
// I do things with the content of the requestUrl...
}
}
Other Claims
I have found many different ways to set cookies through Alamofire that don't work, but if Alamofire isn't the way to do it, please inform me. I really need this to work, and I'm open to any and every suggestion.
It took four weeks to the day, but I figured it out! URLRequest and Alamofire were my glorious answers!
Create the URL to call.
let requestUrl: String = "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple"
Next make the URLRequest with the URL string, and set its http method.
var urlRequest = URLRequest(url: requestUrl)
urlRequest.httpMethod = "POST"
Then set the cookies for the URLRequest.
urlRequest.setValue("myPreferredClub=4969", forHTTPHeaderField: "Cookie")
urlRequest.httpShouldHandleCookies = true
Finally send the URLRequest with Alamofire, and use the response data in whatever way I wish.
Alamofire.request(urlRequest).responseString { response in
if let content: String = response.result.value {
// I do things with the content of the urlRequest...
}
}