ios 自帶UIActivityViewController分享

UIActivityViewController 使用大致使用步驟:

  • //原生分享

    -(void)share{

        //1.設定分享的內容,比如:

        NSString *shareTitle = @"隱祕的角落";

        UIImage *shareImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593694996876&di=cb27080027a6748f99ca4ac4a49fee29&imgtype=0&src=http%3A%2F%2Fimagepphcloud.thepaper.cn%2Fpph%2Fimage%2F74%2F157%2F824.jpg"]]];

        NSURL *shareUrl = [NSURL URLWithString:@"https://www.iqiyi.com/v_2ffkws0bgr0.html?vfm=2008_aldbd&fv=p_02_01"];

        NSArray *activityItems = @[shareTitle,

                                   shareImage,

                                   shareUrl]; // 必須要提供url 纔會顯示分享標籤否則只顯示圖片

        //2.創建分享的控制器

        UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];

        

        //3.設定不想顯示的平臺和功能

        activityVC.excludedActivityTypes = [self excludetypes];

        

        //4. 設置操作回調,用戶點擊 菜單按鈕後事件執行完成會回調這個block

        activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {

            if (completed) {

                NSLog(@"分享成功");

            }else{

                NSLog(@"分享失敗");

            }

     

        };

        

        //5.顯示分享菜單

        [self presentViewController:activityVC animated:YES completion:nil];

    }

     

    -(NSArray *)excludetypes{

        NSMutableArray *excludeTypeM = [NSMutableArray arrayWithArray:@[//UIActivityTypePostToFacebook,

        UIActivityTypePostToTwitter,

        UIActivityTypePostToWeibo,

        UIActivityTypeMessage,

        UIActivityTypeMail,

        UIActivityTypePrint,

        UIActivityTypeCopyToPasteboard,

        UIActivityTypeAssignToContact,

        UIActivityTypeSaveToCameraRoll,

        UIActivityTypeAddToReadingList,

        UIActivityTypePostToFlickr,

        UIActivityTypePostToVimeo,

        UIActivityTypePostToTencentWeibo,

        UIActivityTypeAirDrop,

        UIActivityTypeOpenInIBooks]];

        if (@available(iOS 11.0, *)) {

            [excludeTypeM addObject:UIActivityTypeMarkupAsPDF];

        } else {

            // Fallback on earlier versions

        }

        return excludeTypeM;

    }

  • 示意圖:



 

下面我們再來看一個第三方的分享:WYShareSDK

如果是用cocoapods管理工具管理項目的話,只需要在Podfile裏添加一句 pod 'WYShareSDK' 就可以了。

根據上面我們所說的,原生的分享不需要第三方平臺的appKey和URL Schemes,那麼第三方的分享就需要了,好了,下面我們就來做一些項目的準備工作:

【1】在Xcode中,選擇你的工程設置項,選中TARGETS一欄,在info標籤欄的URL type添加URL scheme 爲你在各大平臺所註冊應用程序的id (如下圖所示)

【2】針對iOS9的適配.右擊項目中的info.plist,然後用Open as -> Source Code的方式打開,增加如下代碼:
 

<key>LSApplicationQueriesSchemes</key>
 <array>
  <string>wechat</string>
  <string>weixin</string>
  <string>sinaweibohd</string>
  <string>sinaweibo</string>
  <string>sinaweibosso</string>
  <string>weibosdk</string>
  <string>weibosdk2.5</string>
  <string>mqqapi</string>
  <string>mqq</string>
  <string>mqqOpensdkSSoLogin</string>
  <string>mqqconnect</string>
  <string>mqqopensdkdataline</string>
  <string>mqqopensdkgrouptribeshare</string>
  <string>mqqopensdkfriend</string>
  <string>mqqopensdkapi</string>
  <string>mqqopensdkapiV2</string>
  <string>mqqopensdkapiV3</string>
  <string>mqzoneopensdk</string>
  <string>wtloginmqq</string>
  <string>wtloginmqq2</string>
  <string>mqqwpa</string>
  <string>mqzone</string>
  <string>mqzonev2</string>
  <string>mqzoneshare</string>
  <string>wtloginqzone</string>
  <string>mqzonewx</string>
  <string>mqzoneopensdkapiV2</string>
  <string>mqzoneopensdkapi19</string>
  <string>mqzoneopensdkapi</string>
  <string>mqzoneopensdk</string>
 </array>
<!--  讓iOS9 退回到以前可以支持http請求  -->
 <key>NSAppTransportSecurity</key>
 <dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
 </dict>

【3】選中項目設置,在Build Settings中的Other Linker Flags中增加-fobj-arc-ObjC

這時編譯一下你的項目,如果沒有報錯,恭喜你環境搭建成功


接下來.分享的代碼使用 在AppDelegate中註冊 微信/QQ/和微博

在你需要分享的地方直接導入WYShareSDK.h,調用其對應的方法分享到各個平臺.例:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章