看板 KnucklesNote
作者 標題 [Xcode][Swift3] 使用 ActivityViewController 分享至其他程式
時間 2017-05-09 Tue. 20:59:43
使用 ActivityViewController 開啟 iOS 的分享功能
在 Navigation Bar 加上一個 Bar Button Item
在屬性檢視器將 System Item 設為 Action
使用 Assisant Editor 加上 @IBAction
名稱輸入「share」
修改 @IBAction 的內容為
@IBAction func share(_ sender: Any) {
guard textTitle != nil, boardName != nil, boardId != nil, textId != nil
else { return }
let shareTitle = "\(textTitle!) - \(boardName!)板 - DispBBS"
let urlString = "https://disp.cc/b/\(boardId!)-\(textId!)"
let shareUrl = NSURL(string: urlString)!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [shareTitle, shareUrl], applicationActivities: nil)
// 要加這行,不然在iPad會閃退
activityViewController.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
// 如果有不想列出來的分享選項,加上下面這行可排除
//activityViewController.excludedActivityTypes = [ .postToWeibo, .print, .assignToContact, .saveToCameraRoll, .addToReadingList, .postToFlickr, .postToVimeo, .postToTencentWeibo ]
self.present(activityViewController, animated: true, completion: nil)
}
guard textTitle != nil, boardName != nil, boardId != nil, textId != nil
else { return }
let shareTitle = "\(textTitle!) - \(boardName!)板 - DispBBS"
let urlString = "https://disp.cc/b/\(boardId!)-\(textId!)"
let shareUrl = NSURL(string: urlString)!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [shareTitle, shareUrl], applicationActivities: nil)
// 要加這行,不然在iPad會閃退
activityViewController.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
// 如果有不想列出來的分享選項,加上下面這行可排除
//activityViewController.excludedActivityTypes = [ .postToWeibo, .print, .assignToContact, .saveToCameraRoll, .addToReadingList, .postToFlickr, .postToVimeo, .postToTencentWeibo ]
self.present(activityViewController, animated: true, completion: nil)
}
在 iPad 會使用 popoverPresentationController 來顯示
--
※ 作者: Knuckles 時間: 2017-05-09 20:59:43
※ 編輯: Knuckles 時間: 2017-05-09 21:20:34
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 272
回列表(←)
分享