我只做了文字和圖片分享功能
1. TARGETS - Info - URL Types
identifier -> weixin
URL Schemes -> 應(yīng)用id
2.在A(yíng)ppDelegate.h 引入頭文件
<pre name="code" class="objc">#import "WXApi.h"{/** * WXSceneSession 分享到會(huì)話(huà) * WXSceneTimeline 分享到朋友圈 * WXSceneFavorite 分享到我的收藏 */ enum WXScene _scene;}
3.在A(yíng)ppDelegate.m
- (id)init{ if(self = [super init]){ _scene = WXSceneSession; } return self;}-(void) changeScene:(NSInteger)scene{ _scene = scene;}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // 其它代碼 // 向微信注冊(cè)應(yīng)用ID [WXApi registerApp:@"xxooxoxoxoxoxoxo"];}#pragma mark - 重寫(xiě)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];}
//<span style="font-family: Arial, Helvetica, sans-serif;">onReq 和 </span><span style="font-family: Arial, Helvetica, sans-serif;">onResp 可以不寫(xiě)</span><span style="font-family: Arial, Helvetica, sans-serif;"></span>-(void) onReq:(BaseReq*)req{ if([req isKindOfClass:[GetMessageFromWXReq class]]) { // 微信請(qǐng)求App提供內(nèi)容, 需要app提供內(nèi)容后使用sendRsp返回 NSString *strTitle = [NSString stringWithFormat:@"微信請(qǐng)求App提供內(nèi)容"]; NSString *strMsg = @"微信請(qǐng)求App提供內(nèi)容,App要調(diào)用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; //顯示微信傳過(guò)來(lái)的內(nèi)容 WXAppExtendObject *obj = msg.mediaObject; NSString *strTitle = [NSString stringWithFormat:@"微信請(qǐng)求App顯示內(nèi)容"]; NSString *strMsg = [NSString stringWithFormat:@"標(biāo)題:%@ \n內(nèi)容:%@ \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]]) { //從微信啟動(dòng)App NSString *strTitle = [NSString stringWithFormat:@"從微信啟動(dòng)"]; NSString *strMsg = @"這是從微信啟動(dòng)的消息"; 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:@"發(fā)送媒體消息結(jié)果"]; 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]; }}
4.這是我寫(xiě)好的在微信會(huì)話(huà)和朋友圈分享文字或者圖片的方法
直接調(diào)用就可以
#pragma mark - 微信, 朋友圈分享#pragma mark 文字分享- (void)sharedByWeChatWithText:(NSString *)WeChatMessage sceneType:(int)sceneType{ SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init]autorelease]; req.text = WeChatMessage; req.bText = YES; req.scene = sceneType; [WXApi sendReq:req];}#pragma mark 圖片分享- (void)sharedByWeChatWithImage:(NSString *)imageName sceneType:(int)sceneType{ WXMediaMessage *message = [WXMediaMessage message]; [message setThumbImage:[UIImage imageNamed:imageName]]; WXImageObject *ext = [WXImageObject object]; NSString *filePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]; ext.imageData = [NSData dataWithContentsOfFile:filePath]; UIImage *image = [UIImage imageWithData:ext.imageData]; ext.imageData = UIImagePNGRepresentation(image); message.mediaObject = ext; SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init]autorelease]; req.bText = NO; req.message = message; req.scene = sceneType; [WXApi sendReq:req];}
別的可以自然代碼問(wèn)題不大,我想到一個(gè)可能的問(wèn)題,你試試運(yùn)行過(guò)去后拔掉USB線(xiàn),然后手機(jī)重新運(yùn)行應(yīng)用,原因是微信的sdk的debug下面有問(wèn)題。。。想當(dāng)初我唄困在這里好久好久,。。。
我同樣是IOS6的系統(tǒng),分享到微信上的基本十次有九次都是看不到的;看上去好像是分享成功了,其實(shí)根本沒(méi)被接受。。。這是微信與IOS6之間的沖突問(wèn)題,上次我提問(wèn)給微信團(tuán)隊(duì),他們回復(fù)我說(shuō)暫時(shí)還有問(wèn)題,帶來(lái)不便十分抱歉。。。
聯(lián)系客服