Open webView link in Safari from Javascript? - iphone

Thanks for looking at my question. To do some testing, I've output the string for the current URL of a webView into the console. But with m.youtube.com, it seems that no URL is passed. This is probably the reason why using methods like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
don't work with m.youtube.com, but do with other websites.
As Mathew said, YouTube loads using Javascript, so let me rephrase my question - is there a way to open the site in Safari when new javascript is loaded on the page?
I've uploaded a sample project that contains my problem.
https://www.dropbox.com/sh/m4yjv7awpj9wlh8/q0nQgHlQQC/YouTubeWebView
Thanks very much!

I think you can refer this,may be this can helpful to you in passing your webview request in safari without URL

Related

How can I receive data(url) from webpage loaded in uiwebview?

I've UIWebView with webpage loaded. This webpage is simple - one picture which follows somewhere
How can I receive this URL after taping for handling in xcode? I don't want to open this link in the same web view(what is happened now). Thanks for any tip.
Use this delegate function.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSURL *regURL=[request URL];
NSString *urlString=[regURL absoluteString];
}

how to detect redirects of url occurs while loading UIWebView on iphone?

Is there any way to detect url redirects occurs while loading a UIWebView in iphone. when I load www.example.com url, it redirects to some other url. I need to get those redirecting urls and where is actually we handles redirection.
Yes you can do this. Implement
– webView:shouldStartLoadWithRequest:navigationType:
This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button or hyperlink on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity or in your case bring up comments page or change nav bar etc.
Maybe from the request you can figure out if the HTTP response code is 3xx redirection then you can do what you want...
I found this thread when having to do the same. I used Srikar's answer, but here is my code.
I'm using Lumberjack logging, but you can replace the below with just NSLog. You can also get specific parts of the URL if you were looking at host, path, or query string to know if there is a failure (which is what I'm about to have to do). If it redirects several times, each request will get called here and if you want you can stop it and do something or just observe.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
DDLogVerbose(#"Redirecting to URL: %#", inRequest.URL.absoluteString);
return YES;
}
Try this:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ){
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}

Open iTunes links to apps in UIWebView if app is already installed

If I open this url in Safari iOS would check if I have this app installed. If it is - the app will be launched otherwise I will be redirected to the AppStore app.
Is it possible to make UIWebView to do the same thing?
The key thing is that system checks weather the app installed and has CFBundleURLTypes registered to respond.
EDIT:
Sorry guys I was wrong with identifying the problem. There is actually another link that redirects to the direct (local) link that is registered for the app. I catch this now in
-(BOOL) webView:(UIWebView *)inWeb
shouldStartLoadWithRequest:(NSURLRequest *)inRequest
navigationType:(UIWebViewNavigationType)inType
and call
[[UIApplication sharedApplication] openURL:url]
If UIApplication openURL does what you want (open the app page in App Store or open the app if it's installed; I guess it will do so) you can "steal" the url opening event in the UIWebView in the following way:
// myWebView will be your initialized web view
[myWebView setDelegate:self];
// implement this method in the owning class
-(BOOL) webView:(UIWebView *)inWeb
shouldStartLoadWithRequest:(NSURLRequest *)inRequest
navigationType:(UIWebViewNavigationType)inType
{
if (inType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
This will open all links clicked in the web view in Safari / App Store / default handling app. If you want to open only iTunes links this way, you might want to implement some URL parsing in this method and redirect only those links. If you return YES, the default handler will be called i.e. the link will be opened in the web view.
The behavior you describe isn't how it currently works on iOS 5.1
clicking on iTMS link a second time will still launch App Store.
This may help you with detecting if a app already installed
https://stackoverflow.com/a/5746603/515359

Call action from UIWebView with JavaScript

I am trying to build a simple app that uploads images to my site. I am using this tutorial:
http://www.youtube.com/watch?v=aQXaJO36I7Y
What I want to do instead of using a button from the app's interface I wan to use a button within a website in a webview. So I want a button to call the opening of the photo album to select a image to upload.
Basically I need a way for a javascript function to call a action in the app itself.
How can I do this?
You could just use a custom link, which you can then detect in the UIWebViewDelegate method.
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *rawURL = [[request URL] absoluteString];
if([rawURL isEqualToString:#"callImageLib://"]) {
[self methodeForImageLib];
}
return NO;
}
return YES;
}
If i understand it correctly you need to communicate iPHone Web application to call native iPhone application and visa versa. please check the following links.
http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript
http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
hope these 2 link help to solve your issue.

UIWebView: open some links in Safari, some within the view

My app features content that (for text formatting reasons) is presented in an UIWebView. Within the content there are links, some of which should open their target in mobile Safari, while others should navigate within the content.
So far, I've catched the link requests using a UIWebView delegate. In my implementation of
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
I'd check the requests URL using lastPathComponent or pathComponents for known elements to determine whether to open the link externally or within the view.
However, I just found out said methods are only available since iOS 4.0, which would render the app useless on iPad. Plus I have the feeling I'm using a dirty solution here.
Is there another way to somehow "mark" the links within my content in a way that makes them easy to distinguish later, when processing the request in the delegate method?
Thanks alot!!
You could covert the URL request into a string, and do a compare for a subdirectory on your website, such as in URLs that only start with "http://www.sample.com/myapp/myappswebcontent/", against the initial substring of your URL. Anything else, send to Safari.
You should set a policy delegate of web view:
For instance in the controller, that contains a web view
[webView setPolicyDelegate:self];
and then override a decidePolicyForNavigation method (this is just an example):
- (void)webView:(WebView *)sender decidePolicyForNavigationAction: (NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id <WebPolicyDecisionListener>)listener
{
if ([[actionInformation objectForKey:WebActionNavigationTypeKey] intValue] == WebNavigationTypeLinkClicked) {
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
}
else
[listener use];
}
you can distinguish there kind of link and ignore or use the listener. If you ignore it, you can open the link in safari, if you use it, the link will open in your webview.
HTH