该代码由本开发团队自己开发,开发者只要花少量时间了解下该代码所提供的动态链接库的用法,就可以轻松开发自己的软件与苹果的icloud服务端进行联系人,相片等同步。从而实现PC与iphone,ipad等终端进行联系人,相片的同步。
官方网站:http://www.huluwa.org/
DEMO下载地址:http://www.huluwa.org/icloud/download/platfrom/pc/DataSyncSDK_intall.msi
下载并安装好DEMO后,运行DEMO,在弹出的窗口中输入icloud帐号及apple id(注:该apple id必须在苹果公司的icloud上登陆过),登陆成功后,在桌面右下角的苹果图标中右击鼠标,在弹出的菜单中选择所要的操作。


本文主要面向使用DateSync的开发者,开发者可通过此文学习如何使用DateSync SDK进行开发。

开发环境

1.安装Microsoft Visual C# 2008/2010

2.安装Microsoft .NET Framework 3.5

3.请将以下DLL文件加入到项目中(以下文件位于DataSyncSDK安装路径下)

DLL文件:

agsXMPP.dll

CE.iPhone.PList.dll

DataSync.dll

log4net.dll

Newtonsoft.Json.dll


主要类介绍

名称

说明

DataSyncObject

DateSync SDK的核心类,通过调用该类中的login,put,get,delete等方法实现在icloud中对联系人,相片流等功能进行操作。

Context

DataSyncObject类中的方法参数都通过该类传入值,传出处理结果。


如何使用DataSync SDK

首先导入DataSync.dll文件到工程中

1.用户登陆

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

       Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

}


2.相片下载

using DataSync;

//进度条回调函数
//参数:transfered已传输字节数, total 表示总字节数
//返回值: true:中断传输,false:继续传输

public static bool TransferProgress(int transfered,int total)
{

return false;
}

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

           Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }


   //登陆后,便可以下载相片

   Context getctx ;
   List<FileNode> files = ds.gIcloud.getPhotoList();//登陆成功后,通过该方法取得需要下载的文件列表
   foreach (FileNode item in files)
   {
       getctx = new Context();
       getctx.transferProgresscallback = new TransferProgressCallBack(TransferProgress);//进度条回调接口
       getctx.modeType = Context.ICLOUD_PS_MODE;//指定要操作的模块为相片流模块
       getctx.inItem = item;//要下载的文件节点
       ds.get(getctx);//下载文件
       if (getctx.outItem != null)
       {
           Console.Write("\n\n file " + getctx.outItem.filename + "down success.");
       }
   }
}

3.上传相片

using DataSync;

//进度条回调函数
//参数:transfered已传输字节数, total 表示总字节数
//返回值: true:中断传输,false:继续传输

public static bool TransferProgress(int transfered,int total)
{

return false;
}

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

                         Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }


   //登陆后,便可以上传相片

   Context putctx = new Context();
   putctx.transferProgresscallback = new TransferProgressCallBack(TransferProgress);
   putctx.srcFileFullName = "e:/ndphoto/IMG_0255.JPG";//指定要上传的文件
   putctx.modeType = Context.ICLOUD_PS_MODE;
   ret = ds.put(putctx); //上传一张相片,最大不能超过50M
   if (ret)
   {
       Console.Write("\n\n file put success. ");
   }

}

4.删除图片

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

   Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

   Context delctx = new Context();
   delctx.srcFileFullName = "e:/ndphoto/IMG_0260.JPG";//要删除的图片
   delctx.modeType = Context.ICLOUD_PS_MODE;
   ret = ds.delete(delctx); //删除图片
   if (ret)
   {
   Console.Write("\n\n file delete success. ");
   }

}

5.获取所有联系人列表

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

   Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

   Context ctx = new Context();
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;
   ds.get(ctx);//结果存在ctx.outContactList中
   List<Contact> contactList = ctx.outContactList;

}


6.删除所有联系人

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

   Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

   ICloudContact iCloudContact = ds.icloud.GetContacts();  
   Context ctx = new Context();
   ctx.contactList = iCloudContact.contacts;
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;    
   ds.delete(ctx);//删除所有联系人
   List<Contact> contactList = ctx.outContactList;

}

7.增加联系人

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

   Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

    //增加联系人
   List<Contact> contacts = new List<Contact>();
   Contact contact = new Contact { prefix = "pre", firstName = "aaa",
   lastName = "bbb", middleName = "ccc",phoneticLastName = "",
   phoneticFirstName = "Nicolas",suffix = "suff",nickName = "nickname",
   jobTitle = "jobTitle", department = "systemSport", companyName = "nd",
   birthday = "1999-01-01",notes = "测试添加一个完整的联系人"
   };
   电话
   contact.phones = new List<Phone>();
   Phone phone1 = new Phone { label = "WORK", field = "13635260966" };
   Phone phone2 = new Phone { label = "HOME", field = "059122321966" };
   Phone phone3 = new Phone { label = "custum", field = "15918726583" };
   contact.phones.Add(phone1);
   contact.phones.Add(phone2);
   contact.phones.Add(phone3);

   邮件
   contact.emailAddresses = new List<EmailAdr>();
   EmailAdr emailAdr1 = new EmailAdr { label = "WORK", field = "test_1@yahoo.com.cn" };    
   EmailAdr emailAdr2 = new EmailAdr { label = "HOME", field = "test_2@gmail.com" };
   EmailAdr emailAdr3 = new EmailAdr { label = "custum", field = "test_3@me.com" };
   contact.emailAddresses.Add(emailAdr1);
   contact.emailAddresses.Add(emailAdr2);
   contact.emailAddresses.Add(emailAdr3);

   地址
   contact.streetAddresses = new List<StreetAdr>();

   StreetField streetField1 = new StreetField();
   streetField1.city = "city";
   streetField1.country = "country";
   streetField1.state = "state";
   streetField1.street = "street";
   streetField1.countryCode = "countryCode";
   StreetAdr streetAdr1 = new StreetAdr { label = "HOME", field = streetField1 };

   StreetField streetField2 = new StreetField();
   streetField1.city = "city";
   streetField1.country = "country";
   streetField1.state = "state";
   streetField1.street = "street";
   streetField1.countryCode = "countryCode";
   StreetAdr streetAdr2 = new StreetAdr { label = "WORK", field = streetField2 };

   StreetField streetField3 = new StreetField();
   streetField1.city = "city";
   streetField1.country = "country";
   streetField1.state = "state";
   streetField1.street = "street";
   streetField1.countryCode = "countryCode";
   StreetAdr streetAdr3 = new StreetAdr { label = "custum", field = streetField3 };
   contact.streetAddresses.Add(streetAdr1);
   contact.streetAddresses.Add(streetAdr2);
   contact.streetAddresses.Add(streetAdr3);

   主页
   contact.urls = new List<Url>();
   Url url1 = new Url { label = "WORK", field = "www.sina.com" };
   Url url2 = new Url { label = "HOME", field = "www.apple.com" };
   Url url3 = new Url { label = "custum", field = "www.google.com" };
   contact.urls.Add(url1);
   contact.urls.Add(url2);
   contact.urls.Add(url3);

   概况
   contact.profiles = new List<Profile>();
   Profile profile1 = new Profile { label = "Facebook", field = "http://twitter.com",user = "sdfdf" };
   Profile profile2 = new Profile { label = "custum", field = "http://facebook.com",user = "jjjjjj" };
   contact.profiles.Add(profile1);
   contact.profiles.Add(profile2);

   日期
   contact.dates = new List<Date>();
   Date date1 = new Date { label = "other", field = "2011-01-01" };
   Date date2 = new Date { label = "custum", field = "2012-01-01" };
   contact.dates.Add(date1);
   contact.dates.Add(date2);

   相关人
   contact.relatedNames = new List<RelatedName>();
   RelatedName relatedName1 = new RelatedName { label = "other", field = "father" };
   RelatedName relatedName2 = new RelatedName { label = "custum", field = "brother" };
   contact.relatedNames.Add(relatedName1);
   contact.relatedNames.Add(relatedName2);

   通讯
   contact.IMs = new List<IM>();

   IMsfield iMsfield1 = new IMsfield();
   iMsfield1.IMService = "QQ";
   iMsfield1.userName = "qqusername";
   IMsfield iMsfield2 = new IMsfield();
   iMsfield2.IMService = "Facebook";
   iMsfield2.userName = "testname";

   IM iMs1 = new IM { label = "QQ", field = iMsfield1 };
   IM iMs2 = new IM { label = "weibo", field = iMsfield2 };
   contact.IMs.Add(iMs1);
   contact.IMs.Add(iMs2);

   contacts.Add(contact);
   Context ctx = new Context();
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;
   ctx.contactList = contacts;
   ds.put(ctx);

}

8.修改联系人

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

   Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

   //修改联系人
   Context ctx = new Context();
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;
   ds.get(ctx);
   List<Contact> contactList = ctx.outContactList;
   List<Contact> contacts = new List<Contact>();
   Contact contact = contactList[0];
   contact.firstName = "aaa";
   contact.lastName = "bbb";
   contact.phones = new List<Phone>();
   Phone phone = new Phone { label = "WORK", field = "987654321" };
   contact.phones.Add(phone);
   contacts.Add(contact);
   Context ctx = new Context();
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;
   ctx.contactList = contacts;
   ds.updata(ctx);

}

9.删除指定联系人

using DataSync;

static void Main()
{

   DataSyncObject ds = new DataSyncObject();//创建DataSyncObject对像
   ds.gIcloud = new Cloud();//生成gIcloud对像

   Context loginctx = new Context();//生成Context对像
   loginctx.loginUsername = "apple id ";//输入icoud 帐号
   loginctx.loginPassword = "password";//输入icoud 帐号密码
   loginctx.modeType = Context.ICLOUD_MODE;//输入Context参数的模块类型,当前设为登陆到icloud服务器上
   bool ret = ds.login(loginctx);//登陆icoud,成功返回true,失败返回false;
   if (ret == false)
    {
        return;
    }

   //删除联系人
   Context ctx = new Context();
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;
   ds.get(ctx);
   List<Contact> contactList = ctx.outContactList;
   List<Contact> delContacts = new List<Contact>();
   Phone phone = new Phone { label = "WORK", field = "15935260966" };
   Contact contact = contactList[0];
   delContacts.Add(contact);
   Context ctx = new Context();
   ctx.modeType = Context.ICLOUD_CONTACT_MODE;
   ctx.contactList = delContacts;
   ds.delete(ctx);

}