iOS之share Extension

作者: ningning_ | 来源:发表于2016-03-25 12:02 被阅读1344次

    先上效果图。从系统自带的浏览器Safari打开一个网址,分享到自己的app  

    具体实现步骤:

    1.File->New->Target, Application Extension->Share Extension

    新建文件

    2.Product Name 是在分享菜单中显示的名字。

    新建文件

    3.

    文件建好效果

    但目前这个分享扩展什么都不能做,需要完善功能。

    4.先来分析ShareViewController这个类

    - (void)viewDidLoad

    - (BOOL)isContentValid

    - (void)didSelectPost

    - (NSArray *)configurationItems

    viewDidLoad这个方法我们就不用说了, isContentValid来判断我们获取到得数据是否是我们想要的。didSelectPost是选择之后的方法, configurationItems 只返回一个SLComposeSheetConfigurationItem 这是一个类似tableview的列表下面会看到效果.以上三步就能实现一个简单的share Extension  编译运行之后,我们会看到一个很简单的分享页面,当然现在我们还没有做任何东西,那么怎么把我们要分享的东西,或者其他操作和我们的应用关联呢或者上传到我们的服务器呢?

    直接上代码:


    shareViewController代码如下

    @interface ShareViewController ()

    @property (strong, nonatomic) UIWebView    *webView;

    @end

    @implementation ShareViewController

    - (BOOL)isContentValid {

    return YES;

    }

    - (void)didSelectPost {

    [self initWebView];

    [self parsePostUrl];

    }

    - (void)parsePostUrl {

    NSExtensionItem *extensionItem = self.extensionContext.inputItems.firstObject;

    NSItemProvider *itemProvider = extensionItem.attachments[0];

    NSUserDefaults *myDefaults = [[NSUserDefaults alloc]

    initWithSuiteName:@"group.com.yabadun.live"];

    [myDefaults setObject:self.contentText forKey:@"text"];

    [myDefaults synchronize];

    if ([itemProvider hasItemConformingToTypeIdentifier:(NSString*)kUTTypeURL]) {

    [itemProvider loadItemForTypeIdentifier:(NSString*)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {

    [self.extensionContext completeRequestReturningItems:@[] completionHandler:^(BOOL expired) {

    [self openAppWithURL:url.absoluteString text:self.contentText];

    }];

    }];

    }

    }

    - (NSArray *)configurationItems {

    return @[];

    }

    - (void)initWebView {

    self.webView = [[UIWebView alloc] init];

    [self.view addSubview:self.webView];

    }

    - (void)openAppWithURL:(NSString*)urlString text:(NSString*)text {

    UIResponder* responder = self;

    while ((responder = [responder nextResponder]) != nil) {

    if ([responder respondsToSelector:@selector(openURL:)] == YES) {

    [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:[NSString stringWithFormat:@"wolaiSocial://%@", [self urlStringForShareExtension:urlString text:text]]]];

    }

    }

    }

    - (NSString*)urlStringForShareExtension:(NSString*)urlString text:(NSString*)text {

    NSString* finalUrl=[NSString stringWithFormat:@"%@____%@", text, urlString];

    finalUrl =  (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(

    NULL,

    (CFStringRef)finalUrl,

    NULL,

    (CFStringRef)@"!*'();:@&=+$,/?%#[]",

    kCFStringEncodingUTF8 ));

    return finalUrl;

    }

    @end




    然后在appdelegate 实现如下

    #pragma mark Handle app open from share extention

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    if ([url.description hasPrefix:@"wolaiSocial"]) {

    [POIShareHelper shareFromURL:url];

    }

    }

    //iOS 9 及以上

    - (BOOL)application:(UIApplication *)app

    openURL:(NSURL *)url

    options:(NSDictionary *)options {

    if ([url.description hasPrefix:@"wolaiSocial"]) {

    [POIShareHelper shareFromURL:url];

    }

    }




    POIShareHelper主要实现如下:通过url,获取该网页的标题和图片分享到app内部的社区模块

    #import "POIHtmlParser.h"    //解析html内容

    #import "POIShareHelper.h"

    #import "POIUploaderHelper.h"  //图片处理

    #import "POIPublishRequest.h"  //发布到社区模块 

    #import "POIExploreViewController.h"

    @implementation POIShareHelper

    + (void)shareFromURL:(NSURL *)url{

    [self shareFromWolaiSocial:url];

    }

    + (void)shareFromWolaiSocial:(NSURL *)url{

    NSString *attrStr = [NSString poi_urlStringFromShareExtension:url.absoluteString];

    NSArray* attrArr = [attrStr componentsSeparatedByString:@"____"];

    if([attrArr count] > 1){

    NSString* text = attrArr[0];

    NSString* linkUrl = attrArr[1];

    POIHtmlParser *parser = [[POIHtmlParser alloc] initWithUrl:attrArr[1]];

    if(parser.imageUrl && ![parser.imageUrl isEqualToString:@""])

    {

    NSURL *url = [NSURL URLWithString:parser.imageUrl];

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

    [POIUploaderHelper uploadImage:image completion:^(id responseObject) {

    NSString* linkImage = @"";

    if ([responseObject isKindOfClass:[NSString class]])

    {

    linkImage = (NSString*)responseObject;

    }

    [self handleShareLinkImageUploadFinished:@{

    @"linkURL":linkUrl,

    @"text":text,

    @"linkTitle":parser.title,

    @"linkImage":linkImage

    }];

    } progress:^(float percent) {

    } failed:^{

    [self handleShareLinkImageUploadFinished:@{

    @"linkURL":linkUrl,

    @"text":text,

    @"linkTitle":parser.title,

    @"linkImage":@""

    }];

    }];

    }else{

    [self handleShareLinkImageUploadFinished:@{

    @"linkURL":linkUrl,

    @"text":text,

    @"linkTitle":parser.title,

    @"linkImage":@""

    }];

    }

    }

    }

    + (void)handleShareLinkImageUploadFinished:(id)object {

    NSString* linkURL = [object objectForKey:@"linkURL"];

    NSString* linkImage = [object objectForKey:@"linkImage"];

    NSString* linkTitle = [object objectForKey:@"linkTitle"];

    NSString* text = [object objectForKey:@"text"];

    [POIPublishRequest requestPublishWithUrl:linkURL text:text linkImage:linkImage linkTitle:linkTitle complete:^(NSInteger stastus) {

    [SVProgressHUD showSuccessWithStatus:@"分享成功!"];

    } failure:^{

    [MBProgressHUD showMessage:@"分享失败" afterDelay:2.0f];

    }]; 此处分享至社区模块代码

    }

    @end



    至于html解析与图片处理的类就不放了,方法有很多。仅供参考哈~ 有不对或有什么问题的地方欢迎纠正


    相关文章

      网友评论

      • 方克己:你这看着都不想看了
        ningning_:@哈哈汪 已经更新 http://www.jianshu.com/p/01c933254e66
      • 7c03aed0f01f:是有点累 看不下去…
        ningning_:@卡夫 已经更新 http://www.jianshu.com/p/01c933254e66
      • Ryan文濤:代码是可以用markdown格式化的,这样看的真心累...还不如直接上个Demo实际~~
        ningning_:@Ryan文濤 已经更新 http://www.jianshu.com/p/01c933254e66

      本文标题:iOS之share Extension

      本文链接:https://www.haomeiwen.com/subject/quarlttx.html