调用微信API 的简单demo

 最近做的项目用到了推荐功能,比如短信,微信和微博。这里只讲微信部分,官方的例子对于刚入门的人来说看起来有点复杂,我把其功能值缩成一项“向微信好友发消息”。这样的话,第一次接触该功能时则比较容易上手。我将原来类减少到2个类,一个appdelegate 和一个SendMsgToWeChatViewController  我试过了可以运行。
对于前期xcode的调配,看看官方文档就行,很简单。

 
其核心代码就这些

(1)appdelegate里:

<1>在didFinishLaunchingWithOptions 里写上

    [WXApi registerApp:@"wxd930ea5......."];// 自己去网上申请个appID吧 


<2>还是在appdelegate里写下以下三个方法

-(void) onResp:(BaseResp*)resp

{

    if([resp isKindOfClass:[SendMessageToWXResp class]])

    {

        NSString *strTitle = [NSString stringWithFormat:@"发送结果"];

        NSString *strMsg = [NSString stringWithFormat:@"发送媒体消息结果:%d", resp.errCode];

        

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];

        [alert release];

    }

}



- (void) sendTextContent:(NSString*)nsText

{

    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];

    req.bText = YES;

    req.text = nsText;

    

    [WXApi sendReq:req];

}


- (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];

}


(2) SendMsgToWeChatViewController类中只要写一个代理的代码就行

    [self.delegate sendTextContent:stringWithText] ;




还是直接上传demo吧


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值