Could not be able to play single video in web view - ios

In my application i need to play single video in UIWebView i could be able to play playlist in UIWebView.
link to get video is:
http://www.youtube.com/watch?v=5cCjaqi31XE&feature=youtube_gdata
here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *videoURL=[self getYTUrlStr:stringURL];
NSURL *url=[NSURL URLWithString:videoURL];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
NSString *videoURL=[self getYTUrlStr:stringURL];
- (NSString*)getYTUrlStr:(NSString*)url
{
if (url == nil)
return nil;
NSString *retVal = [url stringByReplacingOccurrencesOfString:#"watch?v=" withString:#"v/"];
NSRange pos=[retVal rangeOfString:#"version"];
if(pos.location == NSNotFound)
{
retVal = [retVal stringByAppendingString:#"?version=3&hl=en_EN"];
}
return retVal;
}

Need to modify link as below
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"http://www.youtube.com/v/" withString:#"http://www.youtube.com/embed/"];
videoURL=[videoURL stringByReplacingOccurrencesOfString:#"?version=3&hl=en_EN" withString:#"?feature=player_detailpag"];
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"?version=3&f=user_uploads&app=youtube_gdata" withString:#"?feature=player_detailpage"];
Then provided to instance method to UIWebView instance method loadRequest as NSURLRequest.

Related

How do I restrict my application to show up in the "Open in..." menu on iOS for a specific document type

I need to restrict my application to show up for ppt file types.How to do this?
You can use this. This will get the 'kind' of file.
- (NSString *)humanReadableFileType:(NSString *)path
{
NSString *kind = nil;
NSURL *url = [NSURL fileURLWithPath:[path stringByExpandingTildeInPath]];
LSCopyKindStringForURL((CFURLRef)url, (CFStringRef *)&kind);
return kind ? [kind autorelease] : #"";
}
After that check it like that
if([kind isEqualToString:#"ppt"])
{
}
Source: http://importantshock.wordpress.com/2007/01/07/cocoa-snippet-finding-a-files-kind/#post-19

Problems loading UIWebView

I'm attempting to write a SplitView control program in which pressing a table cell in the masterViewController causes an associated web page to load in the detail view controller. I have the following method in the detail view controller that I can confirm is getting called and is receiving the correct input:
-(void)masterAction:(id)sender {
NSString *http = #"http://";
http = [http stringByAppendingString:sender];
_urlString = http;
NSURL *url= [NSURL URLWithString:_urlString];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
}
However, nothing is loading. Any ideas why this might be? The only way I've been able to get anything at all to load is to insert something similar to the following in my viewDidLoad method:
NSURL *url= [NSURL URLWithString:#"http://www.google.com];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
The method is being called using:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *thread = [self.issueData objectForKey:#"responseData"];
NSDictionary *feed = [thread objectForKey:#"feed"];
NSArray *entries = [feed objectForKey:#"entries"];
NSDictionary *posts = entries[indexPath.row];
NSString *urlString = [posts objectForKey:#"link"];
NSArray *split = [urlString componentsSeparatedByString:#"url=http://"];
NSString *url = [split objectAtIndex:1];
[self.delegate masterAction:url];
}
set the delegate of webview.
and try this.
NSString *myUrl = #"http://www.YourWebSite.com";
NSURL *webUrl = [NSURL URLWithString:myUrl];
[webObj loadRequest:[NSURLRequest requestWithURL:webUrl]];
I duplicated this code in a test project and the only piece of code where something can go wrong is if you are forgetting to put www. after the http:// before the domain name.
Trying changing your masterAction method to the one below:
- (void) masterAction: (id) sender
{
if (![sender isKindOfClass:[NSString class]]) return;
NSString *http = #"http://www.";
NSString *urlString = [http stringByAppendingString:sender];
NSURL *url = [NSURL URLWithString:urlString];
[self.web loadRequest:[NSURLRequest requestWithURL:url]];
}
If this is not the issue and the string being sent to the method contains the www. try setting the delegate of the UIWebView to see if any error is thrown when loading the request.

XCDYouTubeVideoPlayer opens and close

I've start working with XCDYouTubeVideoPlayer. It does seem to have, some small issues. When I use the method (like below) to call the player it opens it and close it right away.
I've imported following frameworks:
mediaplayer
AVfoundation
and added the `XCDYouTubeVideoPlayerViewController.h and .m
In the viewController.m I've added this method:
- (IBAction) play:(id)sender
{
[self.view endEditing:YES];
NSString *link = #"m01MYOpbdIk";
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:link];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
}
At the moment its opening and closing the XCDYouTubeVideoPlayer right a way. What am i doing wrong?
I not understand why, but for me , in iOS8 the line
XCDYouTubeVideoPlayerViewController.m at #79
was calling the method :
- (id) initWithContentURL:(NSURL *)contentURL
{
#throw [NSException exceptionWithName:NSGenericException reason:#"Use the
` initWithVideoIdentifier:` method instead." userInfo:nil];
}
I just comment it and works!
XCDYouTubeVideoPlayer library build the youtube "streaming link", by appending the url with the signature provided. It seems that the "url" now has the signature along with it and the "sig" key comes null, which thereby negates the if statement in the function
(NSURL *) videoURLWithData:(NSData *)data error:(NSError * __autoreleasing *)error
Goto file
XCDYouTubeVideoPlayerViewController.m at #248
you will see a for-loop
for (NSString *streamQuery in streamQueries)
{
NSDictionary *stream = DictionaryWithQueryString(streamQuery, queryEncoding);
NSString *type = stream[#"type"];
NSString *urlString = stream[#"url"];
NSString *signature = stream[#"sig"];
if (urlString && signature && [AVURLAsset isPlayableExtendedMIMEType:type])
{
NSURL *streamURL = [NSURL URLWithString:[NSString
stringWithFormat:#"%#&signature=%#",
urlString,
signature]];
streamURLs[#([stream[#"itag"] integerValue])] = streamURL;
}
change it to this (remove the signature variable from if statement and modify the URLWithString)
for (NSString *streamQuery in streamQueries)
{
NSDictionary *stream = DictionaryWithQueryString(streamQuery, queryEncoding);
NSString *type = stream[#"type"];
NSString *urlString = stream[#"url"];
if (urlString && [AVURLAsset isPlayableExtendedMIMEType:type])
{
NSURL *streamURL = [NSURL URLWithString:urlString];
streamURLs[#([stream[#"itag"] integerValue])] = streamURL;
}

How to use local resources to speed up webview in iOS?

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.

Setting UIDelegate for UIWebView

I read somewhere to read javascript console messages using the
- (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)message
delegate method from the UIDelegate. But how/where do I need to set the delegate of the WebView (not the UIWebView) to my custom delegate?
I know Apple doesn't allow this in the AppStore, but I just want to implement this for debugging purposes.
What I tried so far:
- (void)webView:(id)sender didClearWindowObject:(id)windowObject forFrame:(WebFrame*)frame
{
[webView setUIDelegate:[[MyCustomUIDelegate alloc] init]];
}
and
-(void) webView:(id)webView windowScriptObjectAvailable:(id)newWindowScriptObject
{
[webView setUIDelegate:[[MyCustomUIDelegate alloc] init]];
}
This post may help you:
How can my iPhone Objective-C code get notified of Javascript errors in a UIWebView?
You can hook UIWebView control to hidden WebKit framework and get all exceptions, executed functions and similar.
Another way is posted here: Inject javascript code into the response that invoke to a objecie-c function.
NSString* path = [[NSBundle mainBundle] pathForResource:#"script"
ofType:#"js"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
[sender stringByEvaluatingJavaScriptFromString:content];
for exapmle the javascript code may be like:
console = new Object();
console.log = function(log) {
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "ios-log:#iOS#" + log);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
console.debug = console.log;
console.info = console.log;
console.warn = console.log;
console.error = console.log;
and the objective-c code like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
//NSLog(requestString);
NSLog(#"Request: %#", requestString);
if ([requestString hasPrefix:#"ios-log:"]) {
NSString* logString = [[requestString componentsSeparatedByString:#":#iOS#"] objectAtIndex:1];
NSLog(#"UIWebView console: %#", logString);
return NO;
}
return YES;
}

Resources