WebView load HTML page with all resources from a local directory - xcode

I am developing a COCOA application. It's a requirement of the application to load HTML page from a local directory. The HTML page includes links to many resources such as images, JavaScript, XML and CSS file. Hence it's more like a local website without any server. How can I achieve this using WebView control? I am developing application for Mac OS X.
Any help would be highly appreciated,
Farooq-

You can simply use this code -
[[yourWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pathOfLocalWebPage]]];
Hope this helps :)

Swift 3:
let urlpath = Bundle.main.path(forResource: "index", ofType: "html");
let requesturl = URL(string: urlpath!)
let request = URLRequest(url: requesturl!)
webView.mainFrame.load(request)

Related

WebKit WKWebView attempts to make service calls to Apple when using local URL

I'm using Webkit WKWebView to display locally stored HTML files in my OSX application for specific formatted pages.
Each of the WebView objects is linked to a WKWebview object in the storyboard of the application and is initialized using:
#IBOutlet var webView: WKWebView! = WKWebView()
I then display a local HTML using WebView:
let url = Bundle.main.url(forResource: "termsandconditions", withExtension: "html")
let urlRequest = NSURLRequest(url: url!)
outputView.load(urlRequest as URLRequest)
For each of the Webview Objects defined, XCode returns a warning
Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
Am I initializing the WebViews correctly? Why is it trying to connect to the Apple service if I'm only using a local URL?
Determined that this is an Xcode issue making a call to the Apple. When compiled and running on a different device I cannot pick up the call being made.

How can I decrease in app website load time using WKWebView?

I am trying to decrease to load time of in app websites. Our application is currently using WKWebView and right now we have this code to load the webpage
var webView : WKWebView
if let urlStr = self.webUrl, url = NSURL (string: urlStr) {
let requestObj = NSURLRequest(URL: url)
self.webView.loadRequest(requestObj)
}
Is there a way to improve the performance? Would using a dispatch method help? I attempted to use dispatch_async and didn't notice any changes. Also is there a way to track how long it took the webpage to load?
WKWebView wasn't the source of the problem. It is now apparent that the lag came from other sources in the application and WKWebView has little impact on the actual performance of loading pages. SafariWebView, although easier to implement lacks customization and seems to be slower than WKWebView.

xcode swift: how to display a website as pop-over

I am working on an app with links to external websites. I have been using code that opens safari separately, but I would like code that opens the website as a pop-over. Is this an option?
UIApplication.sharedApplication().openURL(NSURL(string:"http://www.bing.com")!)
You can use a UIWebView and load the url into it, you can add that to an existing view or create a view controller with a web view in it and present it as a popover.
let aWebView = UIWebView()
let myUrl = NSURL(string: "http://www.google.com")
let urlRequest = NSURLRequest(URL: myUrl!)
aWebView.loadRequest(urlRequest)
self.view.addSubview(aWebView)

MWPhotoBrowser code update with directory

I am using the github project called MWPhotoBrowser that pulls image files from the web into a gallery on the iPhone. The problem is the code calls for the specific url of the image file. I want to pull images from inside a directory. Any ideas on how this can be done? Here's part of the code.
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:#"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f_b.jpg"]]];
This solution involves use of the NSURL API is it can be used for web or local resources:
NSString *filePath = //path to your file
NSURL *filePathURL = [[NSURL alloc] initFileURLWithPath:filePath];
[photos addObject:[MWPhoto photoWithURL:filePathURL]];

how to make web browser in Xcode load a web page automatically

I am building a web browser in Xcode cocoa application, using the interface builder. I have it all working when it comes to typing in web address and clicking go.
but I would like for it to automatically load a web page instead of seeing white when I open my project, I looked in inspector to see if you can set a specific web address but I couldn't find anything to do this.
How about if you load the URL that you would like in the viewdidload function:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *targetURL = [NSURL fileURLWithPath:WebsitePath];
NSURLRequest *WebRequest = [NSURLRequest requestWithURL:targetURL];
[self.WebViewer loadRequest:WebRequest];
self.WebViewer.scalesPageToFit = YES;
}
Maybe you can try to load the WebView when the application finish launching,just add the URL Request in the applicationDidFinishLauching parameter,else if your WebView is on the MainView you can add a loadingScreen that appear while the webView is loading!
Lucky!

Resources