【问题标题】:How can I load an local HTML file when iOS device is offline?iOS 设备离线时如何加载本地 HTML 文件?
【发布时间】:2017-06-11 10:48:21
【问题描述】:

我想让我的 UIWebView 在设备离线时打开一个本地 HTML5 文件,并在设备在线时让它打开一个网站。

我会留下我的文件的副本。

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

【问题讨论】:

    标签: ios objective-c html uiwebview


    【解决方案1】:

    您需要创建 html 文件并将其保存在本地,然后按如下所述加载它。

    我没有将其标记为重复,因为这是关于离线方法的。不过,答案来自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]];
    

    斯威夫特

    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)
    

    【讨论】:

    • 无论你想在哪里运行它,在哪里显示你的 webView。
    • 代码对我不起作用。我认为是因为代码不知道设备是否连接到网络。
    • 从 Apple 搜索“可达性”代码。它显示了如何检查您是否在线。
    • @SebastiánBolaños 在这里查看 uliwitness 的回答。您要求加载离线 html 这就是我的回答,如果您想知道设备是在线还是离线,那是另一个故事,这已经回答您可以在这里查看:stackoverflow.com/questions/31742601/…,如果这对您有帮助,请接受答案.总帐。
    • @Sneak 感谢您的帮助!我认为错误已经解决。感谢您的大力支持。
    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多