form in app?

Discussion in 'iOS Programming' started by MACloop, May 19, 2010.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    Hello,
    is it possible to add/use a form (some kind of contact form) in an app. For instance - if the user clicks on the UITabBar he/she will get to a form and this form will be send to a specific e-mail, like you can on a webpage? Is it possible? Is there anything in the SDK enabling this? Do I have to use a UIWebView? Are there any other possible solutions? How would you solve something like that? Any advice? Thanks in advance!
    MACloop
     
  2. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #2
    Well you could just have some UITextFields on a view and on submit, upload the information to a webserver using the POST command. Where would you be storing this information?
     
  3. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #3
    Well, I do not really know where to store the information. My thought was to let the user send me an feedback email within the app, without having to leave the app and use the e-mail client on the iPhone. But I do not know if it is possible. If I send the information to the websever, what do I need on the server side? I assume some php-lib to handle this? Do you know any tutorials on this issue?

    Thanks in advance!
    MACloop
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You cannot send email using the built in email app/settings/accounts without the user seeing the email and clicking send.
     
  5. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #5
    ehm...I do not really understand what you are meaning with your comment :-( Is it possible to implement some form-like part into an normal native app?
    Thanks in advance!
    MACloop
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    It's nothing to do with the "form". As far as I understand it a form is just a series of inputs. What you do with the data is up to you. I suspect you are thinking in HTML terms with a form tag and a mailto: action. If so then you need to do all the work of the mailto: action.

    My comment was targeted at "My thought was to let the user send me an feedback email within the app, without having to leave the app and use the e-mail client on the iPhone.". You can do this but the email will be displayed to the user using the normal compose mail interface. The user then need to tap send. You cannot automate this. The only way round that is to ship your own mail sending infrastructure within the app (and not use the users mail account/server etc).

    If you are going to have to have some sort of server-side script (i.e. you don't want to handle the output manually) then I don't really see why you want to use email at all. Just use NSURLConnection to connect to the URL that is the script in the server and send the data directly.
     
  7. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #7
    Thanks alot for the details. Now, I understand what you mean :) The example with NSURLConnection sound interesting. Could you give me some more details about it? As I understand, I collect all the data tipped in by the user and send this data with a NSURLConnection method, to a webserver where a script take care of the rest? It might be a solution, but I have to know what I have to do on the server side/additionally to that what I do in the iphone app...

    MACloop
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    These are the things I expect you to research and work out for yourself. I'm not about to spoon feed you code.
     
  9. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #9
    Ok, no problem. I have done some reading and the thing making me abit confused is to let the app direct communicate with the server. Is that safe? I intend to create a php-script on my web server and with the action(is the user clicks the send-button) in my form do I send the data to this script. The script self will send the e-mail with the data.

    MACloop
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Why are you emailing data? That bit confuses me. What is the end destination of the data?
     
  11. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #11
    ok, well if this is somekind of feedback form, the end destination must be some kind of e-mail address, like info@some_company.com. The person interested in the information written into the form, will get an e-mail with the thoughts sended from the user. I thought this would be the easiest way(?) to solve this. Do you have any other suggestion?
    MACloop
     
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    If you want silent mailing (so the user fills in your form and an email gets sent without them seeing it) then either a server-side executable or bundling a mail library (and outgoing mail server address/username/password) would work. The reason I ask is that if you've already got the data at the server I would just be loading it into a database and providing a secure web front-end so it could be viewed. Perhaps mailing aggregated data every day/week/month (as appropriate).
     
  13. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #13
    Thanks alot - that is a good idea with the database. I have to investigate it a bit more in detail. Thanks for now!
    MACloop
     
  14. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #14
    Hello again,
    ok, all the technic is clear to me, how to implement a form-like part into an app. One big and very important question thou, is the security. To me it does not seem to be very safe soultion, to let the app directly communicate with a php-script on a server. At least the web-programmer-part of me tells this is not good practice. Is it? What are the risks? possible walkarounds?

    I assuem that situations like this one are common, when data are written to a server via some app? I have search the internet on this issue but did not really find anything interesting... any ideas? hints?

    Thanks in advance!
    MACloop
     
  15. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #15
    What strikes you as unsafe? Or looking at it another way what would be safer using a form in a webapp? How do you think that communicates with the server?
     
  16. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #16
    Well, I thought sending a command to a php-script on a server opens up a line directly to the server. It sounds not so safe... When designing webpages (not for iphone in this case) the scripts used on a page are on the same server as the files using them. Regarding webapps - I have no experience with webapps and I do not know what methods and security rules to use when programming a form for a webbapp for iphone. Is this the common way? I am no security professional, but to present the actual script in the source code on a webpage, does not seem to be a good solution.
    MACloop
     
  17. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #17
    OK, I assumed that when you said "At least the web-programmer-part of me" that you had some experience. Clearly not.

    You are not more sending a command than you are when you submit a form. If you do this correctly you are simply sending the data. It is up to the script on the server to handle that data and decide what action to take. Only a complete idiot would send the actual SQL command to insert the data. That way you will get hacked.

    I would suggest you format your data up as an HTML GET request, use NSURLConnection to send it and that your script on the server should perform all the normal validation checks that you would expect on the data. If they data is sensitive use an https URL.
     
  18. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #18
    Ok, I would certanly not send a command to the server, that is not what I meant. I only want to send the data, but I have to give this data an address, ie the script on the server to be used and I have to include this into my code. All action is of course to be done on the server side.

    MACloop
     
  19. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #19
    At some level the address will have to be known. In a web page form it's there in clear text in the action attribute of the page. If you were sending email you'd have to include the mail address in your app. I personally don't see this as an issue.
     
  20. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #20
    Ok thanks for the help.
    MACloop
     
  21. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #21

    I thought of something like, I collect all the values filled into the form in a string and append it to the data to be send to the server.
    After that I use NSURLConnection to send it.
    Thats is very simple, but what should I be aware of when doing this regarding security. I have read in the documentation and the method
    Code:
    connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
    seems to be what I am looking for. What about dataFormat? Other issues that may appear?
    How do I handle a https URL?
    Why do you suggest GET request instead of POST?

    Thanks in advance!
    MACloop
     
  22. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #22
    Because it's a lot easier to handle and I'm not going to talk you through using POST mode.
     
  23. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #23
    ok, I just wanted to ask because the only examples I have found have been using POST.

    MACloop
     
  24. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #24
    Now I have done some reading about this. I need to use POST because I have to send more data than GET is able to. I have a script on the server side and I have controlled that it works. This script sends an email with some data filled in into the form in my app. The problem is that I do not get any email when sedning from my app. I have checked the data by using NSLog and I have checked the script. Both seem to work. I suppose that I do something wrong in my request definition. I have do the following:

    Code:
    - (void) sendRequest:(NSString*)firstname andlastName:(NSString*)last andEmail:(NSString*)email andMsg:(NSString*)nachricht {
    	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"/....../myscript.php"]];
    	
    	//set HTTP Method
    	//setting the headers
    	[request setHTTPMethod:@"POST"];
    	NSString *boundary = [NSString stringWithString:@"------------"];
    	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    	
    	
    	//adding the body:
    	NSMutableData *postBody = [NSMutableData data];
    	[postBody appendData:[[NSString stringWithFormat:@"\n%@\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data\nFirstname\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:firstname] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\n%@\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data\nLastname\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:lastname] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\n%@\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data\nEmail\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:email] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\n%@\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data\nMsg\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:msg] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithFormat:@"\n%@\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    	
    	[request setHTTPBody:postBody];
    	
    //test print here
    	NSString *s = [[NSString alloc]initWithData:postBody encoding:NSASCIIStringEncoding];
    	NSLog(@"postBody %@",s);
    	[s release];
    }


    I have tried to catch the response from the sever and I can readout the header data. In my php-file, I write out an echo with success/failure and I have also tried to catch this data but it is alway empty...?

    Code:
    #pragma mark NSURLConnection Delegates
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    	if ([response respondsToSelector:@selector(statusCode)]){
    		int statusCode = [((NSHTTPURLResponse *)response) statusCode];
    		if (statusCode >= 400){
    			[connection cancel];
    			//err msg here
    		}
    	}
    	if ([response respondsToSelector:@selector(allHeaderFields)]){
    		NSDictionary *dict = [[NSDictionary alloc]initWithDictionary:[((NSHTTPURLResponse *)response) allHeaderFields]];
    		
    		for (id key in dict) {
    			NSLog(@"key: %@, value: %@", key, [dict objectForKey:key]);
    		}
    	receivedDatas = [[NSMutableData alloc]init];
    }
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    	[receivedDatas appendData:data];
    }
    
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    	NSString *se = [[NSString alloc]initWithData:receivedDatas encoding:NSUTF8StringEncoding];
    	NSLog(@"receivedDatas %@",se);
    	[se release];
    
    Does anyone have a hint on this? What am I doing wrong? How could I seach for the error? The connection is successful and also the other parts, only the data is not send/received by the server...

    MACloop
     
  25. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #25
    Hello again,
    I have done some more reading and I have tried to do this as simple as possible. I do not get any failure msg or any other indications on what might be wrong :-( Is this the way to send a totally normal string to a script on a server?

    The form wanted by the php-script is: parameter="value"&parameter="value"&parameter="value"

    Code:
    - (void) sendRequest:(NSString*)firstname andNachName:(NSString*)lastname andEmail:(NSString*)email andMsg:(NSString*)msg {
    	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://...../script_to_use.php"]];
    		
    	//set HTTP Method
    	//setting the headers:
    	[request setHTTPMethod:@"POST"];
    	[request addValue:@"text/html" forHTTPHeaderField: @"Content-Type"];
    	NSMutableData *postBody = [NSMutableData data];
    	[postBody appendData:[[NSString stringWithString:@"firstname=\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:firstname] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"&lastname=\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:lastname] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"&email=\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:email] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"&msg=\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:msg] dataUsingEncoding:NSASCIIStringEncoding]];
    	[postBody appendData:[[NSString stringWithString:@"\""] dataUsingEncoding:NSASCIIStringEncoding]];
    	[request setHTTPBody:postBody];
    	
    	NSString *s = [[NSString alloc]initWithData:postBody encoding:NSASCIIStringEncoding];
    	NSLog(@"print from postBody:\n%@",s);
    	[s release];
    	
    	NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    	
    	if (!conn) {
    		NSLog(@"Failed to submit request");
    	} else {
    		NSLog(@"Request submitted");
    		//receivedDatas = [[NSMutableData alloc] init];
    	}
    	//[request release];
    	[conn release];
    }
    
    
    
    PLEASE - could someone give me some feedback on this? What can I do to detect the error in my code? As I wrote in the post above - the php-script works fine and the values are taken from the fields in the app and the string is correctly created. hmmmm :confused:

    MACloop
     

Share This Page