官方指南:http://dev.wechat.com/document/sdk/ios/index.html
- 向微信注册你的应用程序id。
- 下载微信终端SDK文件。
- 将SDK文件中包含的libWeChatSDK.a,WXApi.h, WXApiObject.h三个文件添加到你所建的工程中。
- 在Xcode4中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序id。如果使用的是Xcode3或更低版本,则需要在plist文件中添加。
- 程序启动时,在代码中向微信终端注册你的id:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [WXApi registerApp:"wxd930ea5d5a258f4f"];
- 重写AppDelegate的handleOpenURL和openURL方法:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [WXApi handleOpenURL:url delegate:self]; }
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [WXApi handleOpenURL:url delegate:self]; }
- 实现WXApiDelegate协议的两个方法:
- (void) onReq:(BaseReq*)req
- (void) onResp:(BaseResp*)resp
-
向微信发送图片的例子:
- (void) sendImageContent { WXMediaMessage *message = [WXMediaMessage message]; [message setThumbImage:[UIImage imageNamed:@"res2.jpg"]];
WXImageObject *ext = [WXImageObject object]; NSString *filePath = [[NSBundle mainBundle] pathForResource:
@
"res2" ofType:@"jpg"]; ext.imageData = [NSData dataWithContentsOfFile:filePath] ;message.mediaObject = ext;
SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease]; req.bText = NO; req.message = message; //req.scene = WXSceneTimeline; //选择发送到朋友圈,默认值为WXSceneSession,发送到会话
[WXApi sendReq:req]; }
- 收到微信程序回应的例子:
-(void) onResp:(BaseResp*)resp { if([resp isKindOfClass:[SendMessageToWXResp class]]) { NSString *strMsg = [NSString stringWithFormat:
@
"发送消息结果:d", resp.errCode]; - OK!