External resources loading

SWF movie can load various external resources when playing, like images, configuration files (XML or other), etc. It can also load URLs to navigate browser to another web-page or just load it. Using various delegate methods you can fully control this behavior: deny, allow, replace or handle the request yourself.

When SWF movie makes an attempt to load an external resource or open a web-page, the delegate will receive the netscapeView:shouldLoadRequest:onTarget: message.

Note: Not all such messages can be captured by Flash’In’App. For instance, if movie loads video using FLVPlayback Flash component, Flash’In’App won’t be able to track this. Such calls bypass standard browser rules and requirements, and load everything directly. So we just don’t know that they are triggered.

Listing 1 will provide a sample of netscapeView:shouldLoadRequest:onTarget: implementation. This sample shows you how you can handle the request differently depending on the URL and target supplied.

Listing 1: Example of netscapeView:shouldLoadRequest:onTarget: implementation

  - (StreamLoadType) netscapeView:(ESNetscapePluginView *)netscapeView shouldLoadRequest:(NSURLRequest *)
request onTarget:(TargetsValues)target
    if(target != Target_Null)
      //if Flash movie wants to open new page, then open it using default browser
      [[NSWorkspace sharedWorkspace] openURL:[request URL]];
      return StreamLoad_None;

    // if Flash movie tries to load local resources, then send data to Flash movie manually
    // otherwise, let FlashInApp do it
    if([[request URL] isFileURL])
      return StreamLoad_External;
      return StreamLoad_Internal;

If the target parameter equals Target_Null it means that SWF file is trying to load data inside itself by that URL, otherwise it is trying to open a web-resource by that address. If the SWF file requests the URL by the relative path, the full path will be built with the help of baseURL, in this case if baseURL is nil, data will not be loaded.


Possible return values of netscapeView:shouldLoadRequest:onTarget: method:

  • StreamLoad_None – halt further handling of the request
  • StreamLoad_Internal – load URL automatically (will be processed only if target is Target_Null).
  • StreamLoad_External – load URL in manual mode. This is where you will have to supply further data yourself for the request. This is how you can organize data replacement, for instance, if a Flash file requests the same address all the time, you can replace it with the one or the other based on various settings.
Note: To load external resources for a SWF file delegate should implement netscapeView:shouldLoadRequest:onTarget: and return StreamLoad_Internal or StreamLoad_External.

Internal loading

When loading resources internally you can receive messages about the start/end/error of resource loading.

One reason for implementing an internal resource load delegate is to track the progress of individual resource loads. For example, you can keep track of the number of resources successfully and unsuccessfully loaded by implementing the following delegate methods. In this example, the resource status is displayed as "Loaded X of Y resources, Z resource errors". Each delegate method below increments these X, Y and Z values. Follow these steps to display the resource load status messages:

1. Add these instance variables to your delegate class:

  int resourceCount;
  int resourceFailedCount;
  int resourceCompletedCount;


2. Implement the netscapeView:startLoadingRequest: method to update the display when Flash’In’App begins to load a resource.

  - (void) netscapeView:(ESNetscapePluginView *)netscapeView startLoadingRequest:(NSURLRequest *)request
    // Update the status message
    [self updateResourceStatus];


3. Implement the netscapeView:failLoadingRequest:withError: method to increment the number of failed resource loads and update the display as in:

  - (void) netscapeView:(ESNetscapePluginView *)netscapeView failLoadingRequest:(NSURLRequest *)request withError:(NSError *)error
    // Update the status message
    [self updateResourceStatus];


4. Implement the netscapeView:finishLoadingRequest: method to increment the number of successful resource loads, and update the display as in:

  - (void) netscapeView:(ESNetscapePluginView *)netscapeView finishLoadingRequest:(NSURLRequest *)request
    // Update the status message
    [self updateResourceStatus];


5. Implement the updateResourceStatus method to update your display.

6. Build and run your application.

When you run your application you should see a progress message showing the total number of resources, and progressively how many resources are loaded successfully and unsuccessfully.


External loading

When loading resources externally you should implement netscapeView:loadStream: delegate method to load the data into Flash Player plugin yourself. Listing 2 provides you with the sample how you can realize this method.

Listing 2: Example of netscapeView:loadStream: implementation

  - (void) netscapeView:(ESNetscapePluginView *)netscapeView loadStream:(ESNetscapePluginStream *)stream
    //load data object containing the data from the location specified by URL of given request
    NSData *data = [NSData dataWithContentsOfURL:[stream requestURL]];
    //create response with specifying size of data, which will be loaded
    NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[stream requestURL] MIMEType:nil 
expectedContentLength:[data length] textEncodingName:nil];
    //initialize stream object for data loading
    [stream startStreamWithResponse:response];
    //send data to stream
    [stream receivedData:data];
    //finish data sending
    [stream finishedLoadingWithData:nil];
    [response release];


Note: startStreamWithResponse: and finishedLoadingWithData: methods are mandatory to be called to initialize and finalize loading correctly.