iOS embed UIWebView change with new youtube API - ios

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

Related

iOS xcode Alternative to UIWebview didFinishLoad

I have a webview that when loaded, the user is logged in by a POST request. After they are logged in, I want them to be taken to a webpage. My POST request is a URL as follows:
- (void)viewDidLoad {
[super viewDidLoad];
[_scoreWebView setDelegate:self];
NSMutableURLRequest *detailRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"myrul"]];
[detailRequest setHTTPMethod:#"POST"];
NSString *sendInfo =[NSString stringWithFormat:#"action=login&EMAIL=email&PASSWORD=password", nil];
NSData *infoData = [sendInfo dataUsingEncoding:NSUTF8StringEncoding];
[detailRequest setHTTPBody:infoData];
[_scoreWebView loadRequest:detailRequest];
}
This log in process works fine. However, after I send the user to my webpage it is launching webviewdidfinishload infinitely. I know that it fires each time something is loaded. Is there an alternate solution to redirecting the user to my page after log in? Also, I have three different pages that the user could be redirected to based on their input, this is just one of them for simplicity. This is my finishload method:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Check here if still webview is loding the content
if (webView.isLoading)
return;
else //finished
NSLog(#"finished loading");
NSLog(#"%# in calendar", _thisScriptUniqueID);
NSString *fullURL=[NSString stringWithFormat:#"myurl/%##score", _thisScriptUniqueID];
NSLog(#"%#", fullURL);
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_scoreWebView loadRequest:requestObj];
}
Is there a different method that could be used to take the user to the page, or would it be possible to include both in the viewDidLoad?
After a lot of research, I decided that the functionality would work a lot better on the server side. I made it so that the same URL logs the user in and brings them to the desired page at the same time.

UIWebview disable redirect to AppStore

I have UIWebview in my app and this is how i navigate some times in the application to URL:
NSURL *url = [NSURL URLWithString:#"http://mp3skull.com/mp3/nirvana.html"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
And sometimes i noticed that if i click on link the AppStore application is opened with some app.
It is possible to disable it?
In the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
You could do
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestedURL = [[request URL] absoluteString];
// URL for opening itunes according to Apple docs is something like this #"http://itunes.apple.com
// But I do believe that it will be some sort of URL scheme that opens it specifically to the app on the app store.
// So without an example this is the best I can provide.
if([requestedURL isEqualToString:#"http://itunes.apple.com"]) {
// or if([requestedURL rangeOfString:#"itunes.apple.com"].location==0) {
// What is happening here is that if the request url that is being request is
// "http://mp3skull.com/mp3/nirvana.html" then we don't want to continue with the request so stop.
return NO;
}
// Otherwise for all other requests continue
return YES;
}
Remember you will need to set the delegate on your UIWebView as this method is a UIWebViewDelegate method - see Apple Documentation on UIWebViewDelegate for more information
Yes it happens, some times when you click on some links, the application will open another application.
Because, those links contain other app schemas. (link)
so, to disable opening such url-schemas, we have to detect them and not load them.
As appStore has "itunes.apple.com",
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestedURL = [[request URL] absoluteString];
if([requestedURL rangeOfString:#"itunes.apple.com"].location==0) {
return NO;
}
return YES;
}

dropbox linkFromController detect cancel

I upgraded DropBox framework in my app and now I have to use linkFromController.
There is a way to detect if user tap on cancel button or if the DropBox auth popup is visible or not?
Thanks,
Max
You can use Following delegate to filter the loading url. Find url that load in cancel button and use it to complete your task.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *url = [[request URL] absoluteString];
if([url isEqualToString:#"Cancel Url"]){
//Do what you want
return NO;
}
}

Wrong base URL in UIWebView

I'm working on a payment system that runs in a UIWebView. The user completes a form on one site, and is taken to a payment gateway to process their card information. Once the payment has been processed, the user is taken back to a confirmation page on the first site.
The sites work as expected when tested in a normal browser, or even Mobile Safari. The sites are black boxes, and I can't change anything inside them. Apparently, the sites use relative URLs, and my issue occurs because my UIWebView is trying to load a page on the second domain with the base URL from the first domain.
For example,
User posts form from http://theform.com/page
User is taken to http://theform.com/OrderCC.aspx?orderRef=e59d7f53a693472cad8a76dd8fb64
In the second step, the user should have been taken to http://thepaymentgateway.com/OrderCC.aspx...
I'm trying to intercept requests to the wrong base URL, and reroute them to a correct one like this:
-(BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest*)inRequest navigationType:(UIWebViewNavigationType)inType {
NSString *wrongURL = #"http://theform.com/OrderCC.aspx";
NSString *request = [[inRequest URL] absoluteString];
NSString *data = [[NSString alloc] initWithData:[inRequest HTTPBody] encoding:NSASCIIStringEncoding];
if ([request length] >= 33 && [[request substringWithRange:NSMakeRange(0, 33)] isEqualToString:wrongURL]) {
[webView loadData:[inRequest HTTPBody] MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://thepaymentgateway.com/OrderCC.aspx%#", [request substringFromIndex:33]]]];
return NO;
}
return YES;
}
After this runs, it seems my UIWebView is just displaying a blank page containing the POST data string. Where am I going wrong?

try to use rangeOfString to behave url for youtube video

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.

Resources