1

我得到下面的代码来调用 PHP 脚本,由于某种原因它说连接成功,但实际上没有调用我的脚本,因为我没有收到电子邮件:-(

没有调用任何 NSURLConnectionDelegate 方法。

在 iPhone 5.x 设备和模拟器上进行测试。

有什么建议/想法吗?非常感谢你!

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

NSLog(@"web service URL: %@", url);

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
    NSLog(@"Connection succeeded");
} else {
    NSLog(@"Connection failed");
}
4

3 回答 3

2
NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

NSLog(@"web service URL: %@", url);

//add the following or something like
[url setString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

//modify the following    
connection = [[NSURLConnection alloc] initWithRequest: request delegate:self startImmediately:YES]; 
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
    NSLog(@"Connection succeeded");
} else {
    NSLog(@"Connection failed");
}
于 2012-07-12T22:02:46.243 回答
0

我敢打赌,您的电子邮件正文中有一些内容不能安全地通过 url 发送

NSString* url = [NSString stringWithFormat:@"http://www.lenniedevilliers.net/mail.php?subject=%@&to=%@&body=%@", subjectLine, toAddress, emailBody ];

所以说您的电子邮件正文包含&,这会破坏您拥有的代码。您要么需要转义消息正文,要么通过请求正文发送它,而不是通过 URL 传递它。我可能是错的,但我敢打赌就是这样。你的代码看起来不错,我敢打赌它的数据;)

于 2012-07-12T17:39:27.030 回答
0

您是否使用 NSURLConnection 的特定委托函数?

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
    responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //NSLog(@"Did Receive Data %@", data);
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Did Fail");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Did Finish");
    // Do something with responseData
}
于 2012-07-12T21:39:46.767 回答