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

时间:2010-06-24 05:26:53

标签: iphone objective-c linq-to-objects

我正试图从我的iphone应用程序发送一个单词/文本到php页面,任何想法?

2 个答案:

答案 0 :(得分:1)

您可以使用异步调用来执行此操作。

// 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];

答案 1 :(得分:0)

您只需使用NSURLConnection加载包含单词/ text作为参数的网址即可。有关使用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/thepage.php?word=@%", 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.
}