1

我正在将一些数据从我的应用程序发送到服务器。我的数据由不同的字段组成,如下所示我的代码

-(void)createXML
{
xmlStr = @"<?xml version='1.0'?>\n<jand_req>\n<inquiryList>\n<productArr>\n";
NSString *nameStr=[NSString stringWithFormat:@"<name>%@</name>\n",name.text];
xmlStr=[xmlStr stringByAppendingString:nameStr];
NSString *compNameStr=[NSString stringWithFormat:@"<comp_name>%@</comp_name>\n",compName.text];
xmlStr=[xmlStr stringByAppendingString:compNameStr];
NSString *cityStr=[NSString stringWithFormat:@"<city>%@</city>\n",city.text];
xmlStr=[xmlStr stringByAppendingString:cityStr];
NSString *countryStr=[NSString stringWithFormat:@"<country>%@</country>\n",[nameToCode objectForKey:country.text]];
xmlStr=[xmlStr stringByAppendingString:countryStr];
NSString *commentsStr=[NSString stringWithFormat:@"<comment>%@</comment>\n",commentsBox.text];
xmlStr=[xmlStr stringByAppendingString:commentsStr];
xmlStr=[xmlStr stringByAppendingString:@"</userDetail>\n</inquiryList>\n</jand_req>"];
}

在此之后,我将上述数据发送到服务器,如下所示

- (void)submitForm
 {
[self createXML];

NSLog(@"myaccesscode%@",[fn getValFromSettings:@"accessCode"]);
   NSString *serviceUrlStr=[NSString stringWithFormat:@"%@/%@/API_sendmail.php?access_code=%@",domainName,apiFolderPath,[fn getValFromSettings:@"accessCode"]];
    NSLog(@"%@",serviceUrlStr);
    NSURL * serviceUrl = [NSURL URLWithString:[serviceUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:100];
    [serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
    [serviceRequest setHTTPMethod:@"POST"];
    [serviceRequest setHTTPBody:[xmlStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *conn=[[[NSURLConnection alloc] initWithRequest:serviceRequest delegate:self startImmediately:YES] autorelease];
}

以上所有代码对我来说都很好,但现在我想在此代码中包含另一个功能,其中包含发件人电子邮件地址,但此发件人电子邮件必须从设备中获取,与我们在应用程序中使用 MFMailComposeViewController 时相同,然后自动获取从设备获取发件人电子邮件地址。任何帮助将不胜感激。

4

1 回答 1

0
#import <AddressBook/AddressBook.h>

NSString *emailAddr = @"";
ABPerson *aPerson = [[ABAddressBook sharedAddressBook] me];
ABMultiValue *emails = [aPerson valueForProperty:kABEmailProperty];
if([emails count] > 0)
    emailAddr = [emails valueAtIndex:0];

不要忘记添加 AddressBook.framework。

现在,emailAddr 包含发件人电子邮件,您可以将其附加到您的 xml 字符串或任何您想要的位置!

-(void)createXML
{
    // Your code
    NSString *emailStr=[NSString stringWithFormat:@"<email>%@</email>\n",emailAddr];
    xmlStr=[xmlStr stringByAppendingString:emailStr];
}

快乐编码!

于 2012-12-31T05:18:24.010 回答