做過幾次分享秀存,這次我也把我的經(jīng)驗總結(jié)分享給大家吧尊勿,只做的QQ和微信的分享
1.登錄友盟官網(wǎng)去添加一個應(yīng)用
添加應(yīng)用
2.填寫好應(yīng)用相應(yīng)信息
填寫信息
3.保存得到的AppKey蛉拙,后面在工程中會使用到
這里附上官方分享文檔鏈接友盟分享
首先在工程中導(dǎo)入友盟分享SDK
友盟分享SDK
我是直接在官網(wǎng)下載所需要的精簡版玛臂,然后直接導(dǎo)入徒恋,上面鏈接有pods導(dǎo)入教程
添加以下系統(tǒng)依賴庫??
添加系統(tǒng)依賴庫
代碼集成
打開工程AppDelegate.h文件夾導(dǎo)入頭文件
#import <UMSocialCore/UMSocialCore.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/* 打開調(diào)試日志 */
[[UMSocialManager defaultManager] openLog:YES];
/* 設(shè)置友盟appkey */
[[UMSocialManager defaultManager] setUmSocialAppkey:USHARE_DEMO_APPKEY];
[self configUSharePlatforms];
return YES;
}
- (void)configUSharePlatforms
{
/* 設(shè)置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WXAPPKEY appSecret:WXAppSecret redirectURL:@"http://mobile.umeng.com/social"];
/* 設(shè)置分享到QQ互聯(lián)的appID
* U-Share SDK為了兼容大部分平臺命名尔店,統(tǒng)一用appKey和appSecret進行參數(shù)設(shè)置眨攘,而QQ平臺僅需將appID作為U-Share的appKey參數(shù)傳進即可。
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAPPKEY/*設(shè)置QQ平臺的appID*/ appSecret:QQAppSecret redirectURL:@"http://mobile.umeng.com/social"];
}
USHARE_DEMO_APPKEY友盟的APPKEY宏
界面展示和分享調(diào)用
自定義一個UICollectionView
#pragma mark -------------------- UICollectionViewDataSource --------------------
//定義展示的UICollectionViewCell的個數(shù)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.shareIconAry.count;
}
//定義展示的Section的個數(shù)
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每個UICollectionView展示的內(nèi)容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identify = @"cell";
self.shareCell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
// [cell sizeToFit];
self.shareCell.icon.image = [UIImage imageNamed:self.shareIconAry[indexPath.row]];
return self.shareCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
__weak typeof(self) weakSelf = self;
switch (indexPath.row) {
case 0:
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
break;
case 1:
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
break;
case 2:
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_QQ];
break;
case 3:
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_Qzone];
break;
default:
break;
}
}
分享回調(diào)
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
//創(chuàng)建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創(chuàng)建網(wǎng)頁內(nèi)容對象
// NSString* thumbURL = @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
NSString * link = @"share_download";
NSString * thumbURL = [[NSString alloc]initWithFormat:@"www.baidu.com"];
// 將鏈接轉(zhuǎn)譯為UTF8因為 codeStr中有中文會導(dǎo)致QQ分享鏈接出錯
NSString* encodedString = [thumbURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"百度一下嚣州,你就知道" descr:nil thumImage:encodedString];
//設(shè)置網(wǎng)頁地址
shareObject.webpageUrl = encodedString;
//分享消息對象設(shè)置分享內(nèi)容對象
messageObject.shareObject = shareObject;
//調(diào)用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
[LCProgressHUD showFailure:@"分享失敗"];
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
[LCProgressHUD showSuccess:@"分享成功"];
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
// [self alertWithError:error];
}];
}
效果圖
效果圖
代碼Demo我已經(jīng)上傳到了GitHub 下載地址
還請大家多多提意見 喜歡的給個Stare
我是Renjiee 我要做最騷的程序猿??????