Im working on a web based application, it contains only one webView, the code below:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ((navigationType == UIWebViewNavigationTypeLinkClicked )) {
[[UIApplication sharedApplication] openURL:[request URL]] ;
}
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
// Set determinate mode
[HUD show:YES];
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
NSString *urls= [url absoluteString];
NSString *code = [urls substringFromIndex: [urls length] - 1];
if ( [urls containsString:#".html"] && ![code isEqualToString:#"#"] ) {
_webView.hidden= YES;
}
this code will open all the web views on safari , but i only need to open specific URLs on safari
for example :
http://xxxx/residential/ this must be opened on safari, while all the other pages should be opened on webView inside the application.
Any help
First thing is that you want to open specific urls that should open in Safari instead of UIWebView. But there is not any conditions that fulfill your requirement.
First check your [request url] with your desired url. If both matched then open that url in SFSafariViewController instead of openUrl function. in else conditon you can load your [request url] which you don't want to open in Safari.
Related
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)"
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
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;
}
I'm loading a remote webpage into an iOS webview which relies on js and css assets. To improve the performance particularly on 3G networks, I'm hoping to call these assets from files local to the iOS device.
The website backing the iOS app is also used by mobile phone browsers, not just the iOS app, so subclassing NSURLRequest to register my own URL prefix (myurl://) is not ideal.
I already have code that launches mobileSafari for URLs outside of my domain:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSString *hostname = [url host];
if (![hostname hasSuffix:#".mysite.com"] && navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
return YES;
}
This method is called from the same controller implementation with code like this:
- (void)viewDidLoad {
[super viewDidLoad];
// ...
webView.delegate = self;
// ...
NSURL *url = [[NSURL alloc] initWithString:([path length] > 0 ? path : #"http://mysite.com/mobile/index")];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
// ...
}
I've reviewed several other questions ( How to use an iPhone webView with an external HTML that references local images? , Dynamically loading javascript files in UIWebView with local cache not working, iOS WebView remote html with local image files, Using local resources in an iPhone webview ) but they all seem to miss a key element to my problem.
The first link has the start of something promising:
if ([url isEqualToString:#"http://path-to-cdn.com/jquery-1.8.2.min.js"]) {
fileToLoad = [[NSBundle mainBundle] pathForResource:#"jquery-1.8.2.min" ofType:#"js"];
}
If that's a sensible approach, I'm stuck on how to get from passing that fileToLoad NSBundle into the webView's loadRequest, since that's expecting a URL.
I think I'm on the right path after realizing that I could use the output of stringByAppendingPathComponent as a URL, like so...
if (![hostname hasSuffix:#".mysite.com"] && navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *urlString = [url absoluteString];
if ([urlString isEqualToString:#"http://path-to-cdn.com/jquery-1.8.2.min.js"]) {
NSString *tempurl = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"js/jquery-1.8.2.min.js"];
[webView loadRequest:[NSMutableURLRequest requestWithURL:tempurl]];
} else {
[[UIApplication sharedApplication] openURL:url];
}
return NO;
}
return YES;
I don't have it working yet (it's still loading the remote URL) but I think I'm on the right path.
I am currently using ChildBrowser in phoneapp to open a website. However, I would like to open all external links of this website to open in Safari.
I do not have control over the source of this website.
My understanding is that I have to modify ChildBrowser to open all links starting with "http" in Safari.
I can't exactly read Objective-C, but I believe the code below is relevant.
- (void)loadURL:(NSString*)url
{
NSLog(#"Opening Url : %#",url);
if( [url hasSuffix:#".png" ] ||
[url hasSuffix:#".jpg" ] ||
[url hasSuffix:#".jpeg" ] ||
[url hasSuffix:#".bmp" ] ||
[url hasSuffix:#".gif" ] )
{
[ imageURL release ];
imageURL = [url copy];
isImage = YES;
NSString* htmlText = #"<html><body style='background-color:#333;margin:0px;padding:0px;'><img style='min-height:200px;margin:0px;padding:0px;width:100%;height:auto;' alt='' src='IMGSRC'/></body></html>";
htmlText = [ htmlText stringByReplacingOccurrencesOfString:#"IMGSRC" withString:url ];
[webView loadHTMLString:htmlText baseURL:[NSURL URLWithString:#""]];
}
else if ( [url hasPrefix:#"http" ])
{
//I have added in this else if.
}
else
{
imageURL = #"";
isImage = NO;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
webView.hidden = NO;
}
Any advice?
The easiest way I've found to do this is, if you know the Domain Host name that you will initially be requesting with childBrowser and you don't plan on requesting any other Domains then you can write this event right into the ChildBrowserViewController.m file.
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
NSString* domain = [url host];
// 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"] || [[url scheme] isEqualToString:#"www"]) {
if ( [domain isEqualToString:#"YOURDOMAIN.NAME] ) {
return YES;
} else {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
} else {
return YES;// [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
This will catch all requests made by childBrowser and if they match standard URL convention (preceding with HTTP, HTTPS, or WWW) and they are not your Domain Host name that you preset then it will foce them to open in Safari. Then when the user returns to your app they will still be on the previous page they where when they clicked the link.
I messed with this for hours until I came up with this.
If you need to be able to open more than one Domain Host with childBrowser but still want each one to open all external links leading away from that current domain in Safari I would suggest the following.
Add the following to ChildBrowserViewController.h
BOOL scaleEnabled;
BOOL isImage;
NSString* originalURL; <- THIS RIGHT HERE
NSString* imageURL;
And this
#property(retain) NSString* imageURL;
#property(retain) NSString* originalURL; <- THIS RIGHT HERE
#property(assign) BOOL isImage;
Then add the following to ChildBrowserViewController.m
#synthesize imageURL;
#originalURL; <- THIS RIGHT HERE
#synthesize supportedOrientations;
#synthesize isImage;
And add the following
- (void)loadURL:(NSString*)url
{
originalURL = url; <- THIS RIGHT HERE
NSLog(#"Opening Url : %#",url);
And then in the method I posted above change the following
NSURL *url = [request URL];
NSString* domain = [url host];
NSURL *oURL = [NSURL URLWithString: originalURL]; <- THIS RIGHT HERE
NSString* oDomain = [oURL host]; <- THIS RIGHT HERE
// 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"] || [[url scheme] isEqualToString:#"www"]) {
if ( [domain isEqualToString:oDomain] ) { <- THIS RIGHT HERE
return YES;
} else {
I haven't tested this second part but it should work just fine.
Good luck.