Open a .webarchive on the iphone?

15,841

Solution 1

webarchive is supported on iOS. Just load it on UIWebView. It just works!

for loading a webarchive on your bundle, just do

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"myFile"
     withExtension:@"webarchive"];

[webView loadRequest:[NSURLRequest requestWithURL:fileURL]];

webview is your UIWebview

Solution 2

a .webarchive is just a plist; theoretically, you could read it using NSPropertyListSerialization and then build a local file structure on the phone, then pump that into a UIWebView. Or use AirSharing.

Solution 3

Expanding a little on suggestions above, if you just want to grab the HTML, the following code snippet may be a good starting point. By inspecting the webMainResource dictionary you can extract other material too, such as images.

#define WEB_ARCHIVE @"Apple Web Archive pasteboard type"

- (NSString*) htmlStringFromPasteboard;
{
    NSData* archiveData = [[UIPasteboard generalPasteboard] valueForPasteboardType:WEB_ARCHIVE];

    if (archiveData)
    {
        NSError* error = nil;
        id webArchive = [NSPropertyListSerialization propertyListWithData:archiveData options:NSPropertyListImmutable format:NULL error:&error];

        if (error)
        {
            return [NSString stringWithFormat:@"Error: '%@'", [error localizedDescription]];
        }
        NSDictionary* webMainResource = [webArchive objectForKey:@"WebMainResource"];
        NSData * webResourceData = [webMainResource objectForKey:@"WebResourceData"];

        NSString* string = [[NSString alloc] initWithData:webResourceData encoding:NSUTF8StringEncoding];

        return [string autorelease];
    }

    return @"No WebArchive data on the pasteboard just now";

}
Share:
15,841
Brad Parks
Author by

Brad Parks

Web programmer, interested in node js, cross platform development, and automating the things!

Updated on June 27, 2022

Comments

  • Brad Parks
    Brad Parks almost 2 years

    Does anyone know if you can programmatically open a .webarchive on the iPhone? A .webarchive is Safari's way of packaging up a webpage and it's associated resources into a single file.

    I tried creating one and browsing to a link to one in mobile safari, but it didn't work....

    Note: I was kind of hoping this could be done without a 3rd party app, as it'd be a nice way to package up a WebApp for use on the iphone without needing a third party tool.

  • Brad Parks
    Brad Parks over 15 years
    Note: I'm kind of hoping that it can be done without a third party app, as it'd be a nice way to package up HTML+Javascript for a WebApp
  • Lomithrani
    Lomithrani almost 13 years
    This wasn't always the case. Do you know which version of iOS added support for this?
  • Kai Engelhardt
    Kai Engelhardt over 12 years
    it doesn't work for me - i'm only getting a white uiwebview. (iOS 5.0.1)
  • Duck
    Duck over 11 years
    How did you create your webarchive? Just load the page on safari and save it as webarchive. Always worked for me since iOS 3.x