如何在iPhone的服务器上使用登录令牌上传图像

时间:2014-03-27 09:32:17

标签: ios iphone objective-c web-services parsing

在我的应用程序中,我在图像视图中获取图像,然后单击aButton,它转到LoginViewController,用户必须填写其用户名和密码。我获得了登录令牌,有一些参数和条件如:

Method: PMac_LogIn  
Parameter: mailaddress String with @   
password String     
Return: If ok, then you receive a loginToken. (> 0)   
If not ok, then loginToken < 0   
-1 = user not found  
-2 = wrong password   
When you can not reach the server, you have to inform the user in dialog, with “Server not available”. In the cases -1 or -2 you should inform the user.

我使用了WSDL网络服务,并且不知道如何登录但已经访问过网络服务。条件代码是什么。

这是我的代码:

- (void)connectionDidFinishLoading:(NSURLConnection *)连接 {     NSLog(@&#34;完成。收到字节:%d&#34;,[webData长度]);

NSString *theXML1 = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding] ;
NSLog(@"theXML: \n%@",theXML1);
[theXML1 release];

if( xmlParser )
{
    [xmlParser release];
}

xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];

[connection release];
[webData release];

UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"User Not Exist" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];

}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
    if( [elementName isEqualToString:@"HelloResult"])
    {
        if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        recordResults = TRUE;
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if( recordResults )
    {
        [soapResults appendString: string];
    }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if( [elementName isEqualToString:@"HelloResult"])
    {
        recordResults = FALSE;

        [soapResults release];
        soapResults = nil;
    }
}

2 个答案:

答案 0 :(得分:0)

//add jsonkit to project 
//top of your file
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
//your login url
#define kloginJSONURL [NSURL URLWithString:@"http://www.mysite.com/login_script.php?"] //2 LOGIN LINK

//supose you have 2 text fields named UserName and UserPassword
NSString *stringLOGINquery = [NSString stringWithFormat:@"%@username=%@&parola=%@",kloginJSONURL, UserName.text, UserPassword.text];
NSURL *COMBINAT = [[NSURL alloc] initWithString:[stringLOGINquery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
 //add this
//async queue with selector
dispatch_async(kBgQueue, ^{
     NSData* data = [NSData dataWithContentsOfURL:COMBINAT];
     [self performSelectorOnMainThread:@selector(fetchedData:)
     withObject:data waitUntilDone:YES];
          NSLog(@"obiect date login%@", data);
});

//now your selector
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
//check if json exists then suppose your json returns a Login key:
if(json) {
NSString *loginToken  =[json objectForKey:@"Login"];
if ([loginToken isEqualToString:@""]) {
//your prompt -> token fail
} else if ([loginToken isEqualToString:@"-1"] || [loginToken isEqualToString:@"-2"] ) {
// fail
} else if ([loginToken isEqualToString:@"Succes"]  {    
// is ok
//here you can implement if length of loginToken > 0 but keep in mind it must be != -1 -2 so write something like this
int LenghtloginToken = [loginToken length]; 
if(LenghtloginToken >0 && ! loginToken isEqualToString:@"-1"] || ! [loginToken isEqualToString:@"-2"]) {
//success
}

关于发送到其他视图的问题 - &gt; 我想你必须开始阅读一些教程。这是一个微不足道的问题。

答案 1 :(得分:0)

int LenghtloginToken = [elementName length];
if(LenghtloginToken >0 && (! [elementName isEqualToString:@"-1"] || ! [elementName isEqualToString:@"-2"])) {
 //success
 NSLog(@"success%@", elementName);
}

请记住,开始阅读有关Objective C中基本内容的一些好教程。