Check the validity of a domain name - ios

I need to make sure that a URL that the user inputs goes where it should go. I mean if the user inputs "http://google.com/blablabla" or "http://google123.com/blablabla". I need to figure out that the second url is not correct because it won't open google's web-site. Is there some method in Swift to do that? Or do I just need to check if the URL contains "http://google.com"?

1. Malformed URL
To verify whether or not the URL itself is malformed, let the OS do it:
(check that the URL has all the parts it needs, namely a scheme and a host)
if let url = URL(string: rawStringInput),
let _ = url.scheme,
let _ = url.host {
// rawStringInput is well formed
}
You can then query all sorts of information from the URL, such as scheme (http), the host (google.com), the path (blablabla), fragment, port and so on.
2. Valid URL
If you want to know whether or not the URL is reachable (i.e. the remote server responds to it), you actually need to execute the request.

Related

How to get the redirect url from deeplink in Swift

How to get the URL from the given redirect deeplink string?
For example, if I have a deeplink string as
myapp://open-browser/https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
I would like to get the return result as
https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
I tried several methods from Apple documentation
Accessing the Parts of a URL,
the closest one was to combine path and query like below screenshot
Does anyone have a better way to do this?
I get your question, I think you want to get only the url starting from https://, please correct me.
If so I think I could remove the deeplink url scheme myapp:// and the host open-browser/ like
let url = "myapp://open-browser/https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME"
let deeplinkBase = "myapp://open-browser/"
let newUrl = url.absoluteString.replacingOccurrences(of: deeplinkBase, with: "")
Then you could use the newUrl as you like since we got the expected result now
https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
My example for the deeplinkBase string is hardcoded, but I guess you could use enums or any handlers for it later.

Change the browser URL bar text

I own a domain in a hosting provider (just the domain). This domain is pointing to another web address:
domain.com-->anotherdomain.dom/path
In another side, I have added my domain to my Cloudflare account, like this:
domain.com-->Cloudflare-->anotherdomain.dom/path
The issue is that after typing domain.dom, the URL text in the browser URL bar is anotherdomain.dom/path, and I need it to be domain.com.
Is it possible to have domain.com in the broswer URL bar? Do I have to write some code inside my .htaccess file or something inside anotherdomain.com? Do I have to do something inside Cloudflare (maybe with "workers")?
It sounds like currently, your domain domain.com is set up as a redirect. When a user visits domain.com in their browser, the server (Cloudflare) responds with a message saying: "Please go to anotherdomain.com/path instead." The browser then acts like the user actually typed anotherdomain.com/path in the address bar.
It sounds like what you want instead is for domain.com to be a proxy. When a request comes in for domain.com, you want Cloudflare to fetch the content from anotherdomain.com/path and then return that content in response to the original request.
In order to do this, you will need to use Workers. Cloudflare Workers allows you to write arbitrary JavaScript code to tell Cloudflare how to handle HTTP requests for your domain.
Here's a Worker script that implements the proxy behavior that you want:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Parse the original request URL.
let url = new URL(request.url);
// Change domain name.
url.host = "anotherdomain.org";
// Add path prefix.
url.pathname = "/path" + url.pathname;
// Create a new request with the new URL, but
// copying all other properties from the
// original request.
request = new Request(url, request);
// Send the new request.
let response = await fetch(request);
// Use the response to fulfill the original
// request.
return response;
}

Create URL with /#/ in path and open in safari

I use firebase dynamic links which contain an URL to our webapp.
If the dynamic link is opened, the deep link is fetched.
So far so good. As we use the /#/path pattern in our webapp to redirect a user to different sections, we have a problem now, creating such an url in our iOS application after we have to append a new parameter in the url
If this example URL is in our dynamic link
https://domain/#/main/page?utm_source=app&utm_medium=button&utm_campaign=testcampaign
i get it and have to append a parameter for autologin mechanism in our webapp.
So here is the point where i fail at two different approaches.
Getting the string from the url and appending the token parameter and value.
This approach works fine until I have to parse the urlString back to an URL object. The /#/ inside causes an error when creating a new URL object.
I try to replace /#/ with /%23/ (encoded #), but this does not work on our ngnix / webapp infrastructure.
Appending the token parameter with new URLQueryItem in URLComponents.
This approach leads to a wrong URL resulting in (token is the added parameter)
https://my-stage.bikersos.com/?token=tokrenvalue#/main/premium?utm_source=app&utm_medium=button&utm_campaign=testcampaign
I append the URL Query Item with this extension
extension URL {
func addQueryParams(newParams: [URLQueryItem]) -> URL? {
let urlComponents = NSURLComponents.init(url: self, resolvingAgainstBaseURL: false)
guard urlComponents != nil else { return nil; }
if (urlComponents?.queryItems == nil) {
urlComponents!.queryItems = []
}
urlComponents!.queryItems!.append(contentsOf: newParams)
print(urlComponents!)
return urlComponents?.url
}
}
does anybody has an idea how I could solve this problem? I personally prefer the second approach, if it is possible to append the parameters at the end
I figured it out how it has to be done with firebase and utm parameters (this link can be added as deep link in a dynamic link for firebase)
https://example.domain.com/?utm_source=newsletter&utm_medium=button&utm_campaign=testcampaign#/path1/subpath?webappparam1=1&webappparam2=asdf
This way all utm parameters are applied and the path will be available in the web application too.
You can add new query params using the iOS SDK but be aware, they are added at the utm parameters location.
If you need to add them at the end, check if there already exists an ? in the path and write your own appending at the end of the url.

Need to open a browser and launch a URL and extract some values from the URL

Initially I tried to get the source of the URL and and extract the values which I was looking for the URL as given below
Dim URL = http://www.google.com
Dim oHTTP : Set oHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
oHTTP.setproxy 2,"<Proxy server:Port>"
oHTTP.Open "GET", URL, False
oHTTP.Send
And since the values which I am looking in the URL are proxy specific. And the values vary based the proxy server name which give explicitily as mentioned the above snippet.
I realised that giving proxy server name explicitlly is not the right approach since PAC file are used. Giving the proxy name only lists down the servers under that name.
So I thought opening a URL from browser such that it takes the PAC details in account and then draw out the neccessry values from there.
And opening a URL from the default browser is achieved as given below, I wanted to know is there a way extract the values from the browser.
CreateObject("WScript.Shell").Run("http://www.google.com")

How make WCF allow dots(.) in url?

I use WCF and have a method like this:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "LoadProducts/{key}/{price}")]
XmlDocument LoadProducts(string key, string price= null);
price is string, inside LoadProducts I will try to parse it from string to double and do my other operations.
But in url, I can not get request any parameter for price like '24.25', '0.253' etc. It does not allow any value with dot.
localhost:13448/RestService.svc/LoadProducts/null/41.145
I get error "Please review the following URL and make sure that it is spelled correctly. "
How can I solve this?
Dots already have a meaning in a URL, they separate the target hostname, IP address or in the path they separate the resources from its extension. You will need to URL encode your request URL.
In .NET there is a method called UrlEncode to help you encode URLs. It is:
string url = "http://localhost/MyService/MyKey/24.25";
string encodedUrl = HttpUtility.UrlEncode(url);
Check out the MSN documentation for UrlEncode for more details.
I solved my issue. I switched server from Visual Studio Development Server to Local IIS Web Server, url took dot symbol inside parameter.

Resources