Problem uploading NSString to server - iphone

I am struggling to get one particular string to upload, using php and mysql I have a number of strings from textfields that will successfully go, this particular string is the text from a label in another viewcontroller. I can see the text with NSLog but for some reason it will not go, I'm confident the server side is working properly as I can pass other strings to that particular row in mySQL.
To get the text from the other view I am using
- (IBAction)submit:(id)sender;
{
GetLocation * getlocation =[[GetLocation alloc] initWithNibName:Nil bundle:nil];
getlocation.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
getlocation.rwanswer=self.rwanswer.text;
[self presentModalViewController:getlocation animated:YES];
[getlocation release];
}
in the first viewcontroller then in the view that is uploading to the server I synthesize the string.
NSString *rwanswer;
#property (nonatomic, copy) NSString *rwanswer;
I can see the text with NSLog but for some reason I cannot get it to upload.
-(IBAction)UploadData :(id)sender {
NSString *appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleDisplayName"];
NSString *result =rwanswer;
NSLog(#"result=%#", result);
NSString *comment =comments.text;
NSString *name = username.text;
NSLog(#"name=%#", name);
NSString *Website =[NSString stringWithFormat:#"http://www.ivectorn.com/Set/Submit.php?name=%#&comments=%#&appname=%#&results=%#", name, comment, appname, result];
[BackroundLoader loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:Website]]];
Any help would be great

I know this problem. I think the problem is by your Website-String. Try to Log int and you will see, that there are characters inside you cannot put in a URL. The following method will help you if this is your problem:
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Instead of
NSString *appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleDisplayName"];
NSString *result =rwanswer;
NSLog(#"result=%#", result);
NSString *comment =comments.text;
NSString *name = username.text;
To the following:
NSString *appname = [[[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleDisplayName"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding
NSString *result = [rwanswer stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding;
NSLog(#"result=%#", result);
NSString *comment =[comments.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding;
NSString *name = [username.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding;
I hope this will help you. I struggled with the same problem some time ago.
Sandro Meier

Related

Parsing HTML <Tag> into ios

i am Parsing HTML Tag into iOS using Hpple. i am able to parse the data where the HTML Tag is
<div id="NewsPageSubTitle">
<p><**span** hi how are you>
Using ios code:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p/span ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but in few case i don't have span, imean the string in html is accessed by tag "p" directly like:
<div id="NewsPageSubTitle">
<p>< hi how are you>
Here I am using ios code as:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but here i am getting a blank data in response.
can any one let me know how to solve the problem?
Since sometimes the para tag has span and sometimes it does not, I would suggest trying to handle that by looping over the children
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
TFHpple * tutorialsParser = [[TFHpple alloc] initWithHTMLData:data];
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageSubTitle']";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
for (TFHppleElement * element in tutorialsNodes) {
NSLog(#"%#", element);
NSLog(#"%#", [element tagName]);
NSLog(#"%#", [element attributes]);
NSLog(#"%#", [element children]);
for (TFHppleElement *childElement in [element children]) {
NSLog(#"%#", childElement);
}
}
Check with this: https://github.com/mwaterfall/MWFeedParser
This will provide the HTML Parser for iphone sdk.
More help on:
this blog and here.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"html" inDirectory:#"New Folder 2"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSString *htmlString = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
TFHpple * Parser = [[TFHpple alloc] initWithHTMLData:data];
NSString *query = #"//p";
NSArray *nodes = [Parser searchWithXPathQuery:query];
for (TFHppleElement *item in nodes)
{
NSLog(#"Title : %#", item.content);
NSLog(#"URL : %#", [item.attributes valueForKey:#"href"]);
}

initWithContentsOfFile Deprecated

I was wondering what code I could use instead of initWithContentsOfFile: as I've been searching for something that isn't deprecated but cannot find anything. I'm trying to display a local HTML file within a webview, below is the code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath];
[self.myWebView loadHTMLString:html baseURL:bundleUrl];
}
And I'm getting a warning saying that initWithContentsOfFile: is deprecated and would like to know how to update it.
Try this:
NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:#"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error];
Use the newer initializer:
NSString's [initWithContentsOfFile: encoding: error:] method, where you can get a useful error passed back to you if there are any problems. I've linked the documentation for you. If you don't know the encoding of the file, you can also use another method that has a usedEncoding parameter.

How to use method stringWithContentsOfFile:encoding:error

I don't know how to use this method.
I want to get the data in an rtf file as a NSString, however I can not.
Here is part of my code.
NSString *filePath = #"path of the file";
NSString *ImagePath = [[[NSString alloc] init] autorelease];
imagePath = [NSString stringWithContentsOfFile: filePath encoding:(NSStringEncoding)nil error:(NSError**) nil];
I can't readout the data in that .rtf file. I am appreciate that everyone may help me. Thanks.
Try using this
imagePath = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:nil];
Here is the function
NSString *content = [[[NSString alloc] initWithContentsOfFile:filepath
usedEncoding:nil
error:nil] autorelease];
before using this be sure u giving a right path (filepath) to the string.

Load different html file on one UIWebView

iam going to change my UIWebView files via UISwipeGuesture but have problem :
i have 100 HTML files that loaded on UIWebView , so i want implement something like this :
if user swipes the view my webView will load next html file but the problem is my code just load first HTML file :
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"file%d",+1] ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.myWebView loadHTMLString:htmlString baseURL:nil];
[NSString stringWithFormat:#"file%d",+1] is always going to return the string file1, so you're always loading file1.html.
Do you mean to be using a counter or index instead of the number 1?
EDIT: More code. This isn't how I'd recommend implementing this, but it should give you an idea and it's relatively compact. (I'd use an ivar or property to track the current file index.)
static NSInteger currentIndex = 0;
if (currentIndex < 99) {
currentIndex++;
}
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"file%d",currentIndex] ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.myWebView loadHTMLString:htmlString baseURL:nil];

How to get List of Images Using NSBundle

I am having List of images in my project i have to get the entire list Using NSBundle, How can i do this
Thank You
This should do it:
NSLog(#"Images in bundle are:");
NSString *imageExtension = #"png";
NSArray *pngPaths = [[NSBundle mainBundle] pathsForResourcesOfType:imageExtension inDirectory:nil];
for (NSString *filePath in pngPaths)
{
NSString *fileName = [filePath lastPathComponent];
NSLog(#"%#", fileName);
}
Just change the imageExtension string for the image type you want.