swift 共享功能在 iphone 中有效,但在 ipad2 中出错

swift share function works in iphone but error in ipad2

我将我的共享功能放入 ShareViewController.swift 文件中,它 运行 在 iPhone 模拟器中非常完美,但是当我 运行 它时它会出错并关闭模拟器在 iPad2 模拟器中。 请帮忙回答。每一步。谢谢!

import UIKit

class ShareViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let firstActivityItem = "share it!"

        let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)

        self.presentViewController(activityViewController, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

错误信息:

2015-07-16 13:35:06.293 pages[2539:46687] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x797fb2c0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0027c746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01d10a97 objc_exception_throw + 44
    2   UIKit                               0x0135aee2 -[UIPopoverPresentationController presentationTransitionWillBegin] + 3086
    3   UIKit                               0x00c71124 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1549
    4   UIKit                               0x00c6f1f7 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 198
    5   UIKit                               0x00ca42cb __40+[UIViewController _scheduleTransition:]_block_invoke + 18
    6   UIKit                               0x00b5d812 ___afterCACommitHandler_block_invoke + 15
    7   UIKit                               0x00b5d7bd _applyBlockToCFArrayCopiedToStack + 415
    8   UIKit                               0x00b5d5d2 _afterCACommitHandler + 549
    9   CoreFoundation                      0x0019d86e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    10  CoreFoundation                      0x0019d7b0 __CFRunLoopDoObservers + 400
    11  CoreFoundation                      0x001931ea __CFRunLoopRun + 1226
    12  CoreFoundation                      0x00192a5b CFRunLoopRunSpecific + 443
    13  CoreFoundation                      0x0019288b CFRunLoopRunInMode + 123
    14  GraphicsServices                    0x03f1e2c9 GSEventRunModal + 192
    15  GraphicsServices                    0x03f1e106 GSEventRun + 104
    16  UIKit                               0x00b330b6 UIApplicationMain + 1526
    17  pages                               0x0004eb74 main + 180
    18  libdyld.dylib                       0x02406ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

试试这个示例代码:

 @IBAction func ShareIt(sender: AnyObject) {

    let things = ["Things to share"]
    let avc = UIActivityViewController(activityItems:things, applicationActivities:nil)
    avc.setValue("Subject title", forKey: "subject")
    avc.completionWithItemsHandler = {
        (s: String!, ok: Bool, items: [AnyObject]!, err:NSError!) -> Void in
    }

    self.presentViewController(avc, animated:true, completion:nil)
    if let pop = avc.popoverPresentationController {
        let v = sender as! UIView // sender would be the button view tapped, but could be any view
        pop.sourceView = v
        pop.sourceRect = v.bounds
    }
}