1. TARGETS - Info - URL Types
identifier -> weixin
URL Schemes -> 应用id
2.在AppDelegate.h 引入头文件 - #import
"WXApi.h"
定义
{ enum WXScene _scene; }
3.在AppDelegate.m中
- (id)init{ if(self = [super init]){ _scene = WXSceneSession; } return self; } -(void) changeScene:(NSInteger)scene { _scene = scene; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 其它代码 // 向微信注册应用ID [WXApi registerApp:@"wx1432f1d44b2f2696"]; } #pragma mark - //重写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]; } -(void) onReq:(BaseReq*)req { if([req isKindOfClass:[GetMessageFromWXReq class]]) { // 微信请求App提供内容, 需要app提供内容后使用sendRsp返回 NSString *strTitle = [NSString stringWithFormat:@"微信请求App提供内容"]; NSString *strMsg = @"微信请求App提供内容,App要调用sendResp:GetMessageFromWXResp返回给微信"; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; alert.tag = 1000; [alert show]; [alert release]; } else if([req isKindOfClass:[ShowMessageFromWXReq class]]) { ShowMessageFromWXReq* temp = (ShowMessageFromWXReq*)req; WXMediaMessage *msg = temp.message; //显示微信传过来的内容 WXAppExtendObject *obj = msg.mediaObject; NSString *strTitle = [NSString stringWithFormat:@"微信请求App显示内容"]; NSString *strMsg = [NSString stringWithFormat:@"标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%u bytes\n\n", msg.title, msg.description, obj.extInfo, msg.thumbData.length]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } else if([req isKindOfClass:[LaunchFromWXReq class]]) { //从微信启动App NSString *strTitle = [NSString stringWithFormat:@"从微信启动"]; NSString *strMsg = @"这是从微信启动的消息"; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } } -(void) onResp:(BaseResp*)resp { if([resp isKindOfClass:[SendMessageToWXResp class]]) { NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"]; NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } } - (void) sendTextContent { SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease]; req.text = @"这里写你要分享的内容。"; req.bText = YES; req.scene = _scene; [WXApi sendReq:req]; }
4.在你控制器里点击按钮方法里
SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease]; req.text = @"李伟宾: 这是我做的测试!http://mobilebj.cn"; req.bText = YES; req.scene = WXSceneSession; //发送到会话 [WXApi sendReq:req]; break; case 2: //微信朋友圈 NSLog(@"微信朋友圈"); SendMessageToWXReq* req2 = [[[SendMessageToWXReq alloc] init]autorelease]; req2.text = @"李伟宾: 这是我做的测试!http://mobilebj.cn"; req2.bText = YES; req2.scene = WXSceneTimeline; //发送到朋友圈 [WXApi sendReq:req2];
时间: 2024-10-05 10:48:30