0

I have an Alamofire request that is suppose to download a file, but it will not execute the code it self. Here is the Alamofire Code:

var testNumbers: Int = 0
var testString: String = "Hi"
Alamofire.download(.GET, "http://www.sayweee.com/admin_groupbuy/export_deal_orders/71w4o?format=csv") { temporaryURL, response in
            
            let fileManager = NSFileManager.defaultManager()
            let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
            let pathComponent = response.suggestedFilename  

            print("1")
            testNumbers = 1  

            print(directoryURL.URLByAppendingPathComponent(pathComponent!)) 

            print("blah")
            testString = "blah"
            print("2")
            testNumbers = 2  

            return directoryURL.URLByAppendingPathComponent(pathComponent!)
            
        }
print(testNumbers)
print(testString)

Executing this code will print this in the console:

 0
 Hi

I am pretty sure this means that the code within the {} are not getting executed. I've read another on this subject, and learned that Alamofire is "asynchronous", as they said in this post. I've tried to get rid of everything in viewDidLoad() method and viewDidAppear() method, and made sure I didn't have any endless loops. Even after that, the results are the same. Any ideas or suggestion on why this is happening to me? I tried to look here and on Google, but I only found one post related to this topic, the one linked above.

Jero Wang
  • 25
  • 6

1 Answers1

1

First of all, I put your code to run in Demo Alamofire found running results and what you said is the same, as shown in the figure below: 0 Hi 2015-12-30 14:31:29.873 iOS Example[3966:142688] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

According to the prompt you will see that the problem: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

The solution is as follows(In the project file "info.plist" ): In project's "info.plist" file to add a "NSAppTransportSecurity" dictionary, an element is added in the dictionary.Key for "NSAllowsArbitraryLoads", the value is: YES, make it back to the HTTP protocol for the time being.

The problem solution links:

Transport security has blocked a cleartext HTTP

The final results are as follows:

0 Hi 1 file:///Users/yangshebing/Library/Developer/CoreSimulator/Devices/151CB429-29B3-46D0-AFF5-37D5B8D9E4FC/data/Containers/Data/Application/756A32D1-64C5-48CF-B652-D3009D80780D/Documents/71w4o.html blah 2

Specific problems you can go to query the apple official documentation, query iOS9 ATS adaptation problem.

The hope can help you!

Community
  • 1
  • 1
  • Thank you. Even though your answer did not help me fix the problem because my problem was different, I appreciate your efforts to test and explain the problem. My program was actually encountering another error before the print commands were displayed. That stopped the program. Now I added a while loop to wait for the download to finish before going on so now it works. Nonetheless, your answers could be helpful to future visitors of this thread. Thank you. – Jero Wang Dec 30 '15 at 07:20