iOS Handoff - getContinuationStreams始终失败

时间:2016-10-17 12:30:24

标签: ios swift nsstream handoff nsuseractivity

我尝试通过NSInputStreamNSOutputStream获取更大的数据来实施iOS Handoff。当我在方法application(_:continue:restorationHandler:)中成功接收用户活动时,我会调用getContinuationStreams,但它始终会失败并显示错误:

  

错误域= NSCocoaErrorDomain代码= 4609"由于网络不可用,无法完成切换。" UserInfo = {NSUnderlyingError = 0x17db43c0 {错误域= NSPOSIXErrorDomain代码= 51"网络无法访问" UserInfo = {NSLocalizedDescription = Connection lost}}}

userInfo的词典NSUserActivity不为空,收到的很好。

我关注了Apple's Handoff Programming Guide和此article

以下是我如何创建用户活动的代码示例:

func createUserActivity() {
    self.userActivity = NSUserActivity(activityType: self.userActivityType)
    self.userActivity?.supportsContinuationStreams = true;
    self.userActivity?.delegate = self
    self.userActivity?.becomeCurrent()
    self.userActivity?.needsSave = true
}

在AppDelegate中,我收到如下用户活动:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    NSLog("continue userActivity %@", userActivity.userInfo!)

    if userActivity.supportsContinuationStreams {
        userActivity.getContinuationStreams(completionHandler: { (inputStream, outputStream, error) in
            if error != nil {
                NSLog("\(type(of: self)), \(#function) \(error.debugDescription)")
            } else {
                if let inStream = inputStream {
                    self.receivedData = NSMutableData()

                    self.currentActivityType = userActivity.activityType

                    self.inputStream = inStream
                    self.inputStream.delegate = self
                    self.inputStream.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
                    self.inputStream.open()
                }
            }

        })
    }

    return true
}

参数inputStreamoutputStream始终为零。

我做错了什么?有没有人有线索?

0 个答案:

没有答案