How to play a Vimeo video in iOS? - objective-c

I've tried to search to web, but I couldn't find a topic not older than 1 year regarding this problem, therefore;
How can I play a Vimeo video in an iOS App?
EDIT1: When using the solution I'm sometimes getting this HTTP response from Vimeo
Why?

This is my way of play a Vimeo video inside a app.
I am using iFrame to load Vimeo video inside my app.
follow this steps and you will too.
create a uiwebview and connect it to your .h file. Mine is _webView.
Add this method to your .m file.
-(void)embedVimeo{
NSString *embedHTML = #"<iframe width=\"300\" height=\"250\" src=\"http://www.vimeo.com/embed/rOPI5LDo7mg\" frameborder=\"0\" allowfullscreen></iframe>";
NSString *html = [NSString stringWithFormat:embedHTML];
[_webView loadHTMLString:html baseURL:nil];
[self.view addSubview:_webView];
}
I am using the embedded code in Vimeo video. (I hope you know what it is)
call this method inside your viewdidload
[self embedVimeo];
Run the app and you will see the video in your view. This way is perfectly working for me and i think this will help for your too.

You can use YTVimeoExtractor, works fine for me.

You can use this code
NSString *htmlStringToLoad = [NSString stringWithFormat:#"http://player.vimeo.com/video/%#?title=0&byline=0&portrait=0\%%22%%20width=\%%22%0.0f\%%22%%20height=\%%22%0.0f\%%22%%20frameborder=\%%230\%%22", videoID];
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:htmlStringToLoad]]];

Please try this,
It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}

Use Below Code the it will work fine
NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
[html appendString:#"<html><head>"];
[html appendString:#"<style type=\"text/css\">"];
[html appendString:#"body {"];
[html appendString:#"background-color: transparent;"];
[html appendString:#"color: white;"];
[html appendString:#"}"];
[html appendString:#"</style>"];
[html appendString:#"</head><body style=\"margin:0\">"];
[html appendString:#"<iframe src=\"//player.vimeo.com/video/84403700?autoplay=1&loop=1\" width=\"1024\" height=\"768\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"];
[html appendString:#"</body></html>"];
[viewWeb loadHTMLString:html baseURL:urlMovie];

I've used this code:
NSString *embedSr = #"<iframe width=\"304\"height=\"350\" src=\"http://player.vimeo.com/video/... \" frameborder=\"0\" allowfullscreen></iframe>";
[[self WebView] loadHTMLString:embedSr baseURL:nil];

I've tried the universal player, it is successful in device with iOS 5, but failed in iOS 4.2 with iPhone 3G. I don't know why. Here's the link to embed it.
Or you can embed manually from the Vimeo site, click embed, and configure the config as you wish.

Related

How can I add 'Follow us on facebook' button to an iOS app?

I've about completed my iOS app but only need to add two buttons:
Follow us on facebook
Follow us on twitter
I assumed I would find a couple of simple examples here, but was surprised to find no answer to this at all (yet).
I'm pretty sure I could spend the next few hours trying to figure it out, but thought I'd reach out for a little time-saving help.
Just looking for the code to go behind a button on a view controller (XCode 4.5.1, iOS 6).
I assume the only variable I might need to supply is the company's facebook account name.
Any suggestions? (Thanks in advance!)
First, the URL schem for Facebook: fb://profile/<yourpageid> (source). A URL with this structure will open the Facebook app, if it is installed.
More on iOS URL schemes.
When your button is tapped, you can check if the Facebook is installed:
-(IBAction)fbButtonTap:(id)sender {
NSURL *fbURL = [[NSURL alloc] initWithString:#"fb://profile/<yourpageid>"];
// check if app is installed
if ( ! [[UIApplication sharedApplication] canOpenURL:fbURL] ) {
// if we get here, we can't open the FB app.
fbURL = ...; // direct URL on FB website to open in safari
}
[[UIApplication sharedApplication] openURL:fbURL];
}
For twitter, you follow the same basic steps. The Twitter URL scheme for iOS is twitter://user?id=12345 or twitter://user?screen_name=yourname (source). Again, if the Twitter app is not installed, you have open the twitter profile in safari.
As for taking direct actions, I do not think you can do that, since there is no inherent knowledge about any other applciation installed on the device. The best I think you can do is direct users to each respective account.
You could use SLRequest have someone follow you. This will only work on iOS 6 though, on iOS 5 Twitter works but not Facebook.
An example of SLRequest for someone to follow you on Twitter, make sure this is called on the background thread:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"Your twitter name" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict];
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"%#", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
}];
And for Facebook you just have to change some of it and look at their API and change the request URL.
this is my code for show people to like the community page of my app.
you can add the _webview where you want to show inside the code.
facebook gives the code for showing the page inside webview.
-(void)likeus
{
_webview =[[UIWebView alloc] initWithFrame:CGRectMake(14,94, 292, 250)];
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Load web view data
NSString *strWebsiteUlr =#"http://www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fenbuyukkim&width=292&height=258&show_faces=true&colorscheme=dark&stream=false&border_color&header=false&appId=433294056715040";
// Load URL
//Create a URL object.
NSURL *url = [NSURL URLWithString:strWebsiteUlr];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[_webview loadRequest:requestObj];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:_webview
action:#selector(removeFromSuperView)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(270.0, 80.0, 30.0, 30.0);
[button setBackgroundImage:[UIImage imageNamed:#"cross.png"] forState:UIControlStateNormal];
[_webview addSubview:button];
}
I am not sure if this is what you are looking for, but I think I found the solution, here at stack overflow on another thread.
Take a look at this thread:
Adding the Facebook Like Button in an iPhone App
Hope that helps you!

Cocoa: get WebView mainFrame html string

I'm creating an Mac app that loads some content from a URL in a WebView and then needs to save the content to a file. The content may and may not be HTML. Loading works fine, the problem is to get the content from the WebView. How do I do that?
Thanks!
I do not know if you use WebView (MacOS) or UIWebVoew (iOS...). I use the following code (in MacOS) which works well for me:
WebFrame *frame = [myWebView mainFrame];
WebDataSource *source = [frame dataSource];
NSData *data = [source data];
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[someTextView setString:str]; // shows the content of myWebView as string

Print Local HTML using Cocoa

I have a simple list of items that needs to be printed using Cocoa. I have a half-baked solution that uses an NSView with a custom drawRect: method, but it's fairly complex and not very easy to maintain.
What I would like to have is an HTML string (which could be easily constructed from the list) that can be embedded in a one-off WebView, then printed.
Assuming I have a simple NSString like:
NSString *htmlString = #"<b>Test</b>";
What's the easiest method for creating a WebView displaying this content? I've tried the below code, but it results in a single blank page:
WebView *webView = [[WebView alloc] init];
NSString *dir = #"/Users/Me/Desktop/";
NSString *fileUrl = [dir stringByAppendingPathComponent:#"Temp_Print.html"];
NSString *htmlString = #"<b>Hi!</b>";
[[htmlString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileUrl atomically:YES];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileUrl]]];
[webView setFrame:NSMakeRect(0, 0, 500, 500)];
NSPrintOperation *po = [NSPrintOperation printOperationWithView:webView printInfo:pi];
[pi release];
[po runOperation];
Another one of those questions you solve right after asking it!
The run loop needs to iterate in order for the content to actually load. I simply finished running the actual print operation in the frame load delegate method:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
...
}
Source

playing music by url

I want to play the music from internet by url.
I create a simply project that has one button with the following code:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie play];
It works but with some delay (I think because it waits while file has been fully downloaded).
So what I need to do that the music starts to play immediately (when, for example, 10% of file has been downloaded)?
Use -[QTMovie autoplay] to automatically play the music once enough data has been downloaded.
In your case:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie autoplay];
From the QTMovie class reference:
The autoplay method configures a QTMovie object to begin playing as soon as enough data is available that the playback can continue uninterrupted to the end of the movie.
If you can consider displaying a Quicktime window over your app, you can use this code :
NSString *url = #"http://someURL.mp3";
UIWebView* tempAudioPlayer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[tempAudioPlayer loadHTMLString:[NSString stringWithFormat:#"<iframe frameborder=\"0\" width=\"0\" height=\"0\" src=\"%#\"></iframe>", url] baseURL:nil];
[self addSubview:tempAudioPlayer];
I first create a UIWebView which will not be displayed. Then I loadHTMLString a <iframe> in it, with the URL of my file as the src value. And I add it to my view. Quicktime appears immediately.

QTKit strange error

just simple peace of code (file 1.mp3 clicked and playing as well in iTunes) :
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSError *outError = nil;
QTMovie *newMovie = [QTMovie movieWithURL:[NSURL URLWithString:#"/Users/Alex/1.mp3"] error:&outError];
if (newMovie) {
//[newMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
[self setMovie:newMovie];
}
[movie play];
give me error
Error Domain=NSOSStatusErrorDomain
Code=-2000 UserInfo=0x2004a6de0 "A
necessary data reference could not be
resolved."
Changing
[movie play];
to
[movie autoplay];
might help you. QTMovie loads the data in the background, so asking it to play right after it's created might be too quick for the QTMovie to really play the file.
You need to create a file:-based NSURL using fileURLWithPath:, not URLWithString:. URLWithString: is meant for URLs like http:, etc.
Try:
QTMovie *newMovie = [QTMovie movieWithURL:
[NSURL fileURLWithPath:#"/Users/Alex/1.mp3"] error:&outError];