I'm trying to send the device token and device name to my server, although it works with only the token, it crashes if i add the device name.
Is there a better way to do this?
(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
NSString* systemName=[[UIDevice currentDevice] name];
NSString *temp = [NSString stringWithFormat:systemName, deviceToken];
NSData *postData = [[NSString stringWithFormat:#"token=%#", temp] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://#############/push/register.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
the console outputs
2013-12-31 17:41:59.294 Wellington[31788:60b] My token is: <###### ###### ###### ###### ##### ###### ######>
(lldb)
NEW CODE
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
NSString* systemName=[[UIDevice currentDevice] name];
*** NSData *postData = [[NSString stringWithFormat:#"token=%#", deviceToken] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSData *postData2 = [[NSString stringWithFormat:#"token=%#", systemName] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://*********/register.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
[request setHTTPBody:postData2];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
it is crashing on stared line
Shouldn't you do something like,
NSString *temp = [NSString stringWithFormat:#"%#|%#", systemName, deviceToken];
Pipe delimetered values.
Reason your code isn't working...:
NSString *temp = [NSString stringWithFormat:systemName, deviceToken];
Will give you only systemName variables value. Because systemName variable string doesn't have %#symbol in it to the OS will not add deviceToken value into temp variable.
Related
I am trying to post some values to my web service which is working for android version nicely and for iOS it works for about 12 requests then it stops sending. What am i doing wrong?
I am sending about 1 per second.
-(void) PostDataToServer:(NSMutableArray*) value
{
NSString *xValue = [NSString stringWithFormat:#"%#", [value objectAtIndex:0]];
NSString *yValue = [NSString stringWithFormat:#"%#", [value objectAtIndex:1]];
NSString *jsonRequest = [NSString stringWithFormat:#"{\"person\":\"%#\",\"x\":\"%#\",\"y\":\"%#\",\"z\":\"%#\"}",#"1",xValue ,yValue ,#"0.0"];
NSLog(#"Request: %#", jsonRequest);
NSURL *url = [NSURL URLWithString:#"mywebserviceURL"];
request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
NSData *urlData= connection= [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(urlData);
I have an IOS application that I would like to send an email via Mandrill. I have tried to implement this, but its not working and Ive confused myself.
When pressing the button to send an email from the IOS application I log this error message:
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}
My code is:
NSString *post = [NSString stringWithFormat:#"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: me#mydomain.com\nTo: me#myotherdomain.com\nSubject: Some Subject\n\nSome content.}"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://mandrillapp.com/api/1.0/messages/send-raw.json"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSLog(#"Post: %#", post);
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(#"Reply: %#", theReply);
Please let me know where I am going wrong.
It looks you forgot the \" after "content.".
Try to write your "post" variable as follow:
NSString *post = [NSString stringWithFormat:#"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: me#mydomain.com\nTo: me#myotherdomain.com\nSubject: Some Subject\n\nSome content.\"}"];
I hope it helps.
NSString *clientsec = #"my client sec";
NSString *clientid = #"client id";
NSString *grant = #"urn:ietf:params:oauth:grant-type:migration:oauth1";
NSString *post =[[NSString alloc] initWithFormat:#"client_secret=%#&grant_type=%#&client_id=%#",clientsec,grant,clientid];
NSLog(#"%#",post);
NSURL *url=[NSURL URLWithString:#"https://accounts.google.com/o/oauth2/token"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",data);
i am posting the data like this but i am getting an error like this
"error" : "invalid_request"
can any one solve my problem ... Thanks in advance
This google API must contain 5 arguments.
code - The authorization code returned from the initial request
client_id - The client_id obtained during application registration
client_secret - The client secret obtained during application registration
redirect_uri - The URI registered with the application
grant_type - As defined in the OAuth 2.0 specification, this field must contain a value of authorization_code
Then your actual request might look like this
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
So you have to add code and redirect_uri parameters in your request.
I tried with this one, and worked:
NSString *urlString = #"https://accounts.google.com/o/oauth2/token";
NSURL *url=[NSURL URLWithString:urlString];
NSString *clientsec = #"my client sec";
NSString *clientid = #"client id";
NSString *grant = #"urn:ietf:params:oauth:grant-type:migration:oauth1";
NSString *post =[[NSString alloc] initWithFormat:#"client_secret=%#&grant_type=%#&client_id=%#",clientsec,grant,clientid];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%ld", [postData length]];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest* theRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[theRequest setTimeoutInterval:120.0f];
[theRequest setURL:url];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[theRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[theRequest setHTTPBody:postData];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSLog(#"%#",responseData);
Output:
client_secret=my client
sec&grant_type=urn:ietf:params:oauth:grant-type:migration:oauth1&client_id=client
id 2013-12-10 19:10:07.283 AkbarVenkatConflict[18969:8c03] <7b0a2020
22657272 6f722220 3a202269 6e76616c 69645f63 6c69656e 74222c0a
20202265 72726f72 5f646573 63726970 74696f6e 22203a20 22426164
20726571 75657374 2e220a7d>
I'm currently working on sending uiimages to my server to be stored on the server. So I get an image from the user gallery and then encode it using Base64 and then send it to my server. However all the images I sent to my server don't show. I'm wondering whether the post request I make with the base64encodedstring alters the encoded string.
Here is what I have done.
//Upload picture to server
NSData *imageData=UIImageJPEGRepresentation(showPic.image, 1.0f);
NSLog(#"The size of the image is %i",[imageData length]);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:imageData];
//NSLog(#"This is the encoded string to be uploaded %#",encodedPic);
prefs=[NSUserDefaults standardUserDefaults];
userid=[prefs stringForKey:#"userid"];
NSLog(#" %# is the userid to be used to upload the pic",userid);
NSString *poster = #"username=";
poster = [poster stringByAppendingString:userid];
poster = [poster stringByAppendingString:#"&image="];
poster = [poster stringByAppendingString:strEncoded];
NSString *post = poster;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://www.example.com/server/upload"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
I want to send a simple message(NSString) to my php script. I am trying to do a POST request. I am unable to do this and I am not sure what I am doing wrong. Can someone tell me my mistake?
Thank you!
PHP Code
<?php
if($_POST['error'])
{
$date= time();
$error=$_POST['error'];
$message=$date." ".$error;
echo $message;
//Rest of code just writes to a log file
?>
---------------------
NSString *post =[[NSString alloc] initWithFormat:#"error=%#",serverError];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://mysite.com/error.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"responseData is %#", responseData);
Try this
NSString *post = [NSString stringWithFormat:#"error=%#", serverError];
rather than
NSString *post = [[NSString alloc] initWithFormat:#"error=%#", serverError];