try to use rangeOfString to behave url for youtube video - ios

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.

Related

safari link is not open in If www is missing iOS

I Referred a many links related to this issue,But I cannot able to find the answer.I am getting a dynamic url in my app...If the url contains http://www then it opens the link,If www is not present then this error occurs.Any Help on this.
I am using this code,
NSString *selectedurl=[self.SelectedItem objectForKey:#"url"];
selectedurl=[selectedurl stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url=[NSURL URLWithString:selectedurl];
if (url.scheme.length == 0)
{
selectedurl = [#"http://" stringByAppendingString:selectedurl];
url = [[NSURL alloc] initWithString:selectedurl];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:selectedurl]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:selectedurl]];
}
only the URL will start in www your page does not open, you need to append thehttp:` in front of the string, so try this
NSString *sentence = #"www.google.com";
NSString *searchWord = #"http";
if ([sentence rangeOfString: searchWord].location != NSNotFound) {
NSLog(#"Yes , the search word is available");
}else
{
// add the http in front of www using stringWithFormat
}
or the alternate way
NSString *string = #"www.google.com";
if ([string containsString:#"http"]) {
NSLog(#"string contains http..!");
} else {
NSLog(#"string does not contain http...!");
}
choice-2
I think what you are searching for is Universal Links. Check this documentation for it here. It's pretty simple and straight forward. And here is a step by step explanation of how to support them. And afterwards, you can validate your own custom universal links here or hereenter link description here

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;
}

iOS embed UIWebView change with new youtube API

I'm starting to notice a change in the way that youtube videos are being loaded into UIWebViews and I wanted to know if this is behavior we should be expecting in the future and/or if we can replicate the previous functionality.
Comparison screenshot :
Old on the right, new on the left. The added youtube button allows users to leave the youtube video and go into the youtube web interface. I would like to be able to prevent the user from leaving the video being played.
I am currently using a category on UIWebView like this :
- (void)loadYouTubeEmbed:(NSString *)videoId
{
NSString* searchQuery = [NSString stringWithFormat:#"http://www.youtube.com/embed/%#?showinfo=0&loop=1&modestbranding=1&controls=0",videoId];
searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:searchQuery]];
[self loadRequest:request];
}
I've noticed that my query will respect either modestbranding=1 or showinfo=0 but not both at the same time. Will this change as the youtube redesign rolls out?
When the Youtube video is loaded, and webView:shouldStartLoadWithRequest:navigationType: is hit, you should be able to filter out that link so it won't proceed.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] isEqualToString:#"<URL String Youtube spits out when video selected>"]) {
NSLog(#"Blocking YouTube...");
return NO;
} else {
NSLog(#"Link is fine, continue...");
return YES;
}
}

stop youtube videos opening youtube app in xcode/cordova 1.6.1

I am building an app in xcode using the Cordova/phonegap framework for ios which displays some html that has some embeded youtube player code in it. iOS seems to redirect the user to the youtube app when it hits this youtube player. In cordova 1.5.0 the following code worked, but in 1.6.1 it doesn't seem to. Any ideas why or what needs changing to get it to work?
code to stop youtube opening up and links to behave selves
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
NSString* urlString = [url absoluteString];
if([urlString rangeOfString:#"http://www.youtube.com/embed"].location != NSNotFound) {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
else if (([[url scheme] isEqualToString:#"http"] || [[url scheme] isEqualToString:#"https"])) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
I would put logging in for url at the beginning of the method, and then a log statement in each clause of the if/elseif/else so you can see what urls are being intercepted and what the method then does with each one.
Maybe the string for the youtube request doesn't match the hardcoded "http://www.youtube.com/embed" any more? Worth a look.
look at http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
this might help.
It says to do this:
NSString *htmlString = #"<html><head>
<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>
<body style=\"background:#F00;margin-top:0px;margin-left:0px\">
<div><object width=\"212\" height=\"172\">
<param name=\"movie\" value=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"
type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"172\"></embed>
</object></div></body></html>";
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.your-url.com"]];

Resources