0

I want to make my UIWebView open a local HTML5 file when the device is offline, and make it open a website when the device is online.

I will leave a copy of my files.

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>


@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    sleep(9.5);

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

ViewController.m

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>


@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Load the url into the webview
    NSURL *url = [NSURL URLWithString:@"http://app.relaxemy.com"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 Answers1

2

You need to create your html file and save it locally and you load it as described below.

I am not marking this as a duplicate since this is regarding offline methods. However, the answer is taken from this thread:

Objective-C

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];

Swift

let htmlFile = NSBundle.mainBundle().pathForResource("fileName", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(html!, baseURL:nil)

Swift 4.x

let htmlFile = Bundle.main.path(forResource: "fileName", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
webView.loadHTMLString(html!, baseURL:nil)
Martin Westin
  • 1,419
  • 1
  • 14
  • 25
  • Wherever you want to run it, where you are showing your webView. –  Jan 26 '17 at 06:02
  • The code is no working for me. I think it's because the code does not know if the device is connected to a network. – Sebastián Bolaños Jan 26 '17 at 06:13
  • Search for "Reachability" code from Apple. It shows how to check whether you're online or not. – uliwitness Jan 26 '17 at 09:51
  • @SebastiánBolaños Check what uliwitness answered above here. You asked for loading offline html thats what I answered, if you want to know if the device is online or offline, thats another story, this has already been answered you can check here: http://stackoverflow.com/questions/31742601/how-to-check-internet-connection-on-ios-device , please accept the answer if this helps you. GL. –  Jan 26 '17 at 13:04
  • @Sneak I appreciate the help! I think the error is already solved. Thanks for the great support. – Sebastián Bolaños Jan 27 '17 at 02:03
  • @Sneak One question more... What am I supposed to write in the @"Sample" part of the code on top? – Sebastián Bolaños Jan 27 '17 at 02:39
  • @SebastiánBolaños If your file is named sample.html then u do as the example. if your file is named index.html , then you do "index" instead. etc . If you need more "advanced" html to show, like entire websites etc. go with the cache solution, it is more advanced tho and you need to read documents and expand your code around it. http://stackoverflow.com/questions/7695874/save-the-web-page-offline-for-read-later-on-ios –  Jan 27 '17 at 03:10