天天看點

iOS QQ空間 好友分享

iOS QQ空間 好友分享

1.進入騰訊開發者平台,可以檢視分享SDK文檔,根據說明按步驟接入

demo示範

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(110, 100, 100, 30);

    [btn setTitle:@"QQ空間分享" forState:UIControlStateNormal];

    [self.view addSubview:btn];

    btn.backgroundColor = [UIColor redColor];

    [btn addTarget:self action:@selector(qqZoneShare) forControlEvents:UIControlEventTouchUpInside];

    UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeCustom];

    btnTwo.frame = CGRectMake(110, 160, 100, 30);

    [btnTwo setTitle:@"QQ好友分享" forState:UIControlStateNormal];

    [self.view addSubview:btnTwo];

    btnTwo.backgroundColor = [UIColor redColor];

    [btnTwo addTarget:self action:@selector(qqFriendShare) forControlEvents:UIControlEventTouchUpInside];

}

- (void)qqFriendShare {

    NSURL *previewURL = [NSURL URLWithString:@"http://baidu.com"];

    NSString *path = [[NSBundle mainBundle] bundlePath];

    NSString *name = [NSString stringWithFormat:@"iconname.png"];

    NSString *finalPath = [path stringByAppendingPathComponent:name];

    NSData *previeImgData = [NSData dataWithContentsOfFile:finalPath];

    QQApiNewsObject *imgObj = [QQApiNewsObject objectWithURL:previewURL title:@"分享内容的title" description:@"本寶寶是内容的描述" previewImageData:previeImgData];

    [imgObj setCflag:kQQAPICtrlFlagQQShare];

    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];

    QQApiSendResultCode sent = [QQApiInterface sendReq:req];

    [self handleSendResult:sent];

}

- (void)qqZoneShare {

#pragma mark--分享純文字 success

//    QQApiTextObject *txtObj = [QQApiTextObject objectWithText:@"分享内容的text"];

#pragma mark--分享news success

//    NSURL *previewURL = [NSURL URLWithString:@"http://v.youku.com/v_show/id_XMTQ3OTM4MzMxMg==_ev_3.html?from=y1.3-idx-uhome-1519-20887.205805-205902.3-1"];

    NSURL *previewURL = [NSURL URLWithString:@"http://baidu.com"];

    NSString *path = [[NSBundle mainBundle] bundlePath];

    NSString *name = [NSString stringWithFormat:@"iconname.png"];

    NSString *finalPath = [path stringByAppendingPathComponent:name];

    NSData *previeImgData = [NSData dataWithContentsOfFile:finalPath];

    QQApiNewsObject *imgObj = [QQApiNewsObject objectWithURL:previewURL title:@"分享内容的title" description:@"本寶寶是内容的描述" previewImageData:previeImgData];

    [imgObj setCflag:kQQAPICtrlFlagQZoneShareOnStart];

    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];

    QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];

    [self handleSendResult:sent];

}

- (void)handleSendResult:(QQApiSendResultCode)sendResult {

    switch (sendResult) {

        case EQQAPIAPPNOTREGISTED:

        {

            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注冊" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

            [msgbox show];

            break;

        }

        case EQQAPIMESSAGECONTENTINVALID:

        case EQQAPIMESSAGECONTENTNULL:

        case EQQAPIMESSAGETYPEINVALID:

        {

            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發送參數錯誤" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

            [msgbox show];

            break;

        }

        case EQQAPIQQNOTINSTALLED:

        {

            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安裝手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

            [msgbox show];

            break;

        }

        case EQQAPIQQNOTSUPPORTAPI:

        {

            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支援" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

            [msgbox show];

            break;

        }

        case EQQAPISENDFAILD:

        {

            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發送失敗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

            [msgbox show];

            break;

        }

        default:

            break;

    }

}