0

I can get the device token for my iPad but the problem how to send it to the database in server via php code .

I had test the php code and it is working .

this my function to send the device token :

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

NSString *devToken = [[[[deviceToken description] 
                        stringByReplacingOccurrencesOfString:@"<"withString:@""] 
                       stringByReplacingOccurrencesOfString:@">" withString:@""] 
                      stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"%@",devToken);
NSString *sli = [NSString stringWithFormat:@"http://localhost:8888/insertiphone.php?number=%@",devToken];
NSURL *url = [NSURL URLWithString:sli];

NSData *test = [NSData dataWithContentsOfURL:url];
if ([test length] == 0) {
    NSLog(@"no done");
}

}

it is not working ..

and this is the php code :

<?php
function inserttodatabase(){
$con = mysql_connect("localhost","root","root");
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO iphoneid (idip)
VALUES ($_GET[number])");
echo "done";
}

inserttodatabase(); 
?>

updated , I tried also with these codes but not working

 NSString *devToken = [[[[deviceToken description] 
                        stringByReplacingOccurrencesOfString:@"<"withString:@""] 
                       stringByReplacingOccurrencesOfString:@">" withString:@""] 
                      stringByReplacingOccurrencesOfString: @" " withString: @""];

NSString *sli = [NSString stringWithFormat:@"http://localhost:8888/insertiphone.php?      number=%@",devToken];
NSLog(@"%@",sli);
NSURL *url = [NSURL URLWithString:sli];

NSURLRequest *urlr = [NSURLRequest requestWithURL:url     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:urlr     delegate:self];

if (connection){
    NSLog(@"connecting");
}else {
    NSLog(@"error");
}

it says connecting but nothing will be inserted into the database

Ali Alzahrani
  • 529
  • 1
  • 6
  • 29

2 Answers2

0

check for nil (dataWithContentsOfUrl can return nil on an issue)

You will get better error information and control using NSURLConnection (connection:didFailWithError:) or ASIHttpRequest (requestFailed:) to send the request.

rest web services in iphone

http://allseeing-i.com/ASIHTTPRequest/How-to-use

Also, does the server code execute when making the request from the iPhone client? Either debug or trace the server code to get better insight into what's going on.

You can also setup something like charles proxy to see what's going over the wire and what's coming back.

Here's a blog post debugging connection issues with charles proxy. http://blog.mediarain.com/2009/08/iphone-http-connection-debugging/

Finally, as a one off try [machinename].local:8888... in the event that you're having name resolution, localhost resolution issues, etc... issues.

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • I do not know how to use these classes ! any examples please – Ali Alzahrani Jul 10 '12 at 00:03
  • added links on using those as well as link to using charles proxy to inspect the wire. hope those help narrow down your issues ... – bryanmac Jul 10 '12 at 00:15
  • sorry but I think I have another problem which is the GET not receiving more than 4 character ! – Ali Alzahrani Jul 10 '12 at 00:57
  • You need to look at whether the full id is going across the wire with charles proxy. Also, can you tell whether GET is not getting > 4 chars or whether the DB is truncating?? You have the insert and the GET all on one line. Read the id on a separate line check the column definition to ensure it's not truncating ... – bryanmac Jul 10 '12 at 01:16
  • sorry , but I update the question with new code .. please any help – Ali Alzahrani Jul 10 '12 at 07:47
0

First, provide more information on what the service code is doing and how you are testing it. The code you have written is issuing an HTTP GET request rather than a put or a post. In most cases, a web service that inserts records into your database (as evidenced by the "insertiphone.php" in the URL) would be implemented using an HTTP POST rather than a GET. If you send it using a GET, as you are doing, it would not perform as expected. So providing more information about how you know the server is working and what you did to test it would help diagnose you problem.

Assuming that you have checked the service and verified it is expecting an HTTP GET (not good RESTful design if a data is being updated on the service, but certainly possible) then I would concur with the answer provided by @bryanmac. Look into his links to learn about better ways to make HTTP requests, checking the server side to see if the request is coming through, using CharlesProxy to debug, etc.

One other debugging tip I would offer if your service is expecting a GET and you can test it with a web browser, try it from Safari with developer extensions enabled and use it to monitor the resource request and response. That will allow you to see what a successful request looks like, which can be compared to the actual request you are sending that you can see by using CharlesProxy.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • sorry but I think I have another problem which is the GET not receiving more than 4 character ! – Ali Alzahrani Jul 10 '12 at 00:59
  • sorry , but I update the question with new code .. please any help – Ali Alzahrani Jul 10 '12 at 13:28
  • You've still not provided any information that tells me if your service is expecting a GET or a POST. You also stated that tested the php code and it is working, but you have not provided information on what you did to test that PHP code and how you know it is working. – Tim Dean Jul 10 '12 at 14:50
  • I am using GET and it is working when I test it from Safari , in other words I want to do like this tutorial http://www.youtube.com/watch?v=LwCyaGoRNWM – Ali Alzahrani Jul 10 '12 at 14:59