Web service call for iOS development

Source: Internet
Author: User

The following changes are declared in the. h file:

Nsmutabledata * webdata;
Nsmutablestring * soapreply;
Nsurlconnection * conn;

Nsxmlparser * xmlparser;
Bool elementfound;

The following are implemented in. M.

// Validate user ID and password by Web Service
-(Void) loginvalidate {

// [Self loginvalidate];
Myimeicode = @ "03 600274 150967 3 ";
Accesskey = @ "null ";

Nsstring * tstimei = myimeicode;
Nsstring * userstring = @ "null ";

If ([userid. Text isequaltostring: @ "iosappsvisitor"]) {
Userstring = @ "domain \ General ";
}
Else {
Userstring = userid. text;
}

Nsarray * userarray = [userstring componentsseparatedbystring: @ "\"];
Nsstring * useridonly = [userarray objectatindex: 1];
Nsstring * userdomain = [userarray objectatindex: 0];

Nsstring * newpassword = [[nsstring alloc] init];

Newpassword = [self encryptpassword];
Nslog (newpassword );
Nsstring * soapmsg = [nsstring stringwithformat:
@ "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "
"<Soap: envelope xmlns: xsi = \" external "xmlns: XSD = \" http://www.w3.org/2001/xmlschema\ "xmlns: Soap = \" external ">"
"<Soap: Body>"
"<Zapps_ins_login_tx xmlns = \" http://cbi.crystalgroup.com.hk/\ ">"
"<P_suserid >%@ </p_suserid>"
"<P_suserpassword >%@ </p_suserpassword>"
"<P_simeicode >%@ </p_simeicode>"
"<P_sdomain >%@ </p_sdomain>"
"</Zapps_ins_login_tx>"
"</Soap: Body>"
"</Soap: envelope>", useridonly, newpassword, tstimei, userdomain];

Nsstring * wsurl = [[nsstring alloc] init];
Wsurl = @ "http://tst.tst.com/wsAppsiDevice/Service.asmx"; // testing URL

Nsmutableurlrequest * Req = [nsmutableurlrequest requestwithurl: [nsurl urlwithstring: wsurl];
[Req addvalue: @ "text/XML; charset = UTF-8" forhttpheaderfield: @ "Content-Type"];
[Req addvalue: @ "http://cbi.crystalgroup.com.hk/zApps_Ins_Login_Tx" forhttpheaderfield: @ "soapaction"];
[Req addvalue: [nsstring stringwithformat: @ "% d", [soapmsg length] forhttpheaderfield: @ "Content-Length"];
[Req sethttpmethod: @ "Post"];
[Req sethttpbody: [soapmsg datausingencoding: nsutf8stringencoding];
Conn = [[nsurlconnection alloc] initwithrequest: req delegate: Self];
If (conn ){
Webdata = [[nsmutabledata data] retain];
}

}

-(Void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) response {
[Webdata setlength: 0];
}

-(Void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {
[Webdata appenddata: Data];
}

-(Void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) error {

// Nsstring * msgresult = [error description];
[Webdata release];
[Connection release];

}

-(Bool) connection :( nsurlconnection *) connection canauthenticateagainstprotectionspace :( nsurlprotectionspace *) protectionspace {

Return [protectionspace. authenticationmethod isinclutostring: nsurlauthenticationmethodservertrust];

}

-(Void) connection :( nsurlconnection *) connection didreceiveauthenticationchallenge :( nsurlauthenticationchallenge *) challenge {

[Challenge. Sender usecredential: [nsurlcredential credentialfortrust: Challenge. protectionspace. servertrust] forauthenticationchallenge: Challenge];
[Challenge. Sender continuewithoutcredentialforauthenticationchallenge: Challenge];

}

-(Void) connectiondidfinishloading :( nsurlconnection *) connection {
If (xmlparser ){
[Xmlparser release];
}
Xmlparser = [[nsxmlparser alloc] initwithdata: webdata];
[Xmlparser setdelegate: Self];

[Xmlparser setshouldresolveexternalentities: Yes];
[Xmlparser parse];

If ([accesskey isw.tostring: @ "null"])
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "warning"
Message: @ "invalid user ID/password, please try again! "
Delegate: Nil cancelbuttontitle: @ "OK"
Otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
Else {

// Write data to plist
// Begin
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Nsstring * filepath = [documentsdirectory stringbyappendingstring: @ "/data. plist"];

//
Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
Nsmutabledictionary * plistdict;
If ([filemanager fileexistsatpath: filepath])
{
Plistdict = [[nsmutabledictionary alloc] initwithcontentsoffile: filepath];
} Else {
Plistdict = [[nsmutabledictionary alloc] init];
}

Nsstring * userstring = @ "null ";

If ([userid. Text isequaltostring: @ "iosappsvisitor"]) {
Userstring = @ "HK \ General ";
}
Else {
Userstring = userid. text;
}

Nsarray * userarray = [userstring componentsseparatedbystring: @ "\"];
Nsstring * useridonly = [userarray objectatindex: 1];

[Plistdict setvalue: useridonly forkey: @ "userid"];
[Plistdict setvalue: userpassword. Text forkey: @ "userpwd"];
[Plistdict setvalue: accesskey forkey: @ "useraccesskey"];
[Plistdict setvalue: myimeicode forkey: @ "userimei"];

If ([plistdict writetofile: filepath atomically: Yes]) {
Nslog (@ "writeplist success ");
} Else {
Nslog (@ "writeplist fail ");
}

[Plistdict release];
// End

}


[Connection release];
[Webdata release];
}

-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri
Qualifiedname :( nsstring *) QNAME attributes :( nsdictionary *) attributedict {
// If ([elementname isinclutostring: @ "getquoteresult"]) {
// If ([elementname isinclutostring: @ "zins_login_txresult"]) {
If ([elementname isequaltostring: @ "zapps_ins_login_txresult"]) {
If (! Soapreply ){
Soapreply = [[nsmutablestring alloc] init];
}
Elementfound = yes;
}
}

-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) string {
If (elementfound ){
[Soapreply appendstring: String];
}
}

-(Void) Parser: (nsxmlparser *) parser didendelement :( nsstring *) elementname
Namespaceuri :( nsstring *) namespaceuri
Qualifiedname :( nsstring *) QNAME {
// If ([elementname isinclutostring: @ "getquoteresult"]) {
// If ([elementname isinclutostring: @ "zins_login_txresult"]) {
If ([elementname isequaltostring: @ "zapps_ins_login_txresult"]) {

// If (! Soapreply ){
// Soapreply = [[nsmutablestring alloc] init];
//}

Nsstring * accesskey1 = [soapreply description];
// [Soapreply setstring: @ ""];
Elementfound = no;

Accesskey = accesskey1;

If ([accesskey1 isw.tostring: @ "null"]) {
Accesskey = @ "null ";
}
Else if (accesskey1! = Nil)
{
/*
Uialertview * alertok = [[uialertview alloc] initwithtitle: @ "Validate"
Message: accesskey1
Delegate: Nil cancelbuttontitle: @ "OK"
Otherbuttontitles: Nil];
[Alertok show];
[Alertok release];
*/

Ipad_demo2appdelegate * appdelegate = (ipad_demo2appdelegate *) [[uiapplication sharedapplication] Delegate];
[Self. View removefromsuperview];

// [Self. navigationcontroller poptorootviewcontrolleranimated: Yes];

Appdelegate. mykey = accesskey;
// [Appdelegate. Window addsubview: appdelegate. topnewsviewcontroller. View];
// Splitviewcontroller
[Appdelegate. Window addsubview: appdelegate. splitviewcontroller. View];

}
Else {
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "Validate"
Message: @ "invalid user ID or password, please try again ."
Delegate: Nil cancelbuttontitle: @ "OK"
Otherbuttontitles: Nil];
[Alert show];
[Alert release];
}

// [Soapreply setstring: @ ""];
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.