美文网首页
iOS分享到第三方应用方法整理

iOS分享到第三方应用方法整理

作者: 草莓味辣妹 | 来源:发表于2019-03-01 14:53 被阅读0次

    分享到Facebook

    在iOS应用内将内容分享到Facebook,可通过接入Facebook SDK实现。官方网站:https://developers.facebook.com/docs/sharing/ios
    接入Facebook SDK这里不做介绍。
    可分享的内容为链接、图片、视频、多媒体(图片+视频),还可为分享的视频添加话题。
    每一种分享内容都有对应的shareContent类型,并通过shareDialog分享到Facebook

    分享链接:

    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content.contentURL = [NSURL URLWithString:@"https://developers.facebook.com"];
    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.content = content;
    dialog.mode = FBSDKShareDialogModeShareSheet;
    [dialog show];
    

    分享图片

    • 图片必须小于12MB
    • 可分享多张图片
    NSMutableArray* photos = [[NSMutableArray alloc] initWithCapacity:images.count];
    for (UIImage* image in images) {
         FBSDKSharePhoto* photo = [[FBSDKSharePhoto alloc] init];
         photo.image = image;
         photo.userGenerated = YES;
            
         [photos addObject:photo];
    }
        
    FBSDKSharePhotoContent* content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = photos;
    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.content = content;
    dialog.mode = FBSDKShareDialogModeShareSheet;
    [dialog show];
    

    分享本地视频

    • 视频大小必须小于 50MB。
    FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
    video.videoURL = url;   //亲测该URL必须是相册视频URL,如果是[NSURL fileURLWithPath:filePath],分享不了,必须写入相册
    FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
    content.video = video;
    content.hashtag = [FBSDKHashtag hashtagWithString:hashTag];  //还可加上话题,字符串必须带有#,只能带一个话题
    

    同时分享图片和视频

    • 用户使用的 iOS 版本至少应为 7.0。
    • 分享内容的用户应安装版本 52.0 或以上的 iOS 版 Facebook 客户端。
    • 照片大小必须小于 12MB,视频大小必须小于 50MB。
    • 用户最多可以分享 1 个视频加 29 张照片,或最多分享 30 张照片。
    FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWith...
    FBSDKShareVideo *video = [FBSDKShareVideo videoWith...
    FBSDKShareMediaContent *content = [FBSDKShareMediaContent new];
    content.media = @[photo, video];
    

    分享到WhatsApp

    分享内容到WhatsApp,只能通过openURL和原生系统分享的方式,官方网站:https://faq.whatsapp.com/en/iphone/23559013

    分享文字

    • 分享文字使用的是openURL的方式,如果文字中带有链接,也能识别出是链接
    NSString *urlString = [NSString stringWithFormat:@"whatsapp://send?text=%@", encodeText];
    BOOL ret = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    if (ret) {
    
    } else {
      NSLog("failed to share");
    }
    

    分享图片

    分享图片只能使用iOS的系统分享,采用UIDocumentInteractionController

    NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:savePath atomically:YES];
    UIDocumentInteractionController *documentInteractionController =
                [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    documentInteractionController.UTI = @"net.whatsapp.image";
    documentInteractionController.delegate = self;
    BOOL success = [documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:viewController.view animated:YES];
    

    分享本地视频

    • 除了文字之外,分享其他内容到WhatsApp只能调用系统分享,以URL的形式
    • 不知道为啥UIDocumentInteractionController 分享不了视频,只能用UIActivityViewController了
    NSMutableArray* items = [NSMutableArray new];
    NSURL *url =  [NSURL fileURLWithPath:filePath];   //filePath为是本地视频路径,可以是沙盒路径,也可以是文件目录路径
    if (url) {
          [items addObject:url];
    }
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    activityVC.excludedActivityTypes = @[ UIActivityTypePostToFacebook, UIActivityTypePostToTwitter ];
    if ([SystemInfo isPad]) {
       if ([activityVC respondsToSelector:@selector(popoverPresentationController)]) {
              // iOS8
           activityVC.popoverPresentationController.sourceView = viewController.view;
           activityVC.popoverPresentationController.sourceRect =
           CGRectMake(viewController.view.frame.size.width * 4 / 5, viewController.view.frame.size.height - 80, 0, 0);
           activityVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
        }
    }
    

    分享到Instagram

    分享图片

    分享图片通过存入相册再分享

    分享视频

    分享视频到Instagram,必须通过相册URL

    NSURL *movieURL = [NSURL fileURLWithPath:filePath];  //filePath为本地视频的路径
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:movieURL completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error || !assetURL) {
            NSLog(@"write video to album error");
        }
        NSString *assetPath = [[assetURL absoluteString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
        NSString *captionItem = [caption stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
        NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@", assetPath, captionItem]];            
        BOOL ret = [[UIApplication sharedApplication] openURL:instagramURL];
        if (ret) {
        } else {
            NSLog("failed to share");
        }
    }];
    

    分享到VK

    在iOS应用内分享内容到VK,由VKsdk支持,sdk支持分享链接、文字和图片,通过VKShareDialogController进行交互,官方网站:https://vk.com/dev/ios_sdk
    而本地视频只能通过系统原生分享的方式,传入相册视频url进行分享

    通过sdk进行分享

    VKShareDialogController * shareDialog = [VKShareDialogController new]; 
    shareDialog.text = @"This post created using #vksdk #ios"; 
    shareDialog.vkImages = @[@"-10889156_348122347",@"7840938_319411365",@"-60479154_333497085"]; 
    shareDialog.shareLink = [[VKShareLink alloc] initWithTitle:@"Super puper link, but nobody knows" link:[NSURL URLWithString:@"https://vk.com/dev/ios_sdk"]]; 
    [shareDialog setCompletionHandler:^(VKShareDialogControllerResult result) { 
        [self dismissViewControllerAnimated:YES completion:nil]; 
    }]; 
    [self presentViewController:shareDialog animated:YES completion:nil]; 
    

    相关文章

      网友评论

          本文标题:iOS分享到第三方应用方法整理

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