how to pass default URL into default browser in IOS? - ios

I have pass this below code in viewDidLoad method but it take the https:// not the the passed URL so i am confused please tell me the solution of my problem if anybody can know.
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *customURL = #"http://www.dannoshottips.com/";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}

not required for add https:// in url because your site is not use https:// protocol.

Your site www.dannoshottips.com not https protocol used.
this site not secure protocol used so you can not use https://.
http://www.dannoshottips.com/
1: http://www.dannoshottips.com/ is open in browser
https://www.dannoshottips.com/ is not open in browser.

Related

UIWebView not displaying my webpage

For some background info, the webpage I'm trying to display is a web app currently being hosted on AWS's EC2. The backend is Python w/ Flask and the frontend is just simple HTML/CSS. The URL has HTTP, as it isn't secured with HTTPS yet. When the url for this webpage is opened, the first thing the browser asks is for login credentials (the browser asks, not the website). This page does load in mobile Safari on my iPhone, and Safari does successfully ask for the credentials. If I enter them in correctly, it will correctly load the page.
So I've tried both Allow Arbitrary Loads under App Transport Security Settings as well as a customized Exception Domain with the following keys:
App Transport Security Settings Dictionary
Exception Domains Dictionary
my website URL Dictionary
NSIncludesSubdomains Boolean (YES)
NSExceptionAllowsInsecureHTTPLoads Boolean (YES)
NSThirdPartyExceptionAllowsInsecureHTTPLoads Boolean (YES)
NSExceptionMinimumTLSVersion String (TLSv1.0)
NSExceptionRequiresForwardSecrecy Boolean (YES)
However, whenever I launch the app on the simulator all I'm getting back is a white screen (can post screenshot if needed).
Here's my code in ViewController.swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "My URL inserted here")
let request = NSURLRequest(URL: url!)
WebView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
If I use Allow Arbitrary Loads, when I look in the output box, it does not say "App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file." When I configure the Exception Domain correctly (with Allow Arbitrary Loads removed) it won't give me the message either. Only sometimes when I change around the settings using Exception Domain (again, with Allow Arbitrary Loads removed) do I get this output.
I'm starting to think the issue goes beyond security, and any advice or steps I could take to try and fix this issue would be much appreciated, thanks!
The white screen is a bit odd, assuming that a 401 would result in a standard error page, but maybe the server set up a white page for this.
My guess is that setting username and password directly in the URL doesn't work, you shouldn't do that anyways, but instead rely on WKWebView's webView:didReceiveAuthenticationChallenge: delegate method.
Here's some sample code hopefully working/helping:
#import "ViewController.h"
#import WebKit;
#interface ViewController () <WKNavigationDelegate>
#property (nonatomic, strong) WKWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:[WKWebViewConfiguration new]];
self.webView.navigationDelegate = self;
[self.view addSubview:self.webView];
[self.webView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[_webView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_webView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[_webView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_webView)]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSURL *target = [NSURL URLWithString:#"http://yourhost.com/possiblePage.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:target];
[self.webView loadRequest:request];
}
- (void)webView:(WKWebView *)webView
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
NSURLCredential *creds = [[NSURLCredential alloc] initWithUser:#"username"
password:#"password"
persistence:NSURLCredentialPersistenceForSession];
completionHandler(NSURLSessionAuthChallengeUseCredential, creds);
}
#end
This is basically the implementation file of a simple ViewController (like from the single view template of XCode). It also shows you how you can add a WKWebView. Definitely make sure to check out all the delegate methods and such so you know what the thing can do for you.
Obviously, password and username have to be set somehow, I guess you can use a simple alert popup to have the user enter this info (this would be similar to Safari in principle). For the first test you can just hardcode it. Also note I set a sample subpage there, just use the exact same URL you would usually use on a desktop browser. Oh, and since the server doesn't have SSL, you need to allow arbitrary loads.
Edit:
RPM gave a good related comment below (thanks) that I had not originally thought about. The method may (actually will very likely) be called multiple times. This ultimately also depends on the website you load. RPM's explanation for why a site may appear plain white is spot on.
In any way, the webView:didReceiveAuthenticationChallenge:completionHandler: method above is just a simple example assuming you know the PW and username. Generally it will be more complex and you shouldn't just open an input dialog every time it is called for the user to enter credentials. As a matter of fact, the provided challenge offers ways to set a specific call to this delegate method into relation to previous calls. For example, it has a proposedCredential property that may already have been set. (Whether that's the case for loading multiple resources I don't know on the top of my head, just try that out.) Also, check its previousFailureCount, etc. A lot of this may depend on the site you load and what it needs to get.

How to display the Authentication Challenge in UIWebView for local network url?

I am trying to access a secure for local network url through UIWebView. When I access it through safari, i get an authentication challenge but the same does not appear in my UIWebView in the application. How can I make it appear?
E.g. http://292.168.1.54/TestWeb/Test.pdf
This url working in safari browser but the same url does not appear in my UIWebView.
Any pointers, sample code or links will be very helpful. Thanks a lot.
There are two ways to get to the authentication (in your case probably basic auth) challenge.
-[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] will give you the request. Now you just start a second request to the same URL and use [NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:] to get the challenge. Then you present a dialog and ask the user for credentials. Save the credentials in NSURLCredentialStorage and then reload the page.
Create a subclass of NSURLProtocol that handles http and https. Similar to this answer and get the authentication challenge there.
I hope this code will help you.
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://skinC.com/abc/"];// here you can write your url that you want to open
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self setNeedsStatusBarAppearanceUpdate];}

how to launch safari even when the url is not valid

I know how to launch safari using the:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
But, this method returns false when the url is not valid, and nothing happens. So, I'd like to launch safari even when the url is invalid. Is it possible?
NO it is not possible to open URL (which is invalid) with safari or any other bowser in iOS or another OS, So it's better to make valid URL rather then fighting with it.
Using below code check, if the URL is valid or not.
NSURL *candidateURL = [NSURL URLWithString:candidate];
if (candidateURL && candidateURL.scheme && candidateURL.host)
{
//Open that URL using openURL method...
}
else
{
//Open any valid hardcoded URL using openURL method
}
Short answer? No.
Long answer? Technically No. But there is a workaround. If you use a redirection service/url shortener, you can hide your invalid url. For example, try this url: http://goo.gl/zRci0B
Safari will be able to open the link but it wont go anywhere. So what ever url(valid/invalid) you want to open, always wrap it with a redirection service.

How to open an url from the browser with a parameter in ios

I want to open an URL from the browser in ios. I know how to open an normal url.. but here I want to pass a parameter to the url.... This is what I used to open the url from the browser
NSURL *url = [NSURL URLWithString:#"http://www.iphonedevelopertips.com"];
[[UIApplication sharedApplication] openURL:url];
Then how can I modify this according to pass a parameter
Thanks
here i pass the country name. you can pass anything what you want.
CountryName=#"India";
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:#"http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=%#",CountryName]];
do like this pass whatever you want pass like this.

Phonegap 2.0 , Cordova external links

I have a situation and I spend so much time in google with no success.
I want to open in my app (IOS), external links which are like that
"External Link" to open in safari not web view. where I have set up in "Cordova.plist"
OpenAllWhitelistURLsInWebView : true
Because I hav as well some Iframe inside my app, where I want to keep user in web view and not to leave the app.
An I have no idea why target="_blank" doesn't work, where here :
https://build.phonegap.com/blog/access-tags it says:
"
on iOS, if a domain is whitelisted, a link will take over the entire webview, unless the link's target is _blank, in which case it will open in the browser. If it is not, it will log an error on the device, while doing nothing from the user's perspective.
"
I tried to use JS way as well,
window.open('http://www.google.com', '_blank');
with no success :(
PS: I do have all my links in External host set up
I appreciate any help.
Thanks!
What you need is this charmer in your MainViewController.m
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:#"http"] || [[url scheme] isEqualToString:#"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
It works with me using following setup:
Cordova.plist:
OpenAllWhitelistURLsInWebView: false
external Hosts: google.com
Link in Code:
< a target='_blank' href='http://maps.google.com/maps?q=something'>
Hope it works for you as well :)

Resources