Different URL Load with shouldStartLoadWithRequest - ios

i am currently using http://something.com this link inside the uiwebview for my ios app. In this dummy website there is another link. I wanna open native ios camera screen after clicking this link. Right now i am able to open cam only for http://something.com this url. But i need to open the cam by clicking another link inside this http://something.com. When i click the other link url is changing to ``http://something.com\webapp`. So how can i make it work? Any ideas?
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSRange range = [urlString rangeOfString:#"http://something.com"];
if (range.location != NSNotFound) {
[self showCamera]; //camera starts
return YES;
} else {
return NO;
}

This should do the trick:
if ([request.URL.absoluteString containsString:#"webapp"]) {
[self showCamera];
return NO;
} else {
return YES;
}

Related

Xcode WebView. Open target="_blank" links in Safari

I have a small app for iOS that uses WebView. I need links that contain target = "_blank" to open in Safari
I found a solution. But it does't work for me.
https://stackoverflow.com/a/15048074/4489534
All links are now opened in Safari, and all links containing "?openInSafari = true" too. But external(downloadable) files like PDF open in WebView.
I can't understand why the condition doesn't work
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = [request URL];
NSString *string = [url query];
if ([string rangeOfString: #"openInSafari=true"].location != NSNotFound){
[[UIApplication sharedApplication] openURL: url];
NSLog(#"Open in Safari");
return NO;
}
}
NSLog(#"Open in WebView");
return YES;
}
EDITED
When i click to link containing "?openInSafari = true", i get "Open in Safari openInSafari=true"
When i click to normal link, i get "Open in Safari (null)"
When i click to downloadable link to PDF file, i get "Open in WebView product_id=50&download_id=21"
When i click to direct link to PDF file, i get "Open in Safari (null)"

How to get url which I hit on UIWebView?

NSString *testString =[NSString stringWithFormat:#" Google"];
[webView loadHTMLString:testString baseURL:nil];
I want to get the URL when I click on this UIWebView content in
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:inRequest.mainDocumentURL];
return NO;
}
return YES;
}
delegate method.
When I click google hyperlink in webview it gives
URL: applewebdata://
Please help me.
You question is not very clear if you want to get the tapped url on webview or the webview's loaded page url ,
If you want to get webview loaded page url you can use NSURLRequestObject,something like
NSString * url = [request URL];
If you want to get the clicked link url , you will have to use this java script
NSString *url = [webView stringByEvaluatingJavaScriptFromString:#"window.location"];
you can do like this way
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString rangeOfString:#"www.google.com"].location!=NSNotFound)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}
For Swift:
Whenever your webpage is loaded you can use this, I use it for a label in this case:
webUrlLabel.text = webView.request.URL.absoluteString
For Objective-C:
NSString *currentPage = [NSString stringWithFormat:#"%#", webView.request.URL.absoluteString];
webUrlLabel is a UILabel
webView is my UIWebView
You already have NSURLRequest object. Access url from that.
inRequest.URL
way 1:
NSString *currentURL = myWebView.request.URL.absoluteString;
Way 2:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
yourTextBox.text = [url absoluteString];
return YES;
}
Please let me know if it works. Thanks

How to open Safari with Cordova 3.5 on iOS 7.1?

I read numerous threads about this problem, but it seems that usual solutions do not work with Cordova 3.5 and iOS 7.1.
So, I'm trying to open an URL in the device default browser, so that the user is able to come back to the app.
This is what I tried:
Does not work:
<a onClick="navigator.app.loadUrl('http://targetURL.com',{openExternal:true})">Link A </a>
Opens the target URL in full screen; the user can not go back to the app:
<a onClick="window.open('http://targetURL.com/','_system')">Link B</a>
Also tried the href&target='_blank' approach, with no more success...
I finally found a way to do it by adding this to the implementation of MainViewController in MainViewController.m, thanks to this thread.
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
NSString *str = url.absoluteString;
NSRange range = [str rangeOfString:#"http://"];
NSRange range1 = [str rangeOfString:#"https://"];
if (range.location != NSNotFound || range1.location != NSNotFound) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}

Prevent to open appstorelink from UIWebView

I´ve a small app with a UIWebView for web surfing in it. On some pages opens the Appstore for promotional purposes (that is annoying). How can i prevent that? Is there a special method? or just fake the browserid?
http://bjango.com/articles/ituneslinks/
here is the complete reference for link formation of the appstore, itunes.
from above reference link, apple.com is common for all kind of links.
So we can create regex or simply search string "apple.com" from url and avoid to load in webview.
If you wanna,use without regex following code may be help you :
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *currentURL = request.URL;
NSString *urlString = url.absoluteString;
NSRange range = [urlString rangeOfString:#"apple.com"];
if (range.location != NSNotFound)
return YES;
else
return NO;
}
Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType: to detect what URLs are getting loaded in the webview. App store URLs usually contain itunes.apple.com or phobos.apple.com.
When you encounter such urls are clicked, you can return NO from the web view delegate method to stop loading the url.
Hope that helps!
Use the following UIWebView Delegate method for this :
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *currentURL = request.URL;
NSString *urlString = url.absoluteString;
NSRange range = [urlString rangeOfString:#"https://itunes.apple.com"]; // Change URL if other than this.
if (range.location != NSNotFound)
return YES;
else
return NO;
}

How to make html link within NSString?

I have NSString as follows
NSString *textOutStations = [NSString stringWithFormat:#"Hello Everyone. Please check out website:<br> http://www.google.com/</br>"];
I want to show google.com as below in URL, as I will load this in UIWebView.
www.google.com
So, whenever user click it, It must open Safari in iPhone.
NSString *textOutStations = [NSString stringWithFormat:#"Hello Everyone. Please check out website:<br> http://www.google.com/"];
[self.webView loadHTMLString:textOutStations baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Then in UIWebView delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// Opening safari
[[UIApplication sharedApplication] openURL:request.URL];
....
}
Try this one
NSString *textOutStations = #"<html><head><title></title></head><body><div> Hello Everyone. Please check out website:<br/> http://www.google.com/ </div></body></html>";
[self.webView loadHTMLString:textOutStations baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
This will help you......
You can not specify a URL with in a string and have it be clickable, this type of functionality is dependant on the object to which is using it, aka, UITextView UITextField UILabel UIWebView etc,
A UIWebview will show your url with in the webview it will not open the link in safari. IF you want to load it in ui web view, it's already ansered above, if you want to open it in safari, you have to do
[[UIAplication sharedApplication] openUrl:urlObject];
if you want to open it to be text with in a UITextView i would suggest this other stack overflow link here
I checked the link option in Attribute Inspector of UIWebView and it detected the link.
Method to OPEN in SAFARI...
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}

Resources