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)"
Related
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.
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;
}
I have an apps link using link maker from AppStore . I have a UIWebView in my app and I am trying to load the link in UIWebView but each time I load that link it opens the inbuilt AppStore instead of showing the content in the UIWebView. I want to open the link in UIWebView instead of opening the inbuilt iOS AppStore.
This what i tried
- (void)viewDidLoad
{
str = #"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa";
str = [NSString stringWithFormat:#"%#/wa/viewContentsUserReviews?", str];
str = [NSString stringWithFormat:#"%#type=Purple+Software&id=", str];
// Here is the app id from itunesconnect
str = [NSString stringWithFormat:#"%#289382458", str];
[webOpenApp loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
}
-(BOOL) webView:(UIWebView *)inWeb
shouldStartLoadWithRequest:(NSURLRequest *)inRequest
navigationType:(UIWebViewNavigationType)inType
{
if (inType == UIWebViewNavigationTypeOther) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
return NO;
}
return YES;
}
The itms-apps:// is an special protocol that when iOS system read this it will launch the App Store to show that app. Apparently this is not what you want.
You should build the UI yourself to display the information of the app. And when user click the buy button you have to redirect user to App Store. Because App Store is the only place user could buy and download an app.
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 need the app recognize if it's a youtube video embed, then in-app webView
Here is the code I'm using now:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSString *urlString = request.URL.absoluteString;
NSString *youtube;
youtube = #"youtube";
if ([urlString rangeOfString:youtube options: NSCaseInsensitiveSearch].location != NSNotFound){
return YES;
}
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
That works in most cases because most youtube links are directly transferred as embedded video already on the webpage. However, I check if i choose a profile page or others from youtube, this will still open in-app, becuz my app doesn't have back button (buttons in html page). So any link that's not to a video will cause can't return.
I tried use #"youtube.com/watch" #"/watch?" #"watch?" as rangeOfString, but only youtube works.
For example:
This a youtube video url: data-url="http://youtube.com/watch?feature=player_detailpage&v=Ke1Y3P9D0Bc" (in-app view good)
dara-url="youtube.com" (fail, still in-app view)
I wonder either i stored string by wrong format, symbols not support in rangeOfString?
Or there can be another way like urlString rangeOfString:youtube && #"watch"
Thank you for this, really appreciate.
NSURL *myURL = [NSURL URLWithString:#"http://www.youtube.com/watch?feature=player_detailpage&v=Ke1Y3P9D0Bc"];
NSString *host = myURL.host;
NSString *path = myURL.path;
NSLog(#"%#", host); // Output: www.youtube.com
NSLog(#"%#", path); // Output: /watch
NSLog(#"%#", myURL.query); // Output: feature=player_detailpage&v=Ke1Y3P9D0Bc
if (NSMaxRange([host rangeOfString:#"youtube.com" options:(NSCaseInsensitiveSearch|NSBackwardsSearch)]) == host.length &&
[path.lowercaseString isEqualToString:#"/watch"]) {
NSLog(#"This is a youtube video.");
}
You can certainly use,
if ([urlString rangeOfString:youtube options: NSCaseInsensitiveSearch].location != NSNotFound && [urlString rangeOfString:#"watch" options: NSCaseInsensitiveSearch].location != NSNotFound)
Whether that will get you what you want, I don't know, but it should work as a valid if statement.