码迷,mamicode.com
首页 > 移动开发 > 详细

iOS混合编程之使用Swift+Objective-C调用WebService

时间:2015-08-07 16:15:05      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:objective-c   swift   ios   混合编程   

       最近由于项目的需要,要使用Swift语言开发个iOS项目,某个简单的需求就是要调用远程的WebService数据。问题出现了,网上有很多使用OC调用WebService的例子,但是几乎没有找到使用Swift调用WebService的例子,我苦苦搜索了好几个小时,就是没有满足我要求的,怎么办,项目要泡汤了?Swift和OC,我该怎么选择?

      天无绝人之路,还好Swift和OC可以完美的实现混合编程,可以相互实现调用。然后又在网上找到一个使用OC访问WebService的例子http://my.oschina.net/plumsoft/blog/75277,也非常感谢这位博主。好吧,我来写一个基于Swift和OC混合编程调用WebService的例子吧。最下面可以下载我的源代码哦。

(1)新建一个Swift项目,在Main.storyboard中设计界面如下,分别绑定号码输入框和查询按钮到ViewControlle.swift中。

技术分享


注意以下代码是Swift:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var phoneNumber: UITextField!//输入手机号码的文本框;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //查询按钮的响应;
    @IBAction func beginQuery(sender: AnyObject) {
        
    }
    
}

(2)右键项目New Files.选择新建一个Cocoa Touch Class,命名为OCWebService,然后会弹出一个是否建立一个Bridging-Header.h文件,点击Yes即可。注意非常重要:需要在Bridging-Header.h桥接头文件中import如这个OC的头文件!!

技术分享


技术分享


技术分享


(3)然后在OCWebService.h中声明如下属性和方法,OCWebService.h具体代码如下:注意这个代码是OC。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

//需要有两个Delegate,一个是解析XML用的,一个是网络连接用的;
@interface OCWebService : NSObject<NSXMLParserDelegate, NSURLConnectionDelegate>

@property (strong, nonatomic) NSMutableData *webData;
@property (strong, nonatomic) NSMutableString *soapResults;
@property (strong, nonatomic) NSXMLParser *xmlParser;
@property (nonatomic) BOOL elementFound;
@property (strong, nonatomic) NSString *matchingElement;
@property (strong, nonatomic) NSURLConnection *conn;

-(void)query:(NSString*)phoneNumber;//在头文件中声明查询方法;

@end

(4)OCWebService.m中具体的方法实现如下:注意这个代码是OC。

#import "OCWebService.h"

@implementation OCWebService

@synthesize webData;
@synthesize soapResults;
@synthesize xmlParser;
@synthesize elementFound;
@synthesize matchingElement;
@synthesize conn;

-(void)query:(NSString*)phoneNumber{
    
    // 设置我们之后解析XML时用的关键字,与响应报文中Body标签之间的getMobileCodeInfoResult标签对应
    matchingElement = @"getMobileCodeInfoResult";
    // 创建SOAP消息,内容格式就是网站上提示的请求报文的主体实体部分    这里使用了SOAP1.2;
    NSString *soapMsg = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap12:Envelope "
                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                         "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
                         "<soap12:Body>"
                         "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
                         "<mobileCode>%@</mobileCode>"
                         "<userID>%@</userID>"
                         "</getMobileCodeInfo>"
                         "</soap12:Body>"
                         "</soap12:Envelope>", phoneNumber, @""];
    
    // 将这个XML字符串打印出来
    NSLog(@"%@", soapMsg);
    // 创建URL,内容是前面的请求报文报文中第二行主机地址加上第一行URL字段
    NSURL *url = [NSURL URLWithString: @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
    // 根据上面的URL创建一个请求
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMsg length]];
    // 添加请求的详细信息,与请求报文前半部分的各字段对应
    [req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    // 设置请求行方法为POST,与请求报文第一行对应
    [req setHTTPMethod:@"POST"];
    // 将SOAP消息加到请求中
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
    // 创建连接
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) {
        webData = [NSMutableData data];
    }
    
}

// 刚开始接受响应时调用
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{
    [webData setLength: 0];
}

// 每接收到一部分数据就追加到webData中
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data {
    [webData appendData:data];
}

// 出现错误时
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error {
    conn = nil;
    webData = nil;
}

// 完成接收数据时调用
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
                                                length:[webData length]
                                              encoding:NSUTF8StringEncoding];
    
    // 打印出得到的XML
    NSLog(@"%@", theXML);
    // 使用NSXMLParser解析出我们想要的结果
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];
    
}

#pragma mark -
#pragma mark XML Parser Delegate Methods

// 开始解析一个元素名
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
    if ([elementName isEqualToString:matchingElement]) {
        if (!soapResults) {
            soapResults = [[NSMutableString alloc] init];
        }
        elementFound = YES;
    }
}

// 追加找到的元素值,一个元素值可能要分几次追加
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
    if (elementFound) {
        [soapResults appendString: string];
    }
    
}

// 结束解析这个元素名
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:matchingElement]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"手机号码信息"
                                                        message:[NSString stringWithFormat:@"%@", soapResults]
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        elementFound = FALSE;
        // 强制放弃解析
        [xmlParser abortParsing];
    }
}

// 解析整个文件结束后
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    if (soapResults) {
        soapResults = nil;
    }
}

// 出错时,例如强制结束解析
- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    if (soapResults) {
        soapResults = nil;
    }
}


@end

(5)最后再来到ViewController.swift的项目主文件使用Swift调用上述用OC写的方法:主要实现是在beginQuery()方法中。注意这个是Swift代码

    //查询按钮的响应;
    @IBAction func beginQuery(sender: AnyObject) {
        
        var webservice = OCWebService()//主要在OCWebService这个OC写的类中实现对WebService的调用;

        webservice.query(phoneNumber.text)//调用query方法开始访问WebService,传递的参数是电话号码;
    }


(6)运行程序,查看效果。

技术分享


技术分享


      该项目Demo下载地址:http://pan.baidu.com/s/1hqgwnJI    。以后我再也不纠结选择Swift还是OC了。

        



版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS混合编程之使用Swift+Objective-C调用WebService

标签:objective-c   swift   ios   混合编程   

原文地址:http://blog.csdn.net/chenyufeng1991/article/details/47338827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!