苹果原生框架的分享

版本记录

版本号 时间
V1.0 2017.07.19

前言

在app中,都有分享的功能,我们可以直接接入各种想要分享的平台进行分享,还有就是可以利用第三方分享平台进行分享,主要的第三方分享平台有ShareSDK,还有就是百度的组件分享等,这里我们就说一下苹果原生框架的分享。

功能需求

下面我们就用苹果的原生框架进行分享。


功能实现

苹果原生分享,主要涉及的类是SLComposeViewController,涉及的框架是#import ,下面我们先看一下类SLComposeViewController

typedef NS_ENUM(NSInteger, SLComposeViewControllerResult) {
    SLComposeViewControllerResultCancelled,
    SLComposeViewControllerResultDone
};

typedef void (^SLComposeViewControllerCompletionHandler)(SLComposeViewControllerResult result); 

// Although you may perform requests on behalf of the user, you cannot append
// text, images, or URLs without the user's knowledge. Hence, you can set the
// initial text and other content before presenting the view to the user, but
// cannot change the content after the user views it. All of the methods used
// to set the content return a Boolean value. The methods return NO if the
// content doesn't fit or if the view was already presented to the user and the
// content can no longer be changed.

SOCIAL_CLASS_AVAILABLE(NA, 6_0)
@interface SLComposeViewController : UIViewController

+ (BOOL)isAvailableForServiceType:(NSString *)serviceType;

+ (SLComposeViewController *)composeViewControllerForServiceType:(NSString *)serviceType;

@property(nonatomic, readonly) NSString *serviceType;

// Sets the initial text to be posted. Returns NO if the sheet has already been
// presented to the user. On iOS 6.x, this returns NO if the specified text
// will not fit within the character space currently available; on iOS 7.0 and
// later, you may supply text with a length greater than the service supports,
// and the sheet will allow the user to edit it accordingly.
- (BOOL)setInitialText:(NSString *)text;

// Adds an image to the post. Returns NO if the additional image will not fit
// within the character space currently available, or if the sheet has already
// been presented to the user.
- (BOOL)addImage:(UIImage *)image;

// Removes all images from the post. Returns NO and does not perform an operation
// if the sheet has already been presented to the user. 
- (BOOL)removeAllImages;


// Adds a URL to the post. Returns NO if the additional URL will not fit
// within the character space currently available, or if the sheet has already
// been presented to the user.
- (BOOL)addURL:(NSURL *)url;


// Removes all URLs from the post. Returns NO and does not perform an operation
// if the sheet has already been presented to the user.
- (BOOL)removeAllURLs;


// Specify a block to be called when the user is finished. This block is not guaranteed
// to be called on any particular thread. It is cleared after being called.
@property (nonatomic, copy) SLComposeViewControllerCompletionHandler completionHandler;
@end

下面我们再看一下可以分享到哪个平台。

#import 
#import 

SOCIAL_EXTERN NSString *const SLServiceTypeTwitter NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeFacebook NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeSinaWeibo NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeTencentWeibo NS_AVAILABLE(10_9, 7_0);
SOCIAL_EXTERN NSString *const SLServiceTypeLinkedIn NS_AVAILABLE(10_9, NA);

由上可知,苹果原生分享相对闭塞,只可以分享到FacebookTwitterSinaWeiboTencentWeiboLinkedIn这五个平台。

下面我们就看一下代码,以分享到新浪微博为例。

#import "JJOCVC.h"
#import 

@interface JJOCVC ()

@end

@implementation JJOCVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self shareAction];
}

#pragma mark - Object Private Function

- (void)shareAction
{
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        NSLog(@"服务不可用");
        return;
    }
    
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    [composeVC setInitialText:@"来看我的直播节目哦"];
    [composeVC addImage:[UIImage imageNamed:@"plcaeholder"]];
    [composeVC addURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1140440766"]];
    
    composeVC.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
           NSLog(@"开始发送...");
        }
    };

    //显示编辑视图
    [self presentViewController:composeVC animated:YES completion:nil];
}

@end




功能验证

下面我们就看一下功能实现情况。

苹果原生框架的分享_第1张图片
新浪微博原生分享

这里还可以点击选择分享位置。

其实分享的这五个平台在苹果的设置中就有所体现,下面看下图。

苹果原生框架的分享_第2张图片
设置界面

所以在设置界面,要设置好要分享平台的账号,要不apple也不会知道你要往哪里分享,就算你不设置,在刚才那个新浪分享界面,你点击发布也一样弹出alert,让你去添加账号。

可以看出来,苹果的原生分享相对闭塞,所以现在都是自己接分享平台去分享。

后记

苹果的原生分享不怎么好用,这里就大家了解下就可以了。

苹果原生框架的分享_第3张图片
蒲公英

你可能感兴趣的:(苹果原生框架的分享)