如何从iphone应用程序发送文字/文本到php文件

问题描述:

我想从我的iphone应用程序发送一个词/文本到php页面,任何想法?如何从iphone应用程序发送文字/文本到php文件

你可能会与异步调用做到这一点。

// Prepare the URL 
NSString myWord = @"YOUR_WORD"; 
NSString *urlString = [NSString stringWithFormat:@"http://<YOUR_DOMAIN>/<YOUR_FILE>.php?word=%@", myWord]; 
NSURL *url = [NSURL URLWithString:urlString]; 

// Get the data from the URL 
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url]; 
// If you want to get an answer from this call then send "self" as delegate 
// (and implement few NSURLConnectionDelegate methods), 
// otherwise send "nil". 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[connection release]; 
[request release]; 

你也可以发送同步字:

// Prepare the URL 
NSString myWord = @"YOUR_WORD"; 
NSString *urlString = [NSString stringWithFormat:@"http://<YOUR_DOMAIN>/<YOUR_FILE>.php?word=%@", myWord]; 

// Get the data from the URL 
NSError *error; 
NSData *aData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString] options:2 error:&error]; 
+0

OK EM想它会infrom你感谢迈克尔 – 2010-06-24 07:54:11

+0

感谢很多它的工作原理。 .... – 2010-06-24 08:32:46

您只需使用NSURLConnection加载包含word/text的URL作为参数。如需帮助,请按照使用NSURLConnection

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836

而且,一些示例代码:

// The word 
NSString wrd = [NSString stringWithString:@"hello"]; 

// Create the request. 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yourdomain.com/[email protected]%", wrd] 
        cachePolicy:NSURLRequestUseProtocolCachePolicy 
       timeoutInterval:60.0]; 
// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    receivedData = [[NSMutableData data] retain]; 
} else { 
    // Inform the user that the connection failed. 
} 
+0

由于阿赫亚EM尝试它....... – 2010-06-24 07:54:31