Hi i want to make a few http request and generate html from within my ipod app. I hear i want a WebView to view the html as if it were a browser. So my question is how do i make the http request? How do i parse json (or xml) and how do i save/load data? I figure i can use webstorage to load/save the data however i am thinking since its an ipod app i have a better way of storing it?
Here's a snippet to load a local html web page contained inside your app bundle:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"mywebpage" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Another alternative is to embed a local web server running in a background thread, and use a URL to localhost.
A UIWebview supports HTML5 local storage.
Here is the working code snippet. for iPod/iPhone/iPad;
Works with iOS7 and iOS6.. I have not tested in others...
UIWebView *_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
Related
I have checked the official links for doc support, and it's clear that docx is not supported in UIWebview.
But, I also found out that some guys were able to open docx files in UIWebview:
Cannot open docx file through webbrowser in app using OpenIn functionality
Why doc and docx losing style information in iOS?
How to open Microsoft Office documents in iPhone
Also, afaik, iOS safari browser is built upon UIWebview and I am able to open docx files from internet in the browser.
However, when I download a docx from my test server (I have cross checked the downloaded docx by importing it from simulator and it opens perfectly on my mac), I am unable to view it in UIWebview.
I am confused,
Is docx Supported or not?
Or it seems that docx has further variety of formats out which some are supported?
Here is my loading code:
NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];
Yes UIWebView supports the docx. Just tried loading a docx file from bundle and it seems working fine(in this example named "DOC1.docx")::
NSString *path = [[NSBundle mainBundle] pathForResource:#"DOC1" ofType:#"docx"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mywebView loadRequest:request];
and If you are trying to display a docx file residing on a server somewhere, you can simply load it to your web view directly:
NSURL *targetURL = [NSURL URLWithString:#"http://www.central.wa.edu.au/Current_Students/JobsCareersandLeavingCentral/Documents/Resume%20Template.docx"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mywebView loadRequest:request];
Don't know why but using the URL scheme to load docx didn't work, (below code didn't work)
NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];
Instead, loading the file in memory(using NSData) and loading the data with MIME type worked (the below code worked like charm!)
NSString *path = [urlFileInView path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
webViewForDocsView.delegate = self;
[webViewForDocsView loadData:data MIMEType:#"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:#"UTF-8" baseURL:nil];
Using UIWebView to display select document types
As per the Apple documentation, docx isn't supported by UIWebView and is deprecated.
To open docx in app use UIDocumentInteractionController.
DispatchQueue.main.async {
let docOpener = UIDocumentInteractionController.init(url: fileURL)
docOpener.delegate = self
docOpener.presentPreview(animated: true)
}
I tried to build a radio in my app using webView. But on loading the link my app crashes every time.
- (IBAction)playMusic:(id)sender {
NSString *stream = #"http://www.bbc.co.uk/radio/listen/live/r2.pls"; //just an example link
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[Webview loadRequest:urlrequest];
}
...but with a page link (like www.google.com) everything works fine.
Maybe there is also a better solution than using webView to play radio.
Thanks to all of you in advance
UIWebView is indeed not meant to play audio files. I would recommend you to use MPMoviePlayerController (part of the MediaPlayer framework) for what you're trying to achieve. I wrote some sample code to below to show how you how to get started.
If you need more information, I suggest you to take a look at the HTTP Live Streaming Resources or MPMoviePlayerController Class Reference
NSURL *URL = [NSURL URLWithString:#"http://www.bbc.co.uk/radio/listen/live/r2.pls"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:URL];
[moviePlayerController prepareToPlay];
[[moviePlayerController view] setFrame:CGRectMake(0.f, 0.f, [[self view] frame].size.width, 24.f)];
[[self view] addSubview:[moviePlayerController view]];
[self setMoviePlayerController:moviePlayerController];
I am trying to open a PDF file in my app Bundle on Safari. I can open this PDF with applications like iBooks or other PDF readers. However I cant get it to open it with safari. Is there any way in which i can achieve this? I tried OpenURL but that doesnt work???
Why don't you open in uiwebview?
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
You can't open that in Safari since iOS apps are sandboxed. The solution available now would be to open the PDF using UIWebView or send it to another app that can handle PDF files.
I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com
Here is my code, it doesn't seem to work for RSS pages, but works fine for google.com (or any other plain old web page), any idea why?
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 510, 1024, 230)];
NSURL *url = [[NSURL alloc] initWithString:#"http://feeds.marketwatch.com/marketwatch/topstories/"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[wv loadRequest:request];
The Marketwatch RSS URL you are using in your example returns XML, not HTML. The reason it looks like HTML when you view it in a browser is that it has an xml-stylesheet attached to it that the browser is using to render a human-readible view. However the UIWebView component does not do the same thing.
For your purposes you'd probably want to request this XML, parse it, and use the contents in a UITableView.