reading excel sheet with url in Iphone sdk - objective-c

I am Having a problem here.I am downloading the excel sheet using Url and have to displaying it in the web view here my problem is the Excel sheet is not displaying.
I written the code as:
NSString *str = #"";
str = [str stringByAppendingString:
#"http://databases.about.com/library/samples/address.xls"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[webView loadData:data MIMEType:#"application/xls" textEncodingName:#"UTF-8"
baseURL:[NSURL URLWithString:#"http://google.com"]];
[contentView addSubview:webView];
Thank you,
Monish.

Are you able to view it using Safari on the phone or does it say "unsupported format"?
Try another file that works in Safari and then try in UIWebView.
Also see this: http://developer.apple.com/iphone/library/qa/qa2008/qa1630.html
First make sure you are able to view the xls in Safari then try code like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.yourwebsite.com/good.xls"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}

Related

UIWebView is not responsive in iOS8 and Objective C

The following code was working properly in iOS7, Now with iOS8 it is not getting and result. What could be the reason and the answer?
NSURLRequest *url = [[NSURLRequest alloc] initWithURL:[[NSURL alloc] initFileURLWithPath:[NSString stringWithFormat:#"https://www.facebook.com/1079798338712435"]]];
[self.webView loadRequest:url];
Use the following code and do proper connection:
NSString *stream=#"http://youtube.com";
NSURL *url=[NSURL URLWithString:stream];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
You got a mistake: do not use -[NSURL initFileURLWithPath:] with http link
I'm sure that url is nil when running your code, and your webview loaded a NIL url, so there is nothing happened
Use -[NSURL initWithString:] instead
Good luck
I have solved it using html request instead of URL, I believe it is iOS8 bug. here is how I got the results
NSString*url2 = [NSString stringWithFormat:#"https://www.facebook.com"];
NSURL*url3 = [NSURL URLWithString:url2];
NSString*htmlString = [NSString stringWithContentsOfURL:url3 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlString baseURL:nil];

Objective-c/iOS - Open local html file with Safari

I have an HTML file save in a temporary directory like that :
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentDirectory = NSTemporaryDirectory();
NSString *documentPath = [documentDirectory stringByAppendingPathComponent:#"mydocument.html"];
[fileManager createFileAtPath:documentPath contents:myHTMLDocumentData attributes:nil];
The document is created in my temporary file. After it, I want to open this document in Safari but it doesn't work :
NSURL *url = [NSURL fileURLWithPath:documentPath];
[[UIApplication sharedApplication] openURL:url];
Nothing happen at screen, no error...
However, if I replace "url" by #"http://google.fr", Safari is launched with google.fr and I can access to my temporary file by typing the url "file://localhost..../myHtmlDocument.html" in Safari.
Hope you can help me
You cannot open a document with resides in your app bundle through Safari (please, see iOS Security Model).
What you need is using an UIWebView to display your document content inside your app using – loadHTMLString:baseURL:; e.g.:
UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];
i dont think you can achieve that, since both UIApplication openURL: and UIDocumentInteractionController will not open local files inside your bundle
Do the following instead, in your viewDidLoad
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath: documentPath]];
[webView loadRequest:request];

UIWebView doesn't load content

I don't understand why if I load the content of a UIWebView in XCode this way:
NSString *string = #"http://www.dummyurl.org/dummy.pdf";
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
everything works fine, but if a build the original string from a php script I wrote:
NSString *strURL = [NSSTring stringWithFormat:#"www.myserver.php"];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
nothing works. I checked my script and it returns EXACTLY the original string (http://www.dummyurl.org/dummy.pdf) that works fine with the first method.
I build from a PHP script the content of a UITextView too, but that works fine.
I don't understand why this method works with the UITextView but not with the UIWebView to load the .pdf.
It may be useful
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *tempString = [string stringByTrimmingCharactersInSet:characterSet];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
Make sure you linked the webview (IBOutlet) and the delegate.
With this lines it should load a url:
webView.delegate = self;
NSURLRequest *urlRequest;
NSString *urlToOpen = [NSString stringWithFormat:#"http://www.stackoverflow.com"];
urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlToOpen]];
[self.webView loadRequest:urlRequest];
Hope this helps...
I had this and it was a cache problem:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];

How can i detect Phone number and Hyperlink from Pdf file?

I am displaying my pdf file in UIWebView now i want to detect links and Phone number which is available in my pdf and by pressing link it should open in browser and touching on number it should be called on that particular number so please anybody have idea about it.Give me some suggestion on it.I have written this code for achieving this task but it do not work:
webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 764, 1004)];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Gita.pdf"];
NSLog(#"FilePath==>%#",filePath);
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSLog(#"url==>%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView setDataDetectorTypes: UIDataDetectorTypeAll];
[webView loadRequest:requestObj];
check this link:
http://bill.dudney.net/roller/objc/entry/tiledlayer_on_the_iphone and http://bill.dudney.net/roller/objc/entry/catiledlayer_example.
Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

Display PDF in UIWebView using loadData

I am trying to display a PDF I have stored locally in a UIWebView. This is how I currently attempt to do this:
if (![[NSFileManager defaultManager] fileExistsAtPath:self.url]) {
LOG_ERROR(#"Couldn't load local file. File at path: %# doesn't exist", self.url);
return;
}
nsurl=[NSURL fileURLWithPath:self.url];
NSData *data = [NSData dataWithContentsOfFile:self.url];
LOG_DEBUG(#"data length:%d",[data length]);
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I have also tried passing nil for textEncoding, as well as using UIWebView's loadRequest. The result is a UIWebView that displays a blank page. No errors occur in the UIWebView delegate method. The strange thing is that data has the correct length, in bytes, for the PDF I am trying to display, which means the file is being found and loaded correctly.
Does anyone have an idea as to what might be going wrong here or how I can better debug this problem?
Below you can find two different approaches to open a .pdf file on your UIWebView; one from a url and one as NSData:
if (self.pdfDataToLoad) //Loading the pdf as NSData
{
[self.webView loadData:self.pdfData
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil];
}
else //Open the pdf from a URL
{
NSURL *targetURL = [NSURL URLWithString:#"https://bitcoin.org/bitcoin.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
}
It turns out the problem was something to do with the file format of the PDFs. I edited them using Illustrator and re-saved them. I guess Mobile Safari doesn't like the way Illustrator formatted the files because each was blank when viewed using the simulator's browser (although I could open the PDFs in regular Safari just fine).
The solution was to open the PDFs using Preview and re-save them. After running each PDF through the Preview save routine I was able to get each PDF to display in a UIWebView without changing any of my code.
You don't need NSData to load local file, loadRequest should work directly with NSURL
[self.webView loadRequest:[NSURLRequest requestWithURL:nsurl]];
I can only suggest to make NSURL like that
nsurl = [NSURL URLWithString:[self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData is used to load the pdf files when they are stored locally in any of the directories.
To load the pdf which was in your application, use the following code .
NSString *path = [[NSBundle mainBundle] pathForResource:#"FileNameHere"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];