iOS項目社會化分享-微信分享,朋友圈分享

來源:互聯網
上載者:User

iOS項目社會化分享-分享,朋友圈分享

我只做了文字和圖片分享功能


1. TARGETS - Info - URL Types

identifier -> weixin

URL Schemes -> 應用id


2.在AppDelegate.h 引入標頭檔

#import "WXApi.h"{/** *      WXSceneSession   分享到會話 *      WXSceneTimeline  分享到朋友圈 *      WXSceneFavorite  分享到我的收藏 */    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:@"xxooxoxoxoxoxoxo"];}#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];}
//onReq 和 onResp 可以不寫-(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];    }}




4.這是我寫好的在會話和朋友圈分享文字或者圖片的方法

直接調用就可以


#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];}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.