I am having an array that contains different urls, and a set of buttons, each link is assigned to each buttons. When clicking on a button, the content in the url which is assigned to that particular button will be downloaded. The user can click on multiple buttons at the same time so that multiple download can perform at the same time. And at the same time user should have the provision to navigate through another views, so that the downloading process should not lock the UI. Which would be the best and easiest way to implement this? Please share your ideas.
Thanks
Just fetch the data asynchronously:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// The code here is executed when the response from the request comes back.
// The variable "data" will contain the response from the data.
// So you can do something like:
UIImage *anImage = [[UIImage alloc] initWithData:data]];
}];
Luke, use AFNetworking or ASIHTTPRequest lib with asynchronous requests.
You could easily implement a Asynchronous NSURLConnection
i.e. each time the user hits that button you fire up an async connection to do the dirty work.
There are plenty of examples - One of the easiest blog style examples to understand is Matt Gallagher's Cocoa With Love. Here is a link.
The gist of the technique is the delegate methods are easy to work with and you can capture each file that you download inside them.
Don't be tempted by the Synchronous style connection as it is not as flexible and you will struggle to make an easy solution for downloading multiple files using that technique.
Related
I am trying to retrieve an image from the ALAssets library using a URL, calling assetforURL, but neither the result nor completion blocks are ever called. Something like this:
[library assetForURL:assetURL
resultBlock:^(ALAsset *myasset){
NSLog(#"asset found");
}
failureBlock:^(NSError *error){
NSLog(#"error");
}];
I know that assetforURL is asynchronous and that the blocks will be called sometime later. But they seem to be never called. I never see the output from the NSLog, and if I put in breakpoints, they are never reached.
What I'm really trying to do is associate images with items in a database. I store the asset URLs in the database, and then use the code above to retrieve them. Is there a different way of doing this? I havn't been able to find any examples of anything like this.
Thanks!
I want to play audio file on ipad which is located on some server. but thing is I want to autoplay audio file using HTML5. Please suggest me the way.
Thanks
The autoplay attribute is purposely disabled on iPhone and iPad:
In Safari on iOS (for all devices,
including iPad), where the user may be
on a cellular network and be charged
per data unit, preload and autoplay
are disabled. No data is loaded until
the user initiates it. This means the
JavaScript play() and load() methods
are also inactive until the user
initiates playback, unless the play()
or load() method is triggered by user
action. In other words, a
user-initiated Play button works, but
an onLoad="play()" event does not.
Having said that there are some hacks for iOS 3 and iOS 4 to work around this with JavaScript. On iOS 3 you can create a link element and then simulate a click event to trigger the media element to play; on iOS4 you can simply call the load() and play() methods directly on the element.
Try this
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:#"www.example.com/play.mp3"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
[myWebView loadRequest: request];
[request release];
Hope this helps!!!
Playing Youtube video in the app is easy and well documented around.
There are two problems with that:
after closing Youtube player, if user wants to play it again it has to wait for online streaming again
can't play offline (load video at home to watch on the road)
Does anyone have code to:
download Youtube video to documents folder and show progress of download
play downloaded video by loading file from documents folder (meaning even when not connected to the internet)
To download the video from YouTube:
Get the URL to download from, via the YouTube API or whatever other method.
Create an NSOutputStream or NSFileHandle opened on a temporary file (in NSTemporaryDirectory() or a temp-named file in your Documents directory).
Set up your progress bar and whatever else you need to do.
Allocate and start an NSURLConnection to fetch the file from the URL. Do not use sendSynchronousRequest:returningResponse:error:, of course.
In the connection:didReceiveResponse: delegate method, read out the length of data to be downloaded for proper updating of the progress bar.
In the connection:didReceiveData: delegate method, write the data to the output stream/file handle and update the progress bar as necessary.
In connectionDidFinishLoading: or connection:didFailWithError:, close the output stream/file handle and rename or delete the temporary file as appropriate.
To play it back, just use NSURL's fileURLWithPath: to create a URL pointing to the local file in the Documents directory and play it as you would any remote video.
Ive used classes from this project: https://github.com/larcus94/LBYouTubeView
It works fine for me.
I can download youtube videos.
I used this code:
LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(#"http://www.youtube.com/watch?v=%#"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
if(!error) {
NSLog(#"Did extract video URL using completion block: %#", videoURL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL: videoURL];
NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [NSString stringWithFormat:(#"video_%#.mp4"), self.videoID ];
[data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES];
NSLog(#"File %# successfully saved", filename);
});
} else {
NSLog(#"Failed extracting video URL using block due to error:%#", error);
}
}];
You can show progress of downloading using technique described in the posts above.
Here is my example: https://github.com/comonitos/youtube_video
I used PSYouTubeExtractor.h class by Peter Steinberger It can get youtube mp4 video url and than downloading and viewing is not a problem
NSURLConnection
+
NSNotificationCenter
+
PSYouTubeExtractor
+
NSMutableData
check these projects -
https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube
these will definitely help you!!
I don't personally know how to download youtube videos (and the code is too big to put in an answer here).
However, here's a complete youtube downloading example here.
It's an open source youtube downloader called LoadTube: here's the a link to the source code.
I would play the video and figure out where the temp file is being stored. If you can get access to it, copy it into some document folder for offline viewing.
I have a UIWebView in an iPhone application I am building. It's job is to load up a URL. The code I use is:
NSURL *url = [NSURL URLWithString:providedURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
This is in viewDidLoad.
I have noticed that it takes around 18 seconds for the page to load in my app....but when using Safari it takes 5-8 seconds. Has anyone run into this issue before? Does it have anything to do with how I'm forming my request?
It is likely because Safari has cached the resources of the site already. A basic UIWebView does not do any caching by itself. You'll have to implement that yourself. See a question about that here:
How to cache content in UIWebView for faster loading later on?
When UIWebView loads Microsoft word documents, it just loads it as if it's one whole strip of paper, disregarding the separation between pages. Any idea how to display it properly (pages separated from each other), I'm open to lower level programming, or alternatives to UIWebView for loading Office documents. I'm currently using IPhone OS 3.2 for IPad.
E.g. I tried creating a word document with 2 pages, and one paragraph on each page, when I load it in UIWebView, it's displayed in one page.
The code I used is from Apple Technical Q&A
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:#"test1.doc" inView:self.myWebview];
As a side note, I'm thinking this should be possible because there are apps like Documents to Go and QuickOffice, I'm not sure how they implemented it.
At the current SDK, it's not possible. We ended up implementing our own and used UIScrollView to meet our requirements. This was a lot of work implementing our own renderer, own panning system, etc.