StanwoodSocial
示例
要运行示例项目,请克隆仓库,并首先从 Example 目录中运行 pod install
。
重要
YouTube & Facebook
当然,任何操作都会在作者的 Feeds 上发布 ;).
Instagram API模拟环境将隔离环境外进行的任何活动,这意味着您不会看到任何点赞,也不能评论/分享。除非应用处于生产环境,否则API将返回未知媒体ID
。在示例项目中,我们将属于模拟环境的帖子传递过去,以查看任何活动。为了查看点赞,需要将您的@用户名
添加到Instagram模拟应用中。
安装
StanwoodSocial可以通过Stanwood主规格提供。要安装它,只需在Podfile中添加以下行,包括私有源规格
use_frameworks!
target 'Project' do
pod 'StanwoodSocial'
end
配置
- 在
AppDelegate
的didFinishLaunchingWithOptions
中添加服务和Facebook配置
// Configure Facebook
STSocialManager.configure(application: application, didFinishLaunchingWithOptions: launchOptions)
// Configure Social Services
let facebook = STSocialService(appID: [YOU_APP_ID], appSecret: [YOU_APP_SECRET], appType: .facebook)
let instagram = STSocialService(appID: [YOU_APP_ID], appSecret: [YOU_APP_SECRET], appType: .instagram, callbackURI: [CALLBACK_URI])
/// MARK: Google callback shoud be yourBundleIdentifier:/oauth2Callback
let youtube = STSocialService(appID: [YOU_APP_ID], appSecret: [YOU_APP_SECRET], appType: .youtube, callbackURI: [CALLBACK_URI])
let configurations = STSocialConfiguration(services: [facebook, instagram, youtube])
_ = STSocialManager.shared.set(configurations: configurations)
- 处理回调
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
/// Handling callback
STSocialManager.handle(callbackURL: url)
/// Confiugring with scheme
return STSocialManager.configure(app: app, open: url, options: options)
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
/// Handling callback
STSocialManager.handle(callbackURL: url)
return true
}
- 在
YOURSocialStreamViewController
的viewDidLoad
中设置目标
STSocialManager.shared.set(target: self)
- 实现
STSocialManagerDelegate
以在viewDidLoad
中获取登录/登出通知。STSocialManager.shared.delegate = self
func didLogout(type: STSocialType?) {
/// Reload data
collectionView.reloadData()
}
func didLogin(type: STSocialType, withError error: Error?) {
/// Reload data
collectionView.reloadData()
}
- 在
Info.plist
中设置YouTube回调URL方案
Facebook其他配置
1. Right-click your `.plist` file and choose "Open As Source Code".
2. Copy & Paste the XML snippet into the body of your file ( <dict>...</dict> ).
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>BUNDLE_SCHEME</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>ID</string>
<key>FacebookDisplayName</key>
<string>DISPLAY_NAME</string>
3. As we use Facebook dialogs (e.g., Login, Share, App Invites, etc.) that can perform an app switch to Facebook apps, your application's .plist also need to handle this.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
4. For a quickstart help, click
身份验证流程
我们将使用FBSDKLoginKit
进行一次性身份验证。
Instagram 使用了 OAuth2
协议,因此我们使用 OAuthSwift
进行身份验证。我们将初始的 access_token
存储在密钥链中,它将被重复使用且没有过期日期。
YouTube/Google
Google 使用了 OAuth2
协议,因此我们使用 OAuthSwift
进行身份验证。一旦用户首次进行身份验证,我们将 refresh_token
存储在密钥链中,它可以被重复使用来获取一个没有过期日期的新令牌。如果用户撤销了应用程序的访问权限,refresh_token
也将被撤销,用户将需要重新进行身份验证。
注意:请确保已启用密钥链共享
社交操作
STLike
对象
获取 STSocialManager.shared.getLike(objectID: [POST_ID], forType: [STSocialType], handler: {
[weak self] (likeObject, error) in
DispatchQueue.main.async(execute: {
if error == nil, let _likeObject = likeObject {
// Setting like/unlike icon and like count
} else {
// Error
})
})
STComment
对象
获取 STSocialManager.shared.getComment(objectID: [POST_ID], forType: [STSocialType], handler: {
[weak self] (commentObject, error) in
if error == nil, commentObject != nil {
// Setting comment object
} else {
// Error
}
})
点赞
检查当前帖子 hasLiked
& canLike
。
guard let hasLiked = likeObject?.hasLiked,
(likeObject?.canLike)! else { return }
点赞一篇文章
STSocialManager.shared.like(postID: [POST_ID], forSocialType: [STSocialType], handler: {
[weak self, id = post!.id] (success: Bool, error: Error?) in
DispatchQueue.main.async(execute: {
if success {
// Set current likeObject
self?.likeObject?.hasLiked = true
/// Set lke icon to like
} else {
// Failed to like the post
}
})
})
取消点赞一篇文章
STSocialManager.shared.unlike(postID: [POST_ID], forSocialType: [STSocialType], handler: {
[weak self, id = post!.id] (success: Bool, error: Error?) in
DispatchQueue.main.async(execute: {
if success {
// Set current likeObject
self?.likeObject?.hasLiked = true
/// Set lke icon to like
} else {
// Failed to like the post
}
})
})
转发一篇文章
// Check if user `canComment`
guard let canComment = commentObject?.canComment else { return }
if canComment {
/// Post comment
STSocialManager.shared.postComment(forObjectId: [POST_ID], type: [STSocialType], withLocalizedStrings: nil)
}
这将为用户显示一个标准弹出对话框 textField
来进行评论。
您可以使用本地化字符串对象 STLocalizedCommentStrings
。
如果您想设置自己的自定义评论对话框,可以使用
post(comment: [COMMENT], forObjectId: [POST_ID], type: [STSocialType])
YouTube
在YouTube视频上发布评论时,我们需要传递频道
STSocialManager.shared.postComment(channel: [CHANNEL_ID], withObjectID: [POST_ID], type: [STSocialType], withLocalizedStrings: nil)
// Or with a custom pop up dialog
STSocialManager.shared.post(toChannel: [CHANNEL_ID], comment: [COMMENT], withObjectID: [POST_ID], type: [STSocialType])
分享一篇文章
使用此函数可以
do {
try STSocialManager.shared.share(postLink: likeObject?.shareLink ?? "", forType: type, localizedStrings: nil, withPostTitle: post!.author.name, postText: post!.text, postImageURL: post!.image, image: postImage.image)
} catch STSocialError.shareError(let message) {
print(message)
} catch {
print(error.localizedDescription)
}
如果未在 ViewController
中设置目标,则此函数将抛出 STSocialError.shareError
。
每个服务提供不同的分享功能
Facebook将分享likeObject?.shareLink
、标题和帖文图片。如果Facebook的iOS SDK可用,将使用标准分享,否则将使用FBSDKShareKit
。
YouTube
使用YouTube分享功能,我们将使用标准iOS UIActivityViewController
,用户可以选择分享视频缩略图或视频链接。
对于本地化的actionSheet
,您可以传递STLocalizedShareStrings
。
使用Instagram,我们将使用标准iOS UIActivityViewController
,用户可以选择分享Instagram图片或帖文链接。
对于本地化的actionSheet
,您可以传递STLocalizedShareStrings
。
操作管理
在viewDidDisappear
中取消所有操作。这将取消在队列中获取STLike
和STComment
对象。
STSocialManager.shared.cancelAllOperations()
在UICollectionViewDelegate
的didEndDisplaying cell
中取消单个操作。
// Canceling an operation task
STSocialManager.shared.cancelOperation(forPostID: [POST_ID], operation: .like)
STSocialManager.shared.cancelOperation(forPostID: [POST_ID], operation: .comment)
注销登录
简单调用STSocialManager.shared.logout
以从所有服务中注销用户。
依赖项
StanwoodSocial
附带多个库。
# iOS Frameworks
s.frameworks = 'Social'
#CocoaPods Frameworks
s.dependency 'OAuthSwift', '~> 1.1.0'
s.dependency 'Locksmith', '~> 3.0.0'
s.dependency 'ObjectMapper', '~> 2.2.1'
s.dependency 'FBSDKCoreKit', '~> 4.17.0'
s.dependency 'FBSDKShareKit', '~> 4.17.0'
s.dependency 'FBSDKLoginKit', '~> 4.17.0'
许可证
StanwoodSocial 在 MIT 许可证下。有关更多信息,请参阅 LICENSE 文件。