Presenting UIActivityViewController casue screen/ui freeze - ios

I try to present UIActivityViewController to my app users but when I do my app sometimes, not always but often, freeze. What I mean is that activity view is on screen but do not respond to user interaction at all. You can't scroll available items, you cannot select any activity item, you cannot click Cancel etc
The way I invoke it is very simple and basic:
func presentSystemShareNotification(notification : Notification) {
if let vURL = notification.userInfo?["videoURL"] {
let activityViewController = UIActivityViewController(activityItems: [vURL], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.airDrop]
naviController.topViewController?.present(activityViewController, animated: true, completion: {() in
print("done presenting")
})
}
}
When ui freezed completion handler is not called. But app itself works, timers are called, ui manipulation like view alpha decrese works and you can see view dim. Any ideas or at least tips what could cause this behavior? Thank you in advance.

Related

How can I get the video from RPScreenRecorder or add a share button?

I'm recording video using RPScreenRecorder.shared().startRecording. However, I want to let the user share the video from within the app. This is the code that stops recording and previews the video in a view controller and gives the user the option to save to photos or cancel.
I cannot figure out how to grab the video from the view controllers view. When I dug into the subviews: preview.view.subviews.first!.subviews.first! There's a view of type: UIRemoteView with no subviews.
Is it possible to grab the video? Or better yet, is there a way to show a UIActivityViewController to allow the user to share the video?
for macOS I could do preview.mode = .share, but for iOS this is not available.
RPScreenRecorder.shared().stopRecording { preview, error in
guard let preview = preview else { return }
self.present(preview, animated: true, completion: nil)
}
There is a share button but it is not visible, if view controller is not presented fullscreen, I think this is a bug related to PRPreviewViewController.
You can change modalTransitionStyle and see share button.
RPScreenRecorder.shared().stopRecording { preview, error in
guard let preview = preview else { return }
preview.modalPresentationStyle = .overFullScreen
self.present(preview, animated: true, completion: nil)
}

How to know if user shared my app or not?

My app is all about getting points and winning money, and what I need to do is let the user share my app then give him some points.
The problem is that I don't know how to detect if the user truly shared the app or not
I'm using the following code:
func shareTapped(){
let text = "example"
let url = URL(string: "example.com")
let image = UIImage(named: "example_image")
let shareViewController = UIActivityViewController(activityItems: [text, image!, url!] ,applicationActivities: nil)
self.present(shareViewController, animated: true, completion: {() in
print("done")
})
}
The sharing method is working perfectly, but I was wondering if there is any delegate we can call in this situation.
Thanks.
So there is 2 scenarios where user can cancel share.
One is when UIActivityViewController present then there is a cancel button on UIActivityViewController from where user can cancel it and yes you can detect it with method
shareViewController.completionWithItemsHandler = { activity, completed, items, error in
}
In above method completed will be false if user canceled from UIActivityViewController cancel button. and it will return true if user share successfully but here comes second case with it.
So for second case suppose user want to share via watsapp and click on watsapp icon from UIActivityViewController and watsapp user list appear.
But there is a cancel button on that screen from where user can cancel sharing but still you will get completed flag true so there is no way you can detect if user click on cancel button from watsapp user list.

IOS swift how to know when activityViewController has been successfully used

I have a tableView that utilizes the UIActivityViewController so users can share content on other 3rd party networks Facebook, twitter, text message etc. I have a function called IncreaseShareByOne() and it's purpose is to count the number of times that something has been shared . My problem is that right now that function fires of every time the UIActivityViewController is clicked . Is there someway that I can find out if a user really shared something so that I can use that function correctly ? becomes sometimes you open the UIActivityViewController and decide not to share anything so I want to catch and not use that function. I am new to swift and am using version 3 .
func sharedShareAction(controller: UIViewController, sender: UIButton) {
controller.present(activityViewController,animated: true,completion: nil)
IncreaseShareByOne()
}
You can add a completion handler to your UIActivityViewController. In the completion handler, check whether the user submitted using the completed value. You'll probably want to do something like this:
func sharedShareAction(controller: UIViewController, sender: UIButton) {
controller.present(activityViewController,animated: true, completion: nil)
activityViewController.completionWithItemsHandler = { activity, completed, items, error in
if !completed {
// handle task not completed
return
}
IncreaseShareByOne()
}
}
Check the API docs for more info.

swift share with function on completion

My app can share files with other apps, but the problem is that I need to delete the files after sharing them... I tried using the onCompletion function as below:
let activityVC = UIActivityViewController(activityItems: objects, applicationActivities: nil)
view.present(activityVC, animated: true) {
try! FileManager.default.removeItem(at: targetURL)
}
The problem is that the onCompletion function executes after the action view disappears not after the whole process of sharing is finished, that's why if I delete the file and the sharing process is still ongoing it will be aborted.. an example is when using telegram for sharing; since telegram asks you to select a contact to send the file to, by that time the view has already disappeard (the function is executed and deleted the file before sharing it)...
It's far too soon to do anything in the completion handler of presenting the controller.
Set the completionWithItemsHandler property of the UIActivityViewController. This will get called when the sharing process is complete.
activityVC.completionWithItemsHandler = { (activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) -> Void in
if completed == true {
try! FileManager.default.removeItem(at: targetURL)
}
}

UIDocumentMenuViewController buttons not doing anything

So I'm testing out Apple's new Document Provider extension. I'm trying to open up a UIDocumentMenuViewController up, and that part is working. But when I try to click on one of the items it presents, it just cancels the action sheet. This happens even when I click on the iCloud item, which is there by default. My code for presenting the controller is as follows:
let type_data = kUTTypeData.__conversion()
let documentMenuViewController = UIDocumentMenuViewController(documentTypes: [type_data], inMode: UIDocumentPickerMode.Import)
navigationController.presentViewController(documentMenuViewController, animated: true, completion: nil)
Anyone know why this is happening?
You need to set the delegate on the UIDocumentMenuViewController. Then implement the -documentMenu:didPickDocumentPicker: delegate method. Then you can go ahead and present the documentPicker that was selected.
func documentMenu(documentMenu: UIDocumentMenuViewController!, didPickDocumentPicker documentPicker: UIDocumentPickerViewController!) {
documentPicker.delegate = self
self.presentViewController(documentPicker, animated: true, completion: nil)
}
There is a simple example of this here in Apple's guide.
Take a look at the NewBox Apple sample code and you'll see the step you are missing. Can't provide the actual answer here as iOS 8 is still under NDA.

Resources