您的位置:首页 > Web前端

mac os 下安装ProtocolBuffer与iOS的简单使用

2016-12-19 20:21 393 查看
查看你的mac里面有没有装brew。brew是mac os里面,类似于ubuntu的apt-get的功能,都可以直接在终端输入命令然后安装程序。-v自然就是版本version的意思ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
这一句半懂不懂,大概就是利用curl工具访问那个url,然后在ruby环境下载安装brew

建议先去Homebrew官网找最新的下载地址
brew install automake

brew install libtool

brew install protobuf
就是利用brew下载安装了。protobuf就是我们想要的,另外两个是依赖库

git clone https://github.com/alexeyxo/protobuf-objc.gitz ./build.sh

从github下载protobuf-objc这个工程,build脚本里面做的是编译。

我建议不要用 ./build.sh ,我安装过程中发现未知错误最终没有进行下去。哎,好失败。懂脚本的朋友可以尝试下。

当我们 git clone https://github.com/alexeyxo/protobuf-objc.git 完成后,

cd ~/protobuf-objc

./autogen.sh

./configure

~/protobuf-objc其实就是刚刚clone的文件目录

进行./configure 可能会报错,不过别着急,先分析错误信息

configure: error: 

ERROR: protobuf headers are required.

You must either install protobuf from google,

or if you have it installed in a custom location

you must add '-Iincludedir' to CXXFLAGS

and '-Llibdir' to LDFLAGS.

If you did not specify a prefix when installing

protobuf, try

  './configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib'

In some 64-bit environments, try LDFLAGS=-L/usr/local/lib64.

仔细看,不难发现终端给出了解决办法,我想这应该是跟系统是不是64位有关吧(个人猜测)。

./configure CXXFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib

运行通过后,

make

make install

最终生成的插件名字为protoc-gen-objc,会被安装到/usr/local/bin/目录下。

你可以 

cd /usr/local/bin/ 

ls -a

按照我的方法,肯定能看见protoc-gen-objc。

一切准备就绪,我们来测试下。

在桌面创建一个 ProtoBuf的文件夹。然后
cd ~/Desktop/ProtoBuf

touch person.proto

vi person.proto

就按ProtocolBuffer的语法规则简单建立一个.proto的文件

创建proto文件

先在桌面创建一个文件夹,文件夹更名为ProtoBuf

实际工作中,proto文件都是后台提供给前端的,前端以此编译成.h和.m文件导入到工程中。

cd ~/Desktop/ProtoBuf

touch person.proto

vi person.proto

proto的文件内容示例:

package web.platform;

message login_account_request

{

required bytes account = 1;

optional bytes passwd = 2;

}

message login_account_response

{

required int32 ec = 1;

optional bytes token = 2;

}

然后保存退出(按esc,输入:wq)

这样就在ProtoBuf的文件夹下面生成了proto文件,MAC默认没有可以打开这个文本文件的,所以选择TextEdit.app进行打开,你可以看到文件内容

注意:proto的内容格式一定要正确,可以将自己创建出来的文本格式和示例的进行对比,package有没有拼写错误,需要分号的地方是否有分号,包括大括号的格式。如果内容格式有问题,可能会导致后面无法生成正确的OC文件

生成OC文件

cd ~/Desktop/ProtoBuf

protoc --plugin=/usr/local/bin/protoc-gen-objc person.proto --objc_out="./"

这样,就在ProtoBuf的文件夹下面生成了OC文件

工程中Protobuf的应用

做了那么多铺垫,说了那么多废话,我们的核心部分终于来了

首先新建一个工程(假设工程名为TestProtoc),并且用CocoaPod在工程中配置ProtocolBuffer

cd~/Desktop/TestProtoc

pod init

打开Podfile文件

输入

pod "ProtocolBuffers", "~> 1.9.7”

关闭Podfile继续在terminal输入

pod install

然后终于可以不理terminal了,我们进入工程做事

先把生成的OC文件导入到工程中,我们以get和post两种网络数据方式进行示例

在ViewController.m文件中导入以下文件

#import <ProtocolBuffers/ProtocolBuffers.h>

#import "Web_test.pb.h"

Get方式

- (void)protoGet

{

NSData *raw_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"此处填写url"]];

login_account_response *response = [[login_account_response alloc]init];

response = [login_account_response parseFromData:raw_data];

NSLog(@"=====%@",response);

NSLog(@"----- %@", [[NSString alloc]initWithData:response.token encoding:NSUTF8StringEncoding]);

}

Post方式

- (void)protoPost

{

NSURL *url = [NSURL URLWithString:@"此处填写url"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

request.timeoutInterval = 0.5;

request.HTTPMethod = @"POST";

// 设置请求体

login_account_requestBuilder *builder = [login_account_request builder];

NSString *accountStr = [NSString stringWithFormat:@"hello"];

NSString *passwdStr = [NSString stringWithFormat:@"123"];

builder.account = [accountStr dataUsingEncoding:NSUTF8StringEncoding];

builder.passwd = [passwdStr dataUsingEncoding:NSUTF8StringEncoding];

login_account_request *req = [builder build];

NSLog(@"%@",[req data]);

request.HTTPBody = [req data];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

if (error)

{

NSLog(@"error = %@",error);

}

else

{

NSLog(@"------ data = %@",data);

NSLog(@"success");

// 返回的数据

NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",result);

}

}];

[task resume];

}

感谢 http://www.2cto.com/kf/201503/382440.html的文章作者
感谢 http://www.jianshu.com/p/5c69bd048750.html 的文档
感谢 http://blog.csdn.net/u014202635/article/details/46531329的文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  object-c protobuf