Audio Controls on Lock Screen/Control Center From AV player Issues - swift3

Im new to swift and Im having issues while using MPRemoteCommandCenter.
When I try to control the audio settings, previously accessed songs also start playing along with the current song.
Im trying to access using the following code:
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
//Update your button here for the pause command
self.player!.pause()
return .success
}
commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
//Update your button here for the play command
self.player!.play()
return .success
}
I have also used the following while ending the view:
UIApplication.shared.endReceivingRemoteControlEvents()

Related

lottie animation causing app crash on device

I am using Lottie to display animations in application and I tried to use this animation
AnimationURL
LottieView -
struct LottieView: UIViewRepresentable {
var animationName: String
func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
let view = UIView()
return view
}
func updateUIView(_ uiView: UIViewType, context: Context) {
let animationView = AnimationView()
let animation = Animation.named(animationName)
animationView.animation = animation
animationView.contentMode = .scaleAspectFit
animationView.backgroundBehavior = .pauseAndRestore
animationView.loopMode = .loop
animationView.translatesAutoresizingMaskIntoConstraints = false
uiView.addSubview(animationView)
NSLayoutConstraint.activate([
animationView.heightAnchor.constraint(equalTo: uiView.heightAnchor),
animationView.widthAnchor.constraint(equalTo: uiView.widthAnchor)
])
animationView.play()
}
}
But when I want to display animation, application crashes in AnimatorNode file from Lottie library in this function (Thread 1: EXC_BAD_ACCESS (code=2, address=0x16ce6bff0)):
func updateContents(_ frame: CGFloat, forceLocalUpdate: Bool) -> Bool {
guard isEnabled else {
return parentNode?.updateContents(frame, forceLocalUpdate: forceLocalUpdate) ?? false
}
}
When I opened View Hierarchy after app crash, I received this log -
Failed to unarchive request data with error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set. around line 1, column 0." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set. around line 1, column 0., NSJSONSerializationErrorIndex=0}
On simulator, animation is running normally, but when I build application on device, application is crashing on this animation. Is there any way to fix this and use this animation?
We ran into the same issue.
It doesn't depend on SwiftUI / UIKit thing.
On Simulator it works ok because your simulator has more memory than iPhone does. So maybe your animation.json is too hard for current version of Lottie-ios lib. Maybe its good idea to report them, that they should update implementation of lib on iOS side.
Thank you!

Show downloaded mp4 inside a view like VStack and play it AVPlayer, SwiftUI

I'm working on the offline downloads section for a video streaming app. Currently, I'm able to download videos and also can see the file path for the downloads as follows:
Button(action: {
let fileManager = FileManager.default
do {
let resourceKeys : [URLResourceKey] = [.creationDateKey, .isDirectoryKey]
let documentsURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let enumerator = FileManager.default.enumerator(at: documentsURL,
includingPropertiesForKeys: resourceKeys,
options: [.skipsHiddenFiles], errorHandler: { (url, error) -> Bool in
print("directoryEnumerator error at \(url): ", error)
return true
})!
for case let fileURL as URL in enumerator {
let resourceValues = try fileURL.resourceValues(forKeys: Set(resourceKeys))
// print(fileURL.path, resourceValues.creationDate!, resourceValues.isDirectory!)
print(fileURL.path)
self.array.append(fileURL.path)
}
} catch {
print(error)
}
}){
Text("Check downloads")
}
//response when button is clicked
/private/var/mobile/Containers/Data/Application/AD90B554-3465-43DD-BAE2-04BC83F850A3/Documents/Bz75srG5nctDCnAWIspM.mp4
/private/var/mobile/Containers/Data/Application/AD90B554-3465-43DD-BAE2-04BC83F850A3/Documents/GWYgrgDVYaowxeGcOjzp.mp4
Since I've renamed the mp4 files using movie ID's (eg: GWYgrgDVYaowxeGcOjzp.mp4) my goal is to use this name to show which videos were downloaded and allow users to play the video. I'm able to do the name processing later on, but how am I able to get the downloaded mp4 files into a view like a VStack? I was thinking of looping through and adding the files into an array but wanted to check if there's a different way. Also, would be great if you could show me how to play a video using the path as the url. I've tried applying suggested methods but ended up with a "NSURLConnection finished with error - code -1002" error. Thank you!

Google Sign In showing blank screen in iOS

I have implemented code as per the google SDK documentation line by line in my app, but still when I click on google sign in button app shifts to new view controller with webview with blank screen. Have tried multiple solution found here : GIDSignIn white screen on iOS 91. But no lucks with resolving the problem , have attached the screen shot for getting closer look about the screen.
Following are the pods that I'm using,
Running XCode 9.1, iOS 10.0 and later. Kindly request someone to help.
Update: View Hierarchy
Update: viewDidLoad's code:
GIDSignIn.sharedInstance().uiDelegate = self
if self.isChangePassword {
self.addSignInView()
}
else {
self.addSignUpView()
}
fileprivate func addSignInView() {
guard let signInEmailView: SignInEmailView = Bundle.main.loadNibNamed(NibNames.SignInEmailView.rawValue, owner: self, options: nil)?[0] as? SignInEmailView
else {
return
}
signInEmailView.delegate = self
gaManager.trackScreen(screenName: ScreenNames.SignIn.rawValue)
self.animateView(signInEmailView)
}
fileprivate func addSignInView() {
guard let signInEmailView: SignInEmailView = Bundle.main.loadNibNamed(NibNames.SignInEmailView.rawValue, owner: self, options: nil)?[0] as? SignInEmailView
else {
return
}
signInEmailView.delegate = self
gaManager.trackScreen(screenName: ScreenNames.SignIn.rawValue)
self.animateView(signInEmailView)
}
I have the same problem. I use the UIAlertView to confirm user really want to do authorization. It will show the blank screen. If I remove the UIAlertView and show the authorization view directly. It works fine.
The problem also show in the Dropbox authorization screen.
If you not use UIAlertView , please try to pass the top most controller
https://github.com/dropbox/dropbox-sdk-obj-c/issues/182
Hope this can do some help.
Finally after so many days found the problem. I was showing splash view from my appDelegate using refrence of UIWindow which was adding and removing an imgview as subview to the window. So it was somehow messing with the UINavigation's stack and my view controller was not getting any reference to the UINavigationController. After removing that code it's working just fine. Also came to the solution that, if I want to show splash screen, as I can't use UIWindow's reference, I have to add new VC and write my all navigation code there. #TedYu Thank you very much for the help. :)

How to work with DispatchQueue.global?

I am downloading images from my server using this
let queue = DispatchQueue.global(qos: .userInteractive)
queue.async{ ...
And while images downloading user can tap the button. But sometimes this button doesn't work (when all images done button work correct).
How to solve this problem?
Donot access global queue for that, instead create a custom DispatchQueue with label, qos as .background and fetch the image, when done use DispatchQueue.main to return the image to the main queue
For e.g:
let queue = DispatchQueue(label: "myImageQueue", qos: .background)
queue.async {
...fetch image
DispatchQueue.main.async {
self.imageView.image = fetchedImage
}
}

How to integrate iAd Interstitial Ads with Cocos2D [duplicate]

My developer and I had previously tried to get interstitial adverts loading on iPhone and iPad, however only managed to get this working on iPad.
During our testing we discovered that interstitials weren't supported on iPhone, but since the release of iOS7 some people now say this is possible.
However I can't find any decent documentation on this. This Stack question has got me wondering again though iAd & Admob Interstitial Integration on iPhone
So, are full screen interstitials possible on iPhone using iAds?
I've also been waiting for interstitial ads for iPhone from iAd. I finally have it implemented into one of my apps and it is running and showing test ads just fine. The app is currently in review and I will update my answer once it is approved and I can test if any actual ads are being served. In the meantime, here is the code I used to implement iAd interstitials:
ViewController.h
#interface ViewController : UIViewController <ADInterstitialAdDelegate> {
ADInterstitialAd *interstitial;
BOOL requestingAd;
}
-(void)showFullScreenAd;
ViewController.m
-(void)viewDidLoad {
requestingAd = NO;
}
// Interstitial iAd
-(void)showFullScreenAd {
// Check if already requesting ad
if (requestingAd == NO) {
[ADInterstitialAd release];
interstitial = [[ADInterstitialAd alloc] init];
interstitial.delegate = self;
self.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
[self requestInterstitialAdPresentation];
NSLog(#"interstitialAdREQUEST");
requestingAd = YES;
}
}
-(void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
interstitial = nil;
[interstitialAd release];
[ADInterstitialAd release];
requestingAd = NO;
NSLog(#"interstitialAd didFailWithERROR");
NSLog(#"%#", error);
}
-(void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd {
NSLog(#"interstitialAdDidLOAD");
if (interstitialAd != nil && interstitial != nil && requestingAd == YES) {
[interstitial presentFromViewController:self];
NSLog(#"interstitialAdDidPRESENT");
}
}
-(void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd {
interstitial = nil;
[interstitialAd release];
[ADInterstitialAd release];
requestingAd = NO;
NSLog(#"interstitialAdDidUNLOAD");
}
-(void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd {
interstitial = nil;
[interstitialAd release];
[ADInterstitialAd release];
requestingAd = NO;
NSLog(#"interstitialAdDidFINISH");
}
I just call -(void)showFullScreenAd after the user performs a certain action. For more information refer to UIViewController iAd Additions Reference
EDIT: 01/03/14
The app update was approved and has been live for 12 hours now. I still have not received any interstitial iAds as of this moment. It may be due to the lack of inventory for interstitial ads on iPhone or it may have something to do with the following message I've received in my iAd module on iTunes Connect. The message reads
When you submit your app, [app name], for approval, it will also be reviewed by iAd for use on the App Network to determine its appropriateness for receiving ads from iAd advertisers. Once your app is approved, you will be ready to start earning revenue for ads that run in your app.
I've never received this message before, I usually receive the "Your app is now eligible to receive ads" message as soon as the update is released in the App Store. It's either because of the new implementation of interstitial ads in my app or Apple has updated some of its iAd procedures.
Will update answer again if and when I do start receiving interstitial iAds. In the meantime you may test it yourself on my app +Quotes. The above code is invoked and an interstitial ad should show once a user shares via Facebook within the app.
EDIT: 01/07/14
Contacted iAd directly and received a response today
Hello Daniel,
Initial fill rates will reflect the recent launch of interstitials on iOS 7. Thank you for your patience as we ramp up ad serving.
If you haven't already, we recommend you integrate standard portrait and landscape iAd banners in your app to take advantage of all available types of advertising inventory.
Finally, for future reference, when you submit your app for approval, it will also be reviewed by iAd to determine its appropriateness for receiving ads from iAd advertisers. Therefore, it may take a few days for your new iAd enabled app to begin receiving ad impressions.
Best Regards,
 iAd App Network Support
So, interstitial iAds for iPhone do work with iOS 7.0 and above but have a low fill rate currently.
My original answer is starting to age away so here's a newer Swift implementation:
This implementation uses a Manual ADInterstitialPresentationPolicy so we can present our interstitials at our own intervals. When manually presenting your interstitial it does not load with its own close button to dismiss itself. So, what I've done is created a UIView to present the interstitial in and used the interstitial's delegate methods to dismiss the UIView. The inconsistency with receiving an ad from the iAd network still arises when testing. Sometimes you receive an ad, sometimes it fails to load which allows us to request a new ad, and sometimes nothing happens at all. This just seems to be the nature of iAd's interstitials.
import UIKit
import iAd // Import iAd
class ViewController: UIViewController, ADInterstitialAdDelegate { // Include the delegate
var iAdInterstitial = ADInterstitialAd() // Our ad
var iAdInterstitialView = UIView() // View to present our ad in
var adLoaded = false // Bool to keep track if an ad is loaded or not
override func viewDidLoad() {
super.viewDidLoad()
setupAd()
}
func setupAd() {
// Set presentation to manual so we can choose when to present the interstitial
// Setting this will also fetch an interstitial ad for us
self.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Manual
iAdInterstitial.delegate = self // Set the delegate
// Make our view the same size as the view we will be presenting in
iAdInterstitialView.frame = self.view.bounds
}
func requestNewAd() {
// This will fetch an ad for us
ViewController.prepareInterstitialAds()
println("Requesting new ad")
}
#IBAction func presentAdButton(sender: AnyObject) {
if (adLoaded) {
// We have an ad that is loaded so lets present it
self.view.addSubview(iAdInterstitialView)
iAdInterstitial.presentInView(iAdInterstitialView)
}
else {
// No ad has been loaded
println("Ad not loaded")
}
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
// Kinda works as expected
// Sometimes is called prematurely
// Sometimes takes minutes after ad is dismissed to be called
println("interstitialAdDidUnload")
// Get new ad
adLoaded = false
iAdInterstitialView.removeFromSuperview()
requestNewAd()
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
// Failed to load ad so lets try again
println("didFailWithError: \(error)")
requestNewAd()
}
func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
// There is an ad and it has begun to download
println("interstitialAdWillLoad")
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
// We got an ad
println("interstitialAdDidLoad")
adLoaded = true
}
func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
println("interstitialAdActionShouldBegin")
return true;
}
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
// Done with this ad. Lets get a new one
println("interstitialAdActionDidFinish")
iAdInterstitialView.removeFromSuperview()
adLoaded = false
requestNewAd()
}