调用微信API 的简单demo

来源:互联网 发布:福建网络干部学院 编辑:程序博客网 时间:2024/05/04 16:18

 最近做的项目用到了推荐功能,比如短信,微信和微博。这里只讲微信部分,官方的例子对于刚入门的人来说看起来有点复杂,我把其功能值缩成一项“向微信好友发消息”。这样的话,第一次接触该功能时则比较容易上手。我将原来类减少到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吧


原创粉丝点击