2015-09-23 11 views
14

Tôi có đoạn code dưới đây để chia sẻ hình ảnh trên instagram từ ứng dụng Swift của tôi: @IBAction func instagramShareButton (sender: AnyObject) {Swift sụp đổ wen khai thác “Open in Instagram” khi sử dụng UIDocumentInteractionController

let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString 
    let path = documentsDirectory.stringByAppendingPathComponent("Share Icon.igo") 

    let imageName: String = "Share Icon.png" 
    let image = UIImage(named: imageName) 
    let data = UIImagePNGRepresentation(image!) 

    data!.writeToFile(path, atomically: true) 

    let imagePath = documentsDirectory.stringByAppendingPathComponent("Share Icon.igo") 
    let rect = CGRectMake(0, 0, 0, 0) 

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0) 
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    UIGraphicsEndImageContext() 

    let fileURL = NSURL(fileURLWithPath: imagePath) 
    print("fileURL = \(fileURL)") 

    var interactionController = UIDocumentInteractionController(URL: fileURL) 

    interactionController.delegate = self 

    interactionController.UTI = "com.instagram.exclusivegram" 

    let msgBody = "My message" 
    interactionController.annotation = NSDictionary(object: msgBody, forKey: "InstagramCaption") 
    interactionController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true) 
} 

func documentInteractionControllerWillPresentOpenInMenu (bộ điều khiển: UIDocumentInteractionController) { }

Mã được dịch từ Objective C sang Swift bởi tôi, vì tôi không tìm thấy gì trong Swift để chia sẻ trên Instagram.

Menu bật lên, tôi thấy instagram ở đó và khi tôi chạm vào nó, tôi nhận được báo lỗi dưới đây:

Assertion failure in -[_UIOpenWithAppActivity performActivity], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.16/UIDocumentInteractionController.m:408

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController has gone away prematurely!'

Tôi tin rằng tôi bằng cách nào đó phải thả đối tượng UIDocumentInteractionController. Tôi có đúng không? Không tìm thấy bất kỳ thông tin nào để giúp tôi hiểu cách tôi có thể thực hiện điều này trong Swift. Xin hãy giúp tôi tìm ra cách tôi có thể giải quyết vấn đề này.

+0

điều này có thể là cùng một vấn đề mà tôi phải đối mặt trong khi trở lại cố gắng làm điều tương tự. Tôi không bao giờ tìm thấy một giải pháp, nhưng đã có thể thực hiện một cách giải quyết thỏa mãn ứng dụng của tôi. Bạn có thể xem bài đăng của tôi ở đây: [link] (http://stackoverflow.com/questions/29065772/ios-uidocumentinteractioncontroller-launchservices-invalidationhandler-called) – Lavvo

Trả lời

41

Tôi nghĩ bạn đúng. Tôi có cùng một vấn đề.

trước khi bắt đầu các chức năng chia sẻ, bạn phải tạo một biến toàn cầu từ UIDocumentInteractionController:

var interactionController: UIDocumentInteractionController? 
@IBAction func instagramShareButton(sender: AnyObject) { 
    ... 
    interactionController = UIDocumentInteractionController(URL: fileURL) 
    interactionController!.UTI = "com.instagram.exclusivegram" 
    let msgBody = "My message" 
    interactionController!.annotation = NSDictionary(object: msgBody, forKey: "InstagramCaption") 
    interactionController!.presentOpenInMenuFromRect(rect, inView: self.view, animated: true) 
} 

này làm việc cho tôi!

+0

Cảm ơn câu trả lời của bạn. Nó thực sự làm việc với biến UIDocumentInteractionController toàn cục. Hóa ra rằng ở phần cuối của chương trình, nó không còn biết những gì interactController là, do đó, nó bị treo. – asheyla

+2

Điều này xảy ra vì cá thể 'UIDocumentInteractionController' là cần thiết khi người dùng chọn bất kỳ ứng dụng nào được bật trong bộ điều khiển. Nó bị treo vì bộ điều khiển được phát hành khi người dùng chạm vào ứng dụng. Chúng ta cần biến toàn cầu ở đây để giữ cho bộ điều khiển tài liệu còn sống. Lưu ý khi xuất sang AirDrop, bộ điều khiển tài liệu không được sử dụng và có thể là biến cục bộ. –

+2

Với toàn cầu có nghĩa là một biến lớp toàn cầu, btw, như nhiều hơn hoặc ít hơn được hiển thị trong mã ở trên. –

Các vấn đề liên quan