崩溃复现:分享资源->其他App打开->停留一段时间->回到App。

//错误提示

Terminating app due to uncaught exception 'NSGenericException', reason: 'Attempt to invoke -_activityImage on <_UIShareServiceActivityProxy_Share: 0x281343ae0>, which is not supported for proxies to out-of-process activities.

分析崩溃原因:

1、直接导致的原因可能是completionWithItemsHandler回调最终未执行(部分版本才会出现)

2、又因为内部代码可能已经调了回调(部分资源已经释放)。

3、这个时候回到App时,又因为completionWithItemsHandler未走,从而会显示UIActivityViewController,最终又因为部分资源被释放,导致崩溃。

4、具体的你们可以去看一下源码(上面只是我的猜想)。

没找到具体解决方法,用的是曲线救国:

1、先“持有”UIActivityViewController - vc

2、completionWithItemsHandler回调执行就不再“持有” vc

3、App进入后台就dismiss vc

4、App进入后台判断vc == nil,根据判断在弹出vc

5、特别注意:vc需要重新生成,不然还是会崩

//部分代码

var activityController:UIActivityViewController?

func share(){

let vc = UIActivityViewController(activityItems: <#资源#>, applicationActivities: nil)

activityController = vc

vc.excludedActivityTypes = [<#不显示类型#>]

//处理iPad

if let popoverController = vc.popoverPresentationController {

popoverController.sourceView = <#显示view#>

popoverController.sourceRect = <#尺寸#>

popoverController.permittedArrowDirections = []

}

<#当前VC#>.present(vc, animated: true, completion: nil)

vc.completionWithItemsHandler = {[weak self] _,_,_,_ in

guard let this = self else {return}

this.activityController = nil

}

}

//进入前台 - 因为有可能会不是分享进入后台,所以要显示出来。如果走的是completionWithItemsHandler回调,说明是分享才进入后台的。

@objc func applicationBecomeActive(){

if let _ = activityController,<#其他判断#>{

//再次显示

share()

}

}

//进入后台

@objc func applicationEnterBackground(){

activityController?.dismissBack()

}

查看原文