前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS8新特性扩展(Extension)应用之二——分享插件

iOS8新特性扩展(Extension)应用之二——分享插件

作者头像
珲少
发布2018-08-16 11:07:11
6770
发布2018-08-16 11:07:11
举报
文章被收录于专栏:一“技”之长一“技”之长

iOS8新特性扩展(Extension)应用之二——分享插件

        在上一篇博客中,介绍了iOS8新特性扩展功能之一的Today功能:http://my.oschina.net/u/2340880/blog/485533,这里我们再介绍一下分享的扩展功能。

      在iOS8之前,除了一些主流的社交平台,例如苹果支持内容分享外,其他开发者的应用若要加入分享的功能,将会十分的复杂。在iOS8的新特性中,apple为我们准备了这样的扩展功能。

首先创建工程,在我们的工程中新建一个Target:

之后,模板中会为我们创建一个controller类,这个controller用于控制我们的分享插件,里面内容:

代码语言:javascript
复制
@implementation ShareViewController
//这个函数用于判断分享内容的可用性,我们在其中获取分享的内容进行检查
- (BOOL)isContentValid {
    // Do validation of contentText and/or NSExtensionContext attachments here
    return YES;
}
//点击post按钮后出发的方法,我们可以在这里将分享的内容进行上传等操作
- (void)didSelectPost {
    // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
    
    // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
    [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
//这里用于设置分享插件的附件按钮
- (NSArray *)configurationItems {
    // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
    return @[];
}

@end

除此之外,还有一些常用的属性:

- (void)presentationAnimationDidFinish;

弹出视图动画结束后执行的方法

@property (readonly, NS_NONATOMIC_IOSONLY) NSString *contentText;

分享的内容文字

@property (copy, NS_NONATOMIC_IOSONLY) NSString *placeholder;

默认显示的提示文字

- (void)didSelectCancel;

取消按钮执行的方法

我们在代码中如下添加后运行:

代码语言:javascript
复制
@implementation ShareViewController
-(NSString *)placeholder{
    return @"提示文字";
}
- (NSArray *)configurationItems {
    // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
    SLComposeSheetConfigurationItem * item =[[SLComposeSheetConfigurationItem alloc]init];
    item.title=@"地点";
    item.value=@"城门";
    return @[item];
}

我们用系统的相册做测试,点击相片的分享按钮:

点击MORE,添加我们的扩展插件。

这时分享栏中多了一个我们的插件,点击效果如下:

还有一点我们需要了解,在这个扩展的plist文件中,有这样一个键:NSExtensionAttributes,里面有一个NSExtensionActivationRule的字典,其中可以设置一些键值,对分享插件的属性进行控制。

这些键的写法在官方文档中的介绍如下:

这些键的意义,文档中介绍的很清楚,我们可以根据需要进行设置。

专注技术,热爱生活,交流技术,也做朋友。 ——珲少 QQ群:203317592

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/07/30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iOS8新特性扩展(Extension)应用之二——分享插件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档