UIWebView accepts normal NSStrings but not a NSString from an object - iphone

So I have an object called 'theList', this contains a load of information about a student, one element is a url to an image of their work online. I am using a webview to display that image using the code:
NSString *urlAddress2 = theList.imageofwork;
NSURL *url2 = [NSURL URLWithString:urlAddress2];
NSURLRequest *requestObj2 = [NSURLRequest requestWithURL:url2];
[workWeb loadRequest:requestObj2];
This doesnt work, but when I change 'theList.imageofwork' (which contains 'http://google.com' I should say, while I test this) to '#"http://google.com"' it works absolutely fine.
Though I need this to be able to draw from 'theList' as the view is general and should change depending on the student that you select. So my question is what am I doing wrong or is there some other way that I should be going about displaying images?
Also, within theList.h, imageofwork is declared as:
#property (nonatomic, retain) NSString *imageofwork;
and in theList.m I have:
#synthesize imageofwork;
I should also mention that I am relatively new to xcode and this is my first post here!
Thanks in advance.

You should read the documentation of the UIWebView class more carefully.
Here is what you should need: loadHTMLString:baseURL:

Related

UIWebView loading and general performance

I am using UIWebView to display textual content in my app (I store the content in local HTML files that I pack with the app). All together, I have three web views whose content I change dynamically based on user feedback.
Although some might argue that this is not the most accepted way, I find UIWebView very convenient to display formatted text, and modify that text using HTML if necessary. While this works 99% of the time, on occasion, I experience problems that generally fall into one of these categories:
Sometimes, the web view content loads slow and is late a second or so.
The content loads but is not showing. However, as long as, I touch the view (try to scroll or something) the content pops in.
A few times I received memory warnings (usually not long after the app's initial loading) that in no way affected the performance of my app. It logged the memory warning but the app worked like nothing happened.
This is the method I use to load content to my web views:
- (void)loadSimpleDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
Aside from this, the shouldStartLoadWithRequest delegate method is also implemented, always returning a YES.
My question is: Is there a way to improve the reliability/performance of my web views (in particular loading)? Is there something I have overlooked, did wrong, or do not know about?
Some additional info:
I am on iOS6 SDK, use ARC, and do everything programmatically (do not use IB or storyboard).
You have 2 options to check what's going on:
Implement webViewDidStartLoad & webViewDidFinishLoad delegate methods to check why the content isn't showing (may be the content isn't loaded yet).
read the html content to an NSString then use loadHTMLString:baseURL instead of using loadRequest and check if it loads faster.
Hope this could help.

Setting up Contact Form in Xcode

I'm relatively new to Xcode, and am trying to style and create a very basic app before I move onto more complex things - so I'm learning all the time.
So I've got my Contact Form view controller all styled up, and I'd like to figure out how to pull the inputted data through so when the user clicks Submit, it send it all through to the defined email address. I'm aware this requires a bit of php server side which I'm happy with, but it's the basic collating in Xcode that's causing an issue - allocating actions/code etc.
Plenty of tutorials explain how to create a form from scratch, but it's implementing it into an existing project that's proving tricky - I have a view Controller setup already with the required interface.
The screenshot of my view controller is here: http://pixelproofdesign.co.uk/stackoverflow/View%20Controller%20Screenshot.png
You can find the project files here: http://pixelproofdesign.co.uk/stackoverflow/Pixel%20Proof_NEW.zip
Any help would be hugely appreciated - it's driving me round the bend!
Connect each text field of the form to an outlet in the .H file. This will then allow you to get at the text property when the user taps the Contact Us button.
#property (weak, nonatomic) IBOutlet UITextField *contactName;
#property (weak, nonatomic) IBOutlet UITextField *contactEmail;
#property (weak, nonatomic) IBOutlet UITextField *contactPhone;
#property (weak, nonatomic) IBOutlet UITextField *contactDescription;
Add an action to that button and concatenate the text fields into an NSString, e.g.
NSString *message = [NSString stringWithFormat:#"name=%#&email=%#&phone=%#&description=%#",
self.contactName.text,
self.contactEmail.text,
self.contactPhone.text,
self.contactDescription.text];
Then you can send this as a POST and the PHP will get each field as a variable. See also sending form data via HTTP POST
when you are done collecting data build up up a NSDictionary
id dict = #{#"formField1": formField1.stringValue; #"FormField2": formField2.stringValue};
=> the resulting dictionary you form encode so you can put it to html (category to get postData
id data = [dict postData];
=> then transfer it using NSURLConnection
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"www.url.de"]];
[request setHTTPBody:data];
[request setHTTPMethod:#"POST"];
[request setValue:#"multipart/form-data, boundary=--------------BOUNDARY------------------" forHTTPHeaderField:#"Content-type"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
Disclaimer:
I wrote this inline here and never tested it for typos
This is a very basic way to do it and when it gets more complex, I'd look into using a Framework ...e.g. AFNetwork

how to read pdf file in iphone

I want to read pdf file in iphone/ipad and i want to give animation(turn page) effect each page of pdf.
For just regular viewing u can use UIWebView. Once you have created this object assign it an IBOutlet and do something like this:
[mywebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithString:[[NSBundle mainBundle] pathForResources:#"myPDF" ofType:#"pdf"]]]];
To embed page flip features you can Subclass UIWebView and and start playing around.

iPhone: How to Display Text from UIWebView HTML Document in a UITextView

I have an RSS feed that gets arranged in a UITableView which lets the user select a story that loads in a UIWebView. However, I'd like to stop using the UIWebView and just use a UITextView or UILabel.
This png is what I am trying to do (just display the various text aspects of a news story):
I have tried using:
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
and assigning the string to a UILabel but it doesn't work from where I am implementing it in webViewDidFinishLoad (--is that not the proper place?). I get a blank textView and normal webView.
If I overlay a UITextView on top of a UIWebView on its own (that is, a webView that just loads one page), the code posted above works displays the text fine. The problem arises when I try to process the RSS feed .
I've been stuck wondering why this doesn't work as it should for a few days now. If you have a better, more efficient way of doing it then placing the code in webViewDidFinishLoad, please let me know! Does it go in my didSelectRowAtIndexPath?
Thank you very much in advance!
I think the you should first log the string returned by :
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
... in the case of the RSS feed to make sure that you are getting something back. It's possible the RSS page doesn't have the same javascript components and that it returns an empty string.
Once you've confirmed that, then it becomes a simple matter of getting it to display properly in the text view.
If the NSString you want to display is not empty, try to do something like this in the webViewDidFinishLoad method:
[yourUILabel performSelectorOnMainThread:#selector(setText:) withObject:[NSString stringWithFormat:#"bla bla %#", #"more bla"] waitUntilDone:YES];
The main thread of an iphone app is responsible for drawing components, that is why your label doesn't show your text.
You could also try setting setNeedsDisplay: to true
Also, the UILabel will not preserve the HTML format. It will display it as just text.
You could try the following:
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML;"];
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML;"];
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerText;"];
You lose out on formatting information with the last line of code.

How to save and view a pdf file through an app on iPhone?

I am trying to save a pdf file in SQLite. I think it will be possible by saving the pdf file as blob. And the problem starts here, how to view that file within the iPhone app now?
I know it can be done using this code:-
#interface PDFViewController : UIViewController {
UIWebView *webView;
NSURL *pdfUrl;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) NSURL *pdfUrl;
#end
#implementation PDFViewController
#synthesize webView, pdfUrl;
- (void)viewDidLoad {
[super viewDidLoad];
// Tells the webView to load pdfUrl
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
}
But what should be the value of the pdfUrl,I am confused...
And if I retrieve it from the DB it will be in a blob format(methinks) so how to convert it into a NSdata object?
Thank You All.
The problem is that WebView does the URL loading, and it won't understand things like non-http requests; yet your application is trying to serve the data. So basically, you're out of luck, unless you can deserialise the blob out of the sqllite database, write it to a file (in /tmp, say) and then open up a file:/// URL from the NSWebView - but I don't believe that the object will let you do that.
You might be able to invoke the [UIWebView loadData:MIMEType:textEncodingName:baseURL:] method, which will allow you to extract the NSData blob from the sqllite database, supply a mime-type of application/pdf, stick in a dummy text encoding (UTF-8) and base URL, and then it might do what you want. But I'd be tempted to try that at least.
the pdfUrl is the web address of the PDF you are tryign to download from the website. (ie http://website.com/mypdf.pdf)
Read the documentation on NSData, it has a method to allow you to create NSData objects based no what you pull out using sqlite.