2016-03-22 149 views
0

我試圖通過使用UIActivityViewController通過AirDrop在一臺iOS設備上將應用程序發送的數據(NSData)發送到另一臺iOS設備。我在我的應用程序plist中創建了一個新的CSM(自定義數據類型)。 public.filename-extension = ppm。 那麼如何將ppm擴展添加到我試圖發送的NSDate對象?我是否認爲當你展示一個UIActivityViewController時,如果發送的對象沒有我的應用程序公共擴展(ppm),那麼我的應用程序圖標將不會顯示在UIActivityViewController窗口中(ppm)?? ....是的,我是真的很困惑! 繼承人我使用呈現UIActivityViewController代碼通過UIActivityViewController發送自定義數據

@IBAction func shareButton(sender: AnyObject) { 

    // myData is the object I want to send to be used in my app on another device 

    let vc = UIActivityViewController(activityItems: [myData],applicationActivities: []) 
    presentViewController(vc, animated: true, completion: nil) 

    } 

基本上,所有我想要做的是發送自定義數據,在我的應用程序中使用

回答

3

你應該看看AirDrop sample code那涵蓋了定義自己的文件類型並與其他設備上的應用程序共享該文件的情況。如果你想分享原始數據的關鍵部分是你必須創建一個UIActivityItemSource的實例,並將其傳遞給UIActivityViewController。這樣的事情:

class DataActivityItemSource: NSObject, UIActivityItemSource { 
    let myData: NSData 
    let typeIdentifier: String 
    let subject: String 
    let previewImage: UIImage 

    init(myData: NSData, typeIdentifier: String, subject: String, previewImage: UIImage) { 
     self.myData = myData 
     self.typeIdentifier = typeIdentifier 
     self.subject = subject 
     self.previewImage = previewImage 
    } 

    // called to determine data type. only the class of the return type is consulted. it should match what -itemForActivityType: returns later 
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject { 
     return myData 
    } 

    // called to fetch data after an activity is selected. you can return nil. 
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { 
     return myData 
    } 

    // if activity supports a Subject field. iOS 7.0 
    @objc func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String { 
     return subject 
    } 

    // UTI for item if it is an NSData. iOS 7.0. will be called with nil activity and then selected activity 
    @objc func activityViewController(activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: String?) -> String { 
     return typeIdentifier 
    } 

    // if activity supports preview image. iOS 7.0 
    @objc func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String?, suggestedSize size: CGSize) -> UIImage? { 
     // look at suggestedSize and resize image (see AirDrop sample code for how to do this) 
     return previewImage 
    } 
} 


@IBAction func shareButton(sender: AnyObject) { 

    // myData is the object I want to send to be used in my app on another device 
    let itemSource = DataActivityItemSource(myData, "com.foo.ppm.typeIdentifier", "My Amazing Journey", aPreviewImage) 
    let vc = UIActivityViewController(activityItems: [itemSource],applicationActivities: []) 
    presentViewController(vc, animated: true, completion: nil) 

} 
+0

感謝您的幫助,並展示了一個示例,但我已經瞭解並將Multipeer框架應用到我的應用中,作爲將數據傳遞到其他設備的成功方法。 – pete