Confusión de URL de activos de iOS

Tengo una URL que se parece a esto:


I am playing it in a UIWebview with the following code:

    NSString *localVideoHTML = @"\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-mp4\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    NSString *html = [NSString stringWithFormat:localVideoHTML, videoId, self.frame.size.width, self.frame.size.height];

The video plays perfectly; I have tried several with these URLs and have had no problem. However, I want to be able to confirm that it has not been deleted from the device before it is played. I was using the following code to do this:

NSURL *url = [NSURL URLWithString:videoID];
NSError *err;
BOOL isReachable = [url checkResourceIsReachableAndReturnError:&err];

This seemed to work with some URLs, but not with others. So, at this point, I'm confused about whether I can determine before-the-fact whether the URL points to an existing file or not (it is possible the file was deleted from the device, as the URL is stored in a Core Data database for repeated use). I'm getting the sense from reading on SO and elsewhere that a URL that looks like the one I posted can't be used reliably; however, I've been able to consistently play movies as shown above with no difficulty.

Por lo tanto, mis preguntas son:

a) Given that I'm able to play all of the tests I've run with URLs that look like the above, am I making a mistake in assuming that it will play with any such URL?


b) Is there a simple way to test for the "reachability" of the URL before loading into the webview, so if it is NOT a valid URL I can deal with that problem in my code?

Thanks, in advance for any assistance you can provide.

preguntado el 09 de septiembre de 13 a las 22:09

So you mean you were getting a false negative with some URLs? Can you provide an example? -

1 Respuestas

Okay, this is resolved, and in case it will help someone else, here goes.

To test whether the video is still on the device at the time it is to be played, the following code was used:

- (void) verifyURLReachable:(NSString *) videoID
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:[NSURL URLWithString:videoID]
     resultBlock:^(ALAsset *asset)
         if (!asset)
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video is Not Available on Device"
                                                             message:@"The requested video is no longer available on this iPad device. This video will be automatically removed from the current goal."
                                                            delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
             alert.tag = alertViewURLUnreachable;
             [alert show];
    failureBlock:^(NSError *error)
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access to Photos is Required"
                                                        message:@"To play videos that are stored on your device you must allow access to your photo and video library. This setting is made in your device setttings under 'Privacy'."
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        alert.tag = alertViewUserDeniedAccess;
        [alert show];

It takes the asset url and checks it against the library; if the video is available, the if(!asset) returns true and puts up the alert view (the delegate hands it back to the view controller to finish up, but it could do whatever it needed in here. The failure block traps the situation where the user has denied access to photos on the device.

As mentioned in the original question, the html given above works fine for displaying the video. So, I first check that the URL exists and if i does, I proceed to playing it. The same UIWebView subclass is used whether the video is a youtube or a video stored on the device; basically only the HTML and the videoID is different (videoID contains the youtube id for youtubes, and the asset URL for local.

Thanks for your reply(s) on this.

Respondido el 10 de Septiembre de 13 a las 08:09


No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.