ios微信和QQ原生分享

本文介绍了如何在iOS应用中实现原生的微信和QQ分享功能,包括从官方SDK下载到具体分享步骤的详细过程,适用于需要原生分享的项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在大家用的最多的还是第三方分享,第三方分享用起来确实非常方便容易,但是有些项目要求要原生分享,特别是一些很老的项目他们的分享几乎都是原生的,所以原生的分享对于一个开发人员来说还是要了解的,下面是自己维护老项目时修改分享是对原生分享的了解。

     SDK去官方网站下载

一.  微信原生分享

1.appdelegate.h  
需要导入WXApi.h并且实现WXApiDelegate协议  
  
  
2.  appdelegate.m  
//实现以下方法,实现微信和app的交互  
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{  
  
    if ([url.schemeisEqualToString:@"your appid"]) {  
        return [WXApihandleOpenURL:url delegate:self];  
    }  
    else{  
        returnYES;  
    }  
}  
  
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{  
  
    if ([url.schemeisEqualToString:@"your appid"]) {  
        return [WXApihandleOpenURL:url delegate:self];  
    }  
    else{  
        returnYES;  
    }  
  
}  
//实现以下方法进行微信分享回调:  
-(void)onResp:(BaseResp *)resp  
{  
        if (resp.errCode==-2) {  
        //未成功分享  
    }  
    else{  
        //成功分享  
    }  
}  
  
  
  
3 在需要调用微信的Controller中实现以下方法:  
- (void)sharePYQTap{  
    //  
    WXMediaMessage *message=[WXMediaMessagemessage];  
    message.title=@" ";  
    message.description=@"";  
    [message setThumbImage:[UIImageimageNamed:@"iconLogin"]];  
      
    WXWebpageObject *webpageObject=[WXWebpageObjectobject];  
    webpageObject.webpageUrl=@"";  
    message.mediaObject=webpageObject;  
      
    SendMessageToWXReq *req=[[SendMessageToWXReqalloc]init];  
    req.bText=NO;  
    req.message=message;  
    req.scene=WXSceneTimeline;  
      
    [WXApisendReq:req];  
  
}  

二.   QQ原生分享

1.导入qq的sdk两个文件分别是TencentOpenApi_IOS_Bundle.bundle和TencentOpenAPI.framework  
  
2. 在项目的targets中找到Info列表,在其中的URL Types中添加新的URL Schemes为tencent+appid identifier为TencentApiIdentifier  
  
3 appdelegate.h  
导入  
#import <TencentOpenAPI/QQApiInterface.h>  
并实现协议QQApiInterfaceDelegate  
  
4 appdelegate.m  
//实现以下方法,实现QQ和app的交互,因为之前集成了微信所以需要判断一下  
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{  
      
    if ([url.schemeisEqualToString:@"微信注册的appid"]) {  
        return [WXApihandleOpenURL:url delegate:self];  
    }  
    elseif ([url.schemeisEqualToString:@"上一步URL Schemes里面填的内容"]){  
        //  
        return [QQApiInterface handleOpenURL:url delegate:self];  
    }  
    else{  
        returnYES;  
    }  
}  
  
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{  
  
    if ([url.schemeisEqualToString:@"微信注册的appid"]) {  
        return [WXApihandleOpenURL:url delegate:self];  
    }  
    elseif ([url.schemeisEqualToString:@"上一步URL Schemes里面填的内容"]){  
        return [QQApiInterface handleOpenURL:url delegate:self];  
    }  
    else{  
        return YES;  
    }  
}  
//实现以下方法进行qq分享回调:  
- (void)onResp:(QQBaseResp *)resp  
{  
    switch (resp.type)  
    {  
        caseESENDMESSAGETOQQRESPTYPE:  
        {  
            SendMessageToQQResp* sendResp = (SendMessageToQQResp*)resp;  
            UIAlertView* alert = [[UIAlertViewalloc] initWithTitle:sendResp.resultmessage:sendResp.errorDescriptiondelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];  
            [alert show];  
            break;  
        }  
        default:  
        {              
            break;  
        }  
    }  
}  
  
  
  
***如果你同时集成qq和微信分享,就会出现问题如下:  
他们的回调方法名称竟然是一样的,直接报错没悬念:这时候就需要进行处理了,找的一个比较好的解决方案:  
- (void)onResp:(id)resp{  
   //  
      
    //Wechat分享返回  
      
    if ([respisKindOfClass:[SendMessageToWXRespclass]]) {  
          
        SendMessageToWXResp * tmpResp = (SendMessageToWXResp *)resp;  
          
        if (tmpResp.errCode ==WXSuccess) {  
            //分享成功  
              
        }else{  
            //分享失败  
              
        }  
          
    }  
      
    //QQ分享返回  
      
    if ([respisKindOfClass:[SendMessageToQQRespclass]]) {  
          
        SendMessageToQQResp * tmpResp = (SendMessageToQQResp *)resp;  
          
        if (tmpResp.type ==ESENDMESSAGETOQQRESPTYPE && [tmpResp.resultintegerValue] == 0) {  
            //分享成功  
        }  
        else{  
            //分享失败  
        }  
          
    }  
}  
  
  
5 在需要调用QQ分享的Controller中实现以下方法:  
- (void)shareQQTap{  
    //官网文档没写这一步,但是这个必须得有否则会提示app未注册  
    TencentOAuth *auth=[[TencentOAuthalloc]initWithAppId:@"你的appid"andDelegate:self];  
      
    NSString    *utf8String =@"http://www.baidu.com";  
    NSString    *title =@" ";  
    NSString    *description =@"";  
    NSData      *data =[[NSDataalloc]init];  
    data=UIImagePNGRepresentation([UIImageimageNamed:@"iconLogin"]);  
      
    QQApiNewsObject     *newsObj=[QQApiNewsObjectobjectWithURL:[NSURLURLWithString:utf8String] title:title description:descriptionpreviewImageData:data];  
      
    SendMessageToQQReq  *req = [SendMessageToQQReqreqWithContent:newsObj];  
    //将内容分享到qq  
    //QQApiSendResultCode sent = [QQApiInterface sendReq:req];  
    //将内容分享到qzone  
    QQApiSendResultCode sent = [QQApiInterfaceSendReqToQZone:req];  
    [selfhandleSendResult:sent];  
  
}  
- (void)handleSendResult:(QQApiSendResultCode)sendResult  
{  
    switch (sendResult)  
    {  
        caseEQQAPIAPPNOTREGISTED:  
        {  
            UIAlertView *msgbox = [[UIAlertViewalloc] initWithTitle:@"Error"message:@"App未注册"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:nil];  
            [msgbox show];  
              
            break;  
        }  
        caseEQQAPIMESSAGECONTENTINVALID:  
        caseEQQAPIMESSAGECONTENTNULL:  
        caseEQQAPIMESSAGETYPEINVALID:  
        {  
            UIAlertView *msgbox = [[UIAlertViewalloc] initWithTitle:@"Error"message:@"发送参数错误"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:nil];  
            [msgbox show];  
              
            break;  
        }  
        caseEQQAPIQQNOTINSTALLED:  
        {  
            UIAlertView *msgbox = [[UIAlertViewalloc] initWithTitle:@"Error"message:@"未安装手Q"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:nil];  
            [msgbox show];  
              
            break;  
        }  
        caseEQQAPIQQNOTSUPPORTAPI:  
        {  
            UIAlertView *msgbox = [[UIAlertViewalloc] initWithTitle:@"Error"message:@"API接口不支持"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:nil];  
            [msgbox show];  
              
            break;  
        }  
        caseEQQAPISENDFAILD:  
        {  
            UIAlertView *msgbox = [[UIAlertViewalloc] initWithTitle:@"Error"message:@"发送失败"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:nil];  
            [msgbox show];  
              
            break;  
        }  
        default:  
        {  
            break;  
        }  
    }  
}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值