iphone textview contents to url? - iphone

How can i have a uitextview value be taken and send to a predefined url using obj-c only without any html /php.

Take a look at NSURL. Use the text value of the UITextView as a parameter.
e.g.
NSURL *webURL = [NSURL URLWithString:MY_TEXTVIEW.text];
NSURLRequest *req = [NSURLRequest requestWithURL:webURL];

NSString *methodString = #"method";
NSString *parameterString = [NSString stringWithFormat:#"value=%#", textField.text];
NSString *apiRoot = #"http://www.domain.com";
NSURL *apiUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#?%#", apiRoot,methodString,parameterString]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:apiUrl];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Related

json return to the format

Api Address:
http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone
return:
KISSY.Suggest.callback({"result": [["iphone4s", "9809"], ["iphone5", "13312"], ["iphone4 手机", "69494400"], ["iphone5 港行", "14267"], ["iphone5三网", "2271160"], ["iphone4手机壳", "6199679"], ["iphone 5手机壳", "2527284"], ["iphone 5 保护壳", "5727586"], ["iphone 4贴膜", "147271"], ["iphone5壳", "2628540"]]})
NSURL * url = [NSURL URLWithString:#"http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSHTTPURLResponse* urlResponse = nil;
NSError * error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSData *date = [NSData alloc]init
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
// NSMutableArray *array=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSMutableArray *array = [jsonParser objectWithData:responseData];
NSLog(#"%#",array);
this array is null. i dont know the reason.
as i refer you request URL ,it has callback in it, if you keep it, it will not return you json as response, so remove "&callback=KISSY.Suggest.callback" from your URL
// Make sure you have include SBJSON files in your Project, as well you have imported header in your View Controller
#import "JSON.h"
// your request URL
NSURL * url = [NSURL URLWithString:#"http://suggest.taobao.com/sug?area=etao&code=utf-8&q=iphone"];
// URL Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSHTTPURLResponse* urlResponse = nil;
NSError * error = nil;
// initiate Request to get Data
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
// Encode your Response
NSString *content = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
// Now read a Dictionary from it using SBJSON Parser
NSDictionary *responseDict = [content JSONValue];
NSLog(#"Response [%#]",responseDict);
I'm not familiar with the SBJsonParser, but the format of the returned string looks like JSONP, not JSON. I would imagine simply cleaning out the wrapper call would get you what you are after.
Also, note that the 'root' of your response is a dictionary, not an array.
{"result": [[...
means that the code might should look like this:
NSDictionary *response = //... decode
NSArray *results = [response objectForKey:#"result"];
Edited
You just need to use http://suggest.taobao.com/sug?area=etao&code=utf-8&q=iphone instead of http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone you own code will work..

Sending Data from UITextField to PHP script via NSURLConnection or JSon

Hi I would like to know how I can go about sending data from 4 UITextField's to a PHP script on a website using NSURLConnection or JSon. Whatever is easier, how can I do this exactly?
Thanks in advance!
Easiest way (synchronous)
NSString strForTextField1 = textField1.text;
NSString strForTextField2 = textField1.text;
NSString strForTextField3 = textField1.text;
NSString strForTextField4 = textField1.text;
//Build the request
NSString *stringOfRequest = [NSString stringWithFormat:#"www.yourserver.com?var1=%#&var2=%#&var3=%#&var4=%#", strForTextField1, strForTextField2, strForTextField3, strForTextField4];
NSURL *url = [NSURL URLWithString:stringOfRequest];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(!error)
{
//log response
NSLog(#"Response from server = %#", responseString);
}

iPhone SDK - Google TTS and encoding

I am developing a text-to-speech iphone app that support multi-languages.
Here is my request URL
requestUrlStr = #"http://www.translate.google.com/translate_tts?tl=en&q=hello";
for english the above url has no problem
but for Chinese
requestUrlStr = #"http://www.translate.google.com/translate_tts?tl=zh-TW&q=你好";
I know the above url will give 'Bad URL', so I used follow method to encode the string into UTF-8
requestUrlStr = [requestUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
It will become
http://www.translate.google.com/translate_tts?tl=zh-CN&q=%E4%BD%A0%E5%A5%BD
Then the Google TTS cannot recognize this Chinese text.
You have to pretend to be a User-Agent other than the default (appName, etc) in your NSURLRequest. Try this (I use Greek language) ...
NSString* userAgent = #"Mozilla/5.0";
NSURL *url = [NSURL URLWithString:[#"http://www.translate.google.com/translate_tts?tl=el&q=Καλημέρα"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setValue:userAgent forHTTPHeaderField:#"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
[data writeToFile:#"/var/tmp/tts.mp3" atomically:YES];
UPDATE 2017
Since our favorite companies enjoy to update and deprecate things, here is the above example as it should be now...
NSString* text = #"καλημέρα";
NSString* lang = #"el";
NSString* sUrl = [NSString stringWithFormat:#"https://translate.google.com/translate_tts?q=%#&tl=%#&client=tw-ob", text, lang];
sUrl = [sUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:sUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"Mozilla/5.0" forHTTPHeaderField:#"User-Agent"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[data writeToFile:#"/var/tmp/tts.mp3" atomically:YES];
}
] resume];
The ...delegate:nil delegateQueue:[NSOperationQueue mainQueue] can be omitted.

Google Reader Token Request giving 403 error

I am trying to get the token from google reader for my iPhone project. I am able to get the Authorization but when I request for the token, I get a 403 Forbidden
Below is my code implementation. Any help would be appreciated.
//The tokenStorer contains the Authorization key
NSString *tokenStorer = [[NSUserDefaults standardUserDefaults]valueForKey:#"authKey"];
NSLog(#"%#", tokenStorer);
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"www.google.com", #"Host",
#"iReader", #"User-Agent",
#"gzip, deflate", #"Accept-Encoding",
tokenStorer, #"Authorization",
nil
];
//#"Auth", NSHTTPCookieName, tokenStorer, NSHTTPCookieValue, #"./google.com", NSHTTPCookieDomain, #"/", NSHTTPCookiePath, nil];
//NSHTTPCookie *authCookie = [NSHTTPCookie cookieWithProperties:cookieDictionary];
//Google token url "http://www.google.com/reader/api/0/token?client=clientName"
NSURL *url=[[NSURL alloc] initWithString:GOOGLE_READER_TOKEN_URL];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[urlRequest setHTTPMethod:#"GET"];
[urlRequest setAllHTTPHeaderFields:cookieDictionary];
NSData *reciveData;
NSURLResponse *response;
NSError *error;
reciveData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSMutableURLRequest *tokenRequest= [[NSMutableURLRequest alloc] initWithURL:url];
NSString *trial = [[NSString alloc]initWithData:reciveData encoding:NSASCIIStringEncoding];
NSLog(#"%# %d",trial, response);
[url release];
The below code solved my problem:
-(id)queryLoginDetails {
//authKey returns the authorization key
NSString *tokenStorer = [[NSUserDefaults standardUserDefaults]valueForKey:#"authKey"];
NSLog(#"%#", tokenStorer);
NSString *auth = [[NSString alloc] initWithString:
[NSString stringWithFormat:#"GoogleLogin auth=%#", [tokenStorer substringToIndex:[tokenStorer length]-1]]];
NSDictionary *createHeader = [[NSDictionary dictionaryWithObjectsAndKeys:#"www.google.com", #"Host", #"iReader", #"User-Agent", #"gzip, deflate", #"Accept-Encoding", auth, #"Authorization", nil]retain];
NSURL *url =[NSURL URLWithString:GOOGLE_READER_TOKEN_URL];
NSData *recieveData;
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[urlRequest setHTTPMethod:#"GET"];
[urlRequest setAllHTTPHeaderFields:createHeader];
NSURLResponse *response;
NSError *error;
recieveData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSString *myString = [[NSString alloc]initWithData:recieveData encoding:NSASCIIStringEncoding];
return myString;
}

Iphone Caching Problem

when I call
-(IBAction)goback:(id)sender
{
NSURL *xmlURL=[NSURL URLWithString:#"http://demo.komexa.com/sicherungsbereich.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2];
NSURLResponse *theResponse;
NSError *theError;
NSData *myRequestResult = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSString *stringReply = (NSString *)[[NSString alloc] initWithData:myRequestResult encoding:NSUTF8StringEncoding];
NSLog(#"reply from server: %#", stringReply);
}
with the Iphone , on the simulator it loads the String everytime really from the internet.But on the devices, it caches the String, so even if the content of http://demo.komexa.com/sicherungsbereich.xml changes (you can do that by calling http://demo.komexa.com) the String does not automatically reload new data.
Have you got an Idea?
I have uploaded the Code here,because of formatting problems:
http://demo.komexa.com/problem.txt
just generate a random number and pass with your url , you get required result
e.g:
int randomNumber1 = 1 + arc4random()% 9;
NSString *rstr = [NSString stringWithFormat: #"%d",randomNumber1];
NSString *address = #"http://www.socialfactory.net/client-app/photovote/get_data.php";
NSString * nstr=[NSString stringWithFormat:#"%#?t=%#",address,rstr];
//NSURLRequest *theRequest=nil;
NSURLRequest * theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:nstr]];
NSURLResponse *resp = nil;
NSError *err = nil;
Maybe your Simulator and iPhone have different proxie settings. check this or try with the NSURLRequestReloadIgnoringLocalAndRemoteCacheData policy which also ignores intermediate cachings. See the docs here.