To view web page in webview - objective-c

I have, for exa, "Facebook" button in SocialWeb.xib. I want that if user clicks on that button, the webview, defined in another View.xib, should view Facebook page. What is the code for View.xib that I must write?

I think you should coordinate this event via your controller, anyway. Can't say exactly what to do without your code. Try this
//Load web view data
NSString *strWebsiteUlr = [NSString stringWithFormat:#"http://your.url"];
//Create a URL object.
NSURL *url = [NSURL URLWithString:strWebsiteUlr];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];

Related

UITextView clickable url opens after terminating app

I am using datadetectortypes with my UITextView to be able to detect the links and navigate to the page. but the app is terminated and link opens with default safari after terminating the app. Is there any way to open the link within the application?
You can do that if you have web view
for example
NSString *urlAddress = #"http://myurl.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[myWebView loadRequest:requestObj];

pages.zip file will fail to displays view using UIWebview and NSURL

I have an application that displays search tips. This will work if I store an .html file in the main bundle and display it using the Apple example code:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
//load document --
[self loadDocument:#"searchTipsLegislators.html" inView:searchTips];
If however I change the document to one created using a compressed Pages file the app return the error:
2010-12-15 08:53:04.537 cv112[42067:7003] Cannot load iWorkImport
2010-12-15 08:53:04.538 cv112[42067:7003] Failed to generate preview
Has anyone gotten this to work?
Two things to try:
Don't compress the pages document. This is no longer necessary as of iOS 3.
Don't expect this to work on Simulator. Does it work on your device?

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];

ipad app opens browser in the background

Im using the openURL command to send an SMS, but it opens the safari,
can I send the command so it opens in the background??
ie, the user dont see the browser opening or the message that it gives, and stays in the app,
NSURL *url = [NSURL URLWithString: #"https://smsgw.exetel.com.au/sendsms/api_sms.php?username=xxx&password=xx&mobilenumber=0xxx&message=xxr&sender=mk&messagetype=Text&referencenumber=04"];
[[UIApplication sharedApplication] openURL: url];
Thank you
You can't have Safari open a URL for you in the background. But supposing you just want to request the URL and ignore whatever comes back, you could try:
NSURL *url = [NSURL urlWithString:#"...whatever..."];
NSURLRequest *request = [NSURLRequest requestWithURL:url delegate:nil];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request];
[connection start];
You can do this be creating a UIWebView inside your app, and calling loadRequest: on the instance, like this:
UIWebView *webView = [[UIWebView alloc] init];
webView.delegate = self;
NSURL *url = [NSURL URLWithString: #"https://smsgw.exetel.com.au/sendsms/api_sms.php?username=xxx&password=xx&mobilenumber=0xxx&message=xxr&sender=mk&messagetype=Text&referencenumber=04"];
NSUrlRequest *request = [[NSUrlRequest alloc] initWithURL:url];
[webView loadRequest:request];
This will load the URL inside the app, without sending you over to Mobile Safari. The benefit of this method over NSUrlConnection is that if you conform to the UIWebViewDelegate protocol and implement webViewDidFinishLoad: in your delegate class, you can see if the call succeeded or not.

reading excel sheet with url in Iphone sdk

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];
}