iOS ShareExtension
使用系统分享。将Safari中的网页分享给微信中的好友。
0.gif
1.png 2.png 4.png 5.png 6.png 3.png1.新建ShareExtension。
2.配置Share Extension,允许发送的数据类型,url,image,mp3,mp4,pdf,word,excel,ppt。
7.png
3.处理Share Extension中的数据。
Share Extension中默认都会有一个数据展现的UI界面。该界面继承SLComposeServiceViewController这个类型,如:
@interface ShareViewController : SLComposeServiceViewController
@end
10.gif
一般采用自定义控制器:
@interface ShareViewController : SLComposeServiceViewController
@end
8.png
11.gif
4.从inputItems中获取数据。
[self.extensionContext.inputItems enumerateObjectsUsingBlock:^(NSExtensionItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.attributedContentText.string.length > 0)
{
self.contentText = obj.attributedContentText.string;
}
[obj.attachments enumerateObjectsUsingBlock:^(NSItemProvider * _Nonnull itemProvider, NSUInteger idx, BOOL * _Nonnull stop) {
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.url"])
{
[itemProvider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSURL class]])
{
self.url = (NSURL *)item;
NSInteger preValue = self.flag.integerValue;
if ([self.url isFileURL])
{
self.flag = [NSNumber numberWithInteger:(preValue|url_file)];
}
else
{
self.flag = [NSNumber numberWithInteger:(preValue|url_flag)];
}
}
[self refreshView];
}];
}
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.image"])
{
[itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:^(id item, NSError *error) {
if ([item isKindOfClass:[NSURL class]])
{
[self.arrImagePath addObject:item];
NSInteger preValue = self.flag.integerValue;
self.flag = [NSNumber numberWithInteger:(preValue | url_image)];
[self refreshView];
count ++;
}
else if ([item isKindOfClass:[UIImage class]] && !self.thumb)
{
self.thumb = (UIImage *)item;
NSInteger preValue = self.flag.integerValue;
self.flag = [NSNumber numberWithInteger:(preValue | url_image)];
[self refreshView];
count ++;
}
}];
}
if ([itemProvider hasItemConformingToTypeIdentifier:(__bridge NSString *)kUTTypeText])
{
NSInteger preValue = self.flag.integerValue;
self.flag = [NSNumber numberWithInteger:(preValue|url_text)];
[itemProvider loadItemForTypeIdentifier:(__bridge NSString *)kUTTypeText options:nil completionHandler:^(id item, NSError *error) {
if ([item isKindOfClass:[NSString class]])
{
NSString *str = (NSString *)item;
if ([str containsString:@"http://"] || [str containsString:@"https://"] || [str containsString:@"file:///"])
{
if (!self.url)
{
self.url = [NSURL URLWithString:str];
if ([self.url isFileURL])
{
self.flag = [NSNumber numberWithInteger:(preValue|url_file)];
}
else
{
self.flag = [NSNumber numberWithInteger:(preValue|url_flag)];
}
}
}
else
{
[self.text appendString:str];
[self.text appendString:@"\n"];
}
}
[self refreshView];
}];
}
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.movie"])
{
[itemProvider loadItemForTypeIdentifier:@"public.movie" options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error)
{
NSInteger preValue = self.flag.integerValue;
NSURL *fileurl = (NSURL *)item;
if ([fileurl isFileURL])
{
self.flag = [NSNumber numberWithInteger:(preValue | url_file)];
self.url = fileurl;
[self refreshView];
}
}];
}
}];
}];
上面的例子中遍历了extensionContext的inputItems数组中所有NSExtensionItem对象,然后从这些对象中遍历attachments数组中的所有NSItemProvider对象。匹配第一个包含public.url标识的附件(具体要匹配什么资源,数量是多少皆有自己的业务所决定)。注意:[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];这行代码,主要是使到视图控制器不被关闭,等到实现相应的处理后再进行调用该方法,对分享视图进行关闭。调用该方法则回到宿主App。
5.传递Share Extension中的数据。有个App Groups功能可以据此传递数据。
WX20190210-223758.png
WX20190210-224011.png
1.依据写文件传递数据。例如:要分享的App储存登录信息。
//获取分组的共享目录
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];
NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
if (success) {
[@"isLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
} else {
[@"isNotLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:Nil];
}
//获取储存在App Groups中的登录信息。
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];
NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
NSString *isLoginStatus = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
2.依据NSUserDefaults,储存数据,试了几次没有取成功过。
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.sharextension"];
if (![userDefaults objectForKey:@"isLogin"])
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
message:@"请登录"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIResponder* responder = self;
while ((responder = [responder nextResponder]) != nil)
{
if([responder respondsToSelector:@selector(openURL:)] == YES)
{
[responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
}
}
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}];
[alert addAction:cancelAction];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
会报错:
[User Defaults] Couldn't read values in CFPrefsPlistSource<0x1c010e340> (Domain:
group.cn.com.mengniu.oa.sharextension, User: kCFPreferencesAnyUser, ByHost:
Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers,
detaching from cfprefsd
网上说在储存App Groups时要添加Team ID。之后取值时虽然不会再报错,但取出来的值为nil。
6.其实苹果官方除了Today Extension外,其他Extension是不提供跳转接口的。所以这里总结的是两种非正常的方式。
1.在Share Extension中无法获取到UIApplication对象,则通过拼接字符串获取。
NSURL *destinationURL = [NSURL URLWithString:[NSString stringWithFormat:@"sharefile://%@",saveFilePath]];
// Get "UIApplication" class name through ASCII Character codes.
NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
if (NSClassFromString(className)) {
id object = [NSClassFromString(className) performSelector:@selector(sharedApplication)];
[object performSelector:@selector(openURL:) withObject:destinationURL];
}
2.这种方式主要实现原理是通过响应链找到Host App的UIApplication对象,通过该对象调用openURL方法返回自己的应用。
UIResponder *responder = self;
while ((responder = [responder nextResponder]) != nil) {
if ([responder respondsToSelector:@selector(openURL:)] == YES) {
[responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
}
}
12.gif
7.未登录的处理。登录成功后先写文件储存登录信息到App Groups中,退出登录后,删除储存在App Groups中的登录信息。到ShareViewController中先判断是否登录,若未登录,则弹窗提示登录不再弹起发送框。
1.登录成功,则保存登录信息。
//获取分组的共享目录
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];
NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
if (success) {
[@"isLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
} else {
[@"isNotLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:Nil];
}
});
2.退出登录或未登录,则清空已经保存的登录信息。
//获取分组的共享目录
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];
NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
[@"isNotLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];
NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
NSString *isLoginStatus = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
//如果未登录提示登录
if (isLoginStatus && [isLoginStatus isEqualToString:@"isNotLogin"]) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"请先登录办随,再分享"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIResponder* responder = self;
while ((responder = [responder nextResponder]) != nil)
{
if([responder respondsToSelector:@selector(openURL:)] == YES)
{
[responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
}
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}];
[alert addAction:cancelAction];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
else//已登录加载发送框
{
[self.view addSubview:container];
}
9.gif
8.在ShareExtension中处理逻辑代码。