ios sharesdk分享功能怎么做

ios sharesdk分享功能怎么做,第1张

1.在 ShareSDK(http://www.shareSDK.cn)的官方上下载 SDK 并导入工程 将 ShareSDK 文件夹到项目文件夹中夹并拖入项目中。

2. 添加依赖框架(Framework) 打开项目设置中的 Build Phases 页,在“Link Binary With Libraries”一栏中,点击左下角的“+” 号;在d出窗口里面分别以下库加入到项目中: SystemConfiguration.framework QuartzCore.framework MessageUI.framework libicucore.dylib

3.引入社区应用配置信息。 打开 main.m 文件加入

#import

如图:

(P.S. 此头文件必须在此文件下导入,否则会导致编译不通过) 4. 配置所有社交平台的 AppKey 打开 ShareSDKConfig.h 文件,根据需求设置各个平台的 App 相关信息(每个平台的 App 都需要到相 应平台上进行应用登记后来取的相关信息) 。如图:

5.

配置 URL Scheme 打开*-Info.plist(*代表你的工程名字) 。在配置文件中新增一项 URL types (如果存在可以不创建) 展开 URL types – URL Schemes, URL Schemes 下新增一项用于新浪微博 , 在 授权的 Scheme(如果不添加则会导致新浪微博的 SSO 授权方式无法返回应用) 。其填写格式为: sinaweibosso.2279784657,其中后面的数字段为你在新浪微博申请的 AppKey。如下图所示:

另外,如果需要使用微信或者 QQ 平台接口也需要在此项下面添加相应的 Scheme。 6.嵌入代码

打开*AppDelegate.m(*代表你的工程名字)�6�5 在

�6�5-

(BOOL)application:(UIApplication

*)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions �6�5 方法内添加如

下语句:

[ShareSDK registerApp:@"520520test"]然后,在处理请求 URL 的委托方法中加入 ShareSDK 的处理方法,如下: - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [ShareSDK handleOpenURL:url wxDelegate:self]} - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [ShareSDK handleOpenURL:url wxDelegate:self]} 此处理方法已包括微信和 QQ 的回复处理,如果使用 ShareSDK 内置提示功能可以不需要再实现消息的 微信和 QQ 的回复消息捕获。

7.分享内容 在分享内容部分 ShareSDK 提供了三种接口来满足不同需求的分享功能实现。 7.1 菜单方式分享: 使用此方式进行分享会首先d出菜单供用户选择分享的目标平台, 然后再显示内容编辑界面供用户进 行分享内容编辑,最后进行分享。调用该方式的接口如下:

id publishContent = [ShareSDK publishContent:@"content" defaultContent:@"" image:[UIImage imageNamed:@"Icon.png"] imageQuality:0.8 mediaType:SSPublishContentMediaTypeNews title:@"ShareSDK" url:url musicFileUrl:nil extInfo:nil fileData:nil]

[ShareSDK showShareActionSheet:self share

第一步:在targets->info->url types中添加一项,命名为wb+appid(到官网开放平台去申请)

第二步:写一个分享功能类

[objc] view plain copy print?

// 省略头文件

@interface HYBShareSDKHelper : NSObject

+ (void)registerShareSDK

+ (BOOL)handleOpenURL:(NSURL *)url

+ (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

// 调用此方法来分享信息

typedef void (^HYBShareCompletion)(BOOL successful)

+ (void)shareWithContent:(NSString *)content

toController:(UIViewController *)controller

pngImage:(UIImage *)pngImage

title:(NSString *)title

url:(NSString *)url

mediaType:(SSPublishContentMediaType)mediaType

shareViewDelegate:(id<ISSShareViewDelegate>)shareViewDelegate

completion:(HYBShareCompletion)completion

@end

外部调用上面封装的方法来实现功能

[objc] view plain copy print?

//

// HYBShareSDKHelper.m

// CustomSharedSDKDemo

//

#import "HYBShareSDKHelper.h"

#import "HYBAppCommonInfoTool.h"

#import "HYBShareView.h"

#define kShareSDKAppKey @""

#define kShareSDKAppSecret @""

#define kSinaWeiboAppKey @""

#define kSinaWeiboAppSecret @""

@implementation HYBShareSDKHelper

+ (void)registerShareSDK {

[ShareSDK registerApp:kShareSDKAppKey]

// 添加新浪微博应用

NSString *redirectUri = @""

// 添加新浪微博应用

[ShareSDK connectSinaWeiboWithAppKey:kSinaWeiboAppKey

appSecret:kSinaWeiboAppSecret

redirectUri:redirectUri]

// 当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台

[ShareSDK connectSinaWeiboWithAppKey:kSinaWeiboAppKey

appSecret:kSinaWeiboAppSecret

redirectUri:redirectUri

weiboSDKCls:[WeiboSDK class]]

return

}

+ (BOOL)handleOpenURL:(NSURL *)url {

return [ShareSDK handleOpenURL:url wxDelegate:self]

}

+ (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self]

}

// 这里是自己定制的d出分享UI

+ (void)showShareViewInController:(UIViewController *)controller completion:(HYBShareClickBlock)completion {

HYBShareView *sv = [[HYBShareView alloc] initWithImages:@[@"sns_wx_icon", @"sns_wx_fr_icon", @"sns_qq_icon", @"sns_qzone_icon", @"sns_sina_icon"] titles:@[@"微信好友", @"微信朋友圈", @"QQ好友", @"QQ空间", @"新浪微博"] completion:^(NSUInteger index) {

if (completion) {

completion(index)

}

}]

[sv showInController:controller]

}

+ (void)shareWithContent:(NSString *)content

toController:(UIViewController *)controller

pngImage:(UIImage *)pngImage

title:(NSString *)title

url:(NSString *)url

mediaType:(SSPublishContentMediaType)mediaType

shareViewDelegate:(id<ISSShareViewDelegate>)shareViewDelegate

completion:(HYBShareCompletion)completion {

// 分享内容

id<ISSContent>sharedContent = [ShareSDK content:content

defaultContent:content

image:[ShareSDK pngImageWithImage:pngImage]

title: title

url:url

description:@"自己看着办"

mediaType:mediaType]

// 验证参数

id<ISSAuthOptions>authOptions = [ShareSDK authOptionsWithAutoAuth:YES

allowCallback:YES

authViewStyle:SSAuthViewStyleFullScreenPopup

viewDelegate:nil

authManagerViewDelegate:nil]

// 显示分享列表

[self showShareViewInController:controller completion:^(NSUInteger index) {

if (index == 4) {// 新浪微博

[self shareToSinaWeiboWithContent:sharedContent authOptions:authOptions content:content pngImage:pngImage completion:^(BOOL successful) {

if (completion) {

completion(successful)

}

}]

}

}]

}

// 分享到Sina weibo

+ (void)shareToSinaWeiboWithContent:(id<ISSContent>)sharedContent

authOptions:(id<ISSAuthOptions>)authOptions

content:(NSString *)content

pngImage:(UIImage *)pngImage

completion:(HYBShareCompletion)completion {

[sharedContent addSinaWeiboUnitWithContent:content

image:[ShareSDK pngImageWithImage:pngImage]]

// if haven authorized, then call

if (![ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo]) {

[ShareSDK authWithType:ShareTypeSinaWeibo options:authOptions result:^(SSAuthState state, id<ICMErrorInfo>error) {

if (state == SSAuthStateSuccess) {

id<ISSShareOptions>shareOptions = [ShareSDK simpleShareOptionsWithTitle:@"美容总监"

shareViewDelegate:nil]

[ShareSDK clientShareContent:sharedContent

type:ShareTypeSinaWeibo

authOptions:authOptions

shareOptions:shareOptions

statusBarTips:YES

result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo>statusInfo, id<ICMErrorInfo>error, BOOL end) {

if (completion &&end) {

DDLogVerbose(@"%@", error.errorDescription)

completion(state == SSPublishContentStateSuccess)

}

}]

}

}]

} else {// use client share to Sina App Client

id<ISSShareOptions>shareOptions = [ShareSDK simpleShareOptionsWithTitle:@"美容总监"

shareViewDelegate:nil]

[ShareSDK clientShareContent:sharedContent

type:ShareTypeSinaWeibo

authOptions:authOptions

shareOptions:shareOptions

statusBarTips:YES

result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo>statusInfo, id<ICMErrorInfo>error, BOOL end) {

if (completion &&end) {

DDLogVerbose(@"%@", error.errorDescription)

completion(state == SSPublishContentStateSuccess)

}

}]

}

}

@end


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11725728.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存