SLComposeViewController

1 SLComposeViewController

  1. Creating a Social Compose View Controller
  2. Checking the Social Service Type
  3. Composing Posts
  4. Handling Results

2 实战演练


1 SLComposeViewController

SLComposeViewController主要用于在当前应用开启分享界面,快速分享相关内容到其他APP或社交平台。如下所示

1.1 Creating a Social Compose View Controller

/// 根据扩展的Bundle identifier初始化
public init!(forServiceType serviceType: String!)

1.2 Checking the Social Service Type

/// 判断该分享扩展是否支持
public class func isAvailableForServiceType(serviceType: String!) -> Bool

/// 获取分享扩展的类型编码
public var serviceType: String! { get }

1.3 Composing Posts

/// 设置默认内容
public func setInitialText(text: String!) -> Bool

/// 添加图片
public func addImage(image: UIImage!) -> Bool

/// 清空所有图片
public func removeAllImages() -> Bool

/// 添加url链接
public func addURL(url: NSURL!) -> Bool

/// 清空所有链接
public func removeAllURLs() -> Bool

1.4 Handling Results

/// 分享结果监听
public var completionHandler: SLComposeViewControllerCompletionHandler!

2 实战演练

应用内打开分享扩展很简单,只需如下所示。

//
//  ViewController.swift
//  YJSocial
//
//  CSDN:http://blog.csdn.net/y550918116j
//  GitHub:https://github.com/937447974/Blog
//
//  Created by yangjun on 16/1/24.
//  Copyright © 2016年 阳君. All rights reserved.
//

import UIKit
import Social

/// 快速分享
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        // 分享到当前应用扩展(微博分享SLServiceTypeSinaWeibo)
        let serviceType = "com.YJSocial.ShareExtension" // 扩展Bundle identifier
        guard SLComposeViewController.isAvailableForServiceType(serviceType) else {
            print("不支持:\(serviceType)")
            return
        }
        let vc = SLComposeViewController(forServiceType: serviceType)
        vc.setInitialText(serviceType) // 默认内容
        // 处理结果回调
        vc.completionHandler =  {(result: SLComposeViewControllerResult) -> Void in
            switch result {
            case SLComposeViewControllerResult.Cancelled:
                print("Cancelled")
            case SLComposeViewControllerResult.Done:
                print("Done")
            }
        }
        self.presentViewController(vc, animated: true, completion: nil)
    }

}

 


Appendix

Sample Code

Swift

Social Framework Reference

SLComposeViewController Class Reference

Revision History

时间描述
2016-01-25博文完成

CSDN:http://blog.csdn.net/y550918116j

GitHub:https://github.com/937447974/Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值