Detecting onclick on an ASP page on webView Objective C - ios

I am using Classic ASP for my server page. When the following URL is loaded on webView. I will detect the link when user click on the webView using - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
If classbook.asp is detected when a new URL link is clicked on the webView, I will trigger a segue to load a new VC and load the new webView
However there is another google map link on the webView trigger by the following Classic ASP onclick code.
How do I detect when this google map link is clicked on the webView? Currently, the above method which is used to detect URL links cannot detect onclick
Server Classic ASP
<div id="map" onclick="window.open('http://maps.google.com/maps?q=<%=sLat%>,<%=sLongi%>(<%=sFCName%>)', '_system');"></div>
Objective C
#interface ClassesDet (){
AppDelegate *appDelegate;
NSString *sURL;
NSString *sURLFav;
}
#end
#implementation ClassesDet
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
sURL = #"https://www.share-fitness.com/apps/class_det.asp?classcode=CLS100032&access=C&memcode=SF100035&catcode=YG&fccode=KL00059&dtpclass=24/04/2018&tpfrom=20:15&fav=false&lang=EN&encode=20a728210f0869f04aab6cf1682881816a36f0bd47cc894ac4d8dd22dd0ded24"
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ClassBook *target = segue.destinationViewController;
if ([segue.identifier isEqualToString:#"ClassBook"]) {
NSLog(#" To ClassBook sURL : %#", sURL);
target.urlString = sURL;
} else {
NSLog(#" Else to Other VC and the sURL : %#", sURL);
target.urlString = sURL;
}
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
NSString *theString = [URL absoluteString];
NSRange match;
NSLog(#"The URL Clicked : %#", sURL);
match = [theString rangeOfString: #"classbook.asp"];
//---If the URL string does not contain classbook.asp meaning it is loading the webview.
if (match.location == NSNotFound) {
NSLog(#"Load the webView with this sURL : %#", sURL);
return YES; //---Load webview, the URL will be sURL nothing to do with theString
} else {
sURL = theString;
NSLog(#"Trigger the Segue ClassBook and pass the sURL : %# Over ",sURL );
//---Calling the following method will trigger the segue and redirect to another viewController.
[self performSegueWithIdentifier:#"ClassBook" sender:self];
return NO; //---Don't load the webview
}
}
#end

Check out this answer. The problem you're having is that shouldStartLoadWithRequest: doesn't get called on JavaScript window.open(). The answer at the link has one way of making sure your code gets called. I was thinking one of the delegate methods would catch this but now I don't see it. Perhaps I was thinking of WKWebView.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([webView.request.URL.absoluteString containsString:#"http://maps.google.com/maps?q"]) {
//Your logic if require
}
else {
//Your logic if require
}
return YES;
}

Related

iOS webview load another url with Safari

I am making an app for iOS, use UIwebview to load my website in the app. I want all links (besides my website) open in Safari. Read many articles and tried many different suggestion still can't make it.
I am not familiar in coding, use Xcode 6.3.2
Here is my code of ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://www.mywebsite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Check if this was a click event and then some other criteria for determining if you want to launch Safari.
if (navigationType == UIWebViewNavigationTypeLinkClicked
&& [ [ request.URL scheme ] isEqualToString: #"http" ] ) {
[[UIApplication sharedApplication] openURL:request.URL];
// Return false to indicate to the UIWebView to not navigate to the linked target
return false;
}
// Return true so that the UIWebView loads the link target
return true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
please help
Update your method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
in something like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestURL = [[request URL] absoluteString];
if ([requestURL rangeOfString:#"mydomain.com"].location != NSNotFound) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}

iOS UIWebViewDelegates not called

I have a UIViewController class named WebScreenViewController, which is having an UIWebView. Here is the class implementation file:
#implementation WebScreenViewController
#synthesize urlString;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
// Do any additional setup after loading the view.
[self gotoUrl:self.urlString];
//[self gotoUrl:#"http://www.google.com"];
}
#pragma mark - UIWebView Delegates
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
NSString* urlStr = url.absoluteString;
NSLog(#"shouldStartLoadWithRequest. URLStr = %#", urlStr);
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* html = [webView stringByEvaluatingJavaScriptFromString:#"document.body"];
NSLog(#"webViewDidFinishLoad: HTML string = %#", html);
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"didFailLoadWithError. ERROR: %#", error);
}
#pragma mark - Member Methods
-(void)gotoUrl:(NSString*)urlStr
{
NSLog(#"Calling website %# in UIWebView...", urlStr);
NSURL* url = [NSURL URLWithString:urlStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
#end
The above code work well on one of my testing device but did not work on another. It worked on my iPod Touch with the latest iOS 8.1.2. But it didn't work on my iPad3 with iOS version 7.0. When I run it on my iPad3, it will call the gotoUrl: method and the log "Calling website %# in UIWebView..." will be displayed. And that's it. I receive no more logs from the delegates. To check the internet connections, I tried using Safari on the same device and it was working.
Check whether you've connected the web view's outlet correctly in iPad storyboard if you're using different storyboards for iPhone & iPad.
Also check that the web view is not nil.
Hope this helps..
NSString * strURL = [NSString stringWithFormat:#"http://www.google.com/"];
NSURL *loadURL=[NSURL URLWithString:strURL];
NSURLRequest *loadRequest = [NSURLRequest requestWithURL:loadURL];
[webView loadRequest:loadRequest];
webView.delegate=self;
Try this way in your view did load or if you have URL you can replace first line like this
NSString * strURL = [NSString stringWithFormat:self.urlString];

Open links in safari instead of inside webview

I am a beginner in making iOS applications. I have made a simple webview showing my web page. The problem is that every link that is pressed in my web page opens in the webview. I want some links to open in safari. I would like links starting with "..something" to be opened inside the webview and every other link to be opened in safari. I also have a button for email and dial which i want to open in the dial app and email app on the phone. Is this a possibility? please explain simple.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize webView;
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:#"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I made the same application for android using java with this code below
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try{
System.out.println("url called:::" + url);
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
} else if (url.startsWith("http:")
|| url.startsWith("https:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} else if (url.startsWith("mailto:")) {
MailTo mt=MailTo.parse(url);
send_email(mt.getTo());
}
else {
return false;
}
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}
You'll need to make your controller a UIWebViewDelegate and implement the webView: shouldStartLoadWithRequest:navigationType: method.
#interface ViewController () <UIWebViewDelegate>
viewDidLoad should look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
webView.delegate = self;
[webView loadRequest:requestURL];
}
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked && [self shouldOpenInSafari:[inRequest URL]]) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
- (BOOL)shouldOpenInSafari:(NSURL*)url
{
if ([url.scheme isEqualToString:#"mailto"]) {
return YES;
}
else if ([url.scheme isEqualToString:#"tel"]) {
return YES;
}
else if (([url.scheme isEqualToString:#"http"] || [url.scheme isEqualToString:#"https"]) && [url.host isEqualToString:#"example.com"]) {
return YES;
}
return NO;
}
Then you'll need to implement the shouldOpenInSafari: method. The openURL: method can also handle tel: and mailto: links.
Some code from here.
For the urls which you want to open in web view, use the same code which you have.
For opening in safari, use this
NSString* launchUrl = #"URL to open in safari";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];

UIWebView Launch in Safari

I know that this has been brought up many a times and answered twice as many on this site, however I think I may have something a little different and need to know if it is possible.
I am trying to load a banner ad from a website into a UIWebView in the app. All of this works flawlessly. However no matter what code I have tried to implement I cannot get it so that when the ad is clicked in the app it will launch in safari.
Basically I am wanting to have my own Ad server. The ad is managed ad hosted on our site. The banner has the link embedded into it by the server.
Here is the code I am using.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *string;
string = #"http://www.samplesite.com/mobile_ads";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.adBox loadRequest:requestObj];
}
-(BOOL) adBox:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Any ideas of where I should go?
In your UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method, do something like the following (assuming your ads have part of their URL identifiable):
- (void)methodThatCreatesTheWebview {
UIWebView *webview = [[UIWebView alloc] init];
webview.delegate = self;
}
// Portion of the URL that is only present on ads and not other URLs.
static NSString * const kAd = #"adSubdominOrSubDirectory";
// pragma mark - UIWebViewDelegate Methods
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString rangeOfString:kAd] != NSNotFound) {
// Launch Safari to open ads
return [[UIApplication sharedApplication] openURL:request.URL];
} else {
// URL isn't an ad, so just load it in the webview.
return YES;
}
}

Open links in Safari instead of UIWebVIew?

I have an app with a UIWebView inside a UIViewController. I load HTML from a web service as a string like this:
self.webView loadHTMLString:_string baseURL:nil
Is it possible for the HTML links in this string to be opened in the browser and not in the UIWebView in my app? How can I do this?
I have tried this in the UIViewController that "hosts" the UIWebVIew:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
It doesn't seem to be working....
Any ideas?
Have you set the delegate of the UIWebView to your UIViewController? There's nothing obviously wrong with your code as far as I can see, so it's likely to be something small like that.
Add this in class..
#interface yourViewController : UIViewController
<UIWebViewDelegate>
Add this in View did load
- (void)viewDidLoad
{
[description loadHTMLString:string baseURL:nil];
description.delegate = self;
}
Add this in your .m file
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Note:
UIWebView *description;
#synthesize description;
Then It will work perfectly the way you deserve..!! :)
Set the delegate of the UIWebView to your UIViewController after that use this UIWebview method and check the condition, for example now webview current url is google.com, if suppose you clicked on gmail the url contains the string with gmail. Use the below method it opened a safari browser and automatically loaded that url.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
NSURL *url = [inRequest URL];
if ([[url absoluteString] rangeOfString:#"gmail"].location == NSNotFound) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
If you already setup properly the UIWebViewDelegate, simply doing
self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;
should work
Add this line (
self.webview.delegate = self;
)
For Example in viewController.m
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlString baseURL:nil];
self.webview.delegate = self;
Apple introduced a Safari View Controller on iOS 9. Safari View Controller brings all of the features user expect from Safari over to your app without ever leaving it.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)
Yes, in your hyperlink tag add
target="blank"
And one question mark will be fine, thank you

Resources