顯示廣告
隱藏 ✕
看板 Knuckles_note
作者 Knuckles (站長 那克斯)
標題 [Xcode] iOS內建的分享功能 UIActivityViewController
時間 2015-03-30 Mon. 05:41:53


設定點了一個按鈕後,會出現 iOS 內建的分享功能


修改 XXXViewController.m

在 @interface XXXViewController () 這行下面加上
- (IBAction)shareTo:(id)sender;

在 @implement XXXViewController 這行後面加上
- (IBAction)shareTo:(id)sender{
    NSString *shareString = @"要分享的字串";
    UIImage *shareImage = [UIImage imageNamed:@"shareImage.jpg"];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:shareString, shareImage, nil] applicationActivities:nil];

    // fix iPad iOS8
    if ( [activityViewController respondsToSelector:@selector(popoverPresentationController)] ) {
        activityViewController.popoverPresentationController.sourceView = self.view;
    }

    [self presentViewController:activityViewController animated:YES completion:nil];
}


在 storyboard 的 XXXViewController 拉一個title為「分享至...」的 Button

到 ViewController 的連結設定
連結 Recieved Actions 的 shareTo: 到「分享至...」
[圖]

然後選擇「touch up inside」


在手機上點了分享按鈕
[圖]

選 Facebook
[圖]

會出現之前設定的shareString與shareImage


要把語系改成中文的話,在 Xcode 專案設定的 info
點一下 Localization native development region 後按 tab 鍵,選 Taiwan
[圖]


再回到 APP 點分享鈕,就會看到變成中文了
[圖]





參考:
https://cg2010studio.wordpress.com/2014/05/29/ios-活動視圖控制器-uiactivityviewcontroller/


◎ 錯誤解決記錄

在 iPad 使用 iOS8 時,點了「分享至...」的按鈕後會閃退
錯誤訊息:
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7ce8a0f0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'

解決方法:
參考 http://stackoverflow.com/questions/25644054/uiactivityviewcontroller-crashing-on-ios8-ipads 

在 [self presentViewController:activityViewController animated:YES completion:nil];
之前加上
    // fix iPad iOS8
    if ( [activityViewController respondsToSelector:@selector(popoverPresentationController)] ) {
        activityViewController.popoverPresentationController.sourceView = self.view;
    }


--
※ 作者: Knuckles 時間: 2015-03-30 05:41:53
※ 編輯: Knuckles 時間: 2015-10-19 12:02:06
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 1241 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇