I'm having this problem with reading from a txt file from a website without a certificate even though I have allowed it in Info.plist.
My Swift code looks like this:
let url = URL(string: "http://newskit.matsworld.io/domaci/text.txt")!
let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
if let localURL = localURL {
if let string = try? String(contentsOf: localURL) {
print(string)
}
}
}
task.resume()
And the Info.plist looks like this:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://newskit.matsworld.io</key>
<dict>
<key>ExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>IncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
I'm stuck with this and can't figure it out. Thanks for help!
The domain key is supposed to be just the domain without the scheme (http) and without subdomains if IncludesSubdomains is specified
<key>matsworld.io</key>
Related
I am facing a strange issue with AWSS3.
Setup:
AWS Mobile HUB
Cognito
DynamoDB
S3
--> Cognito, Dynamo & even S3 (through cognito user data) work.
However I now tried to connect directly to AWS3 with the following code:"
let transferManager = AWSS3TransferManager.default()
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = "XXXXXXXXXXXX"
uploadRequest?.key = "user-data/" + awsId! + "/primary_profile_picture.png"
uploadRequest?.body = imgUrl as URL
transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as? NSError {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error uploading: \(uploadRequest?.key) Error: \(error)")
}
} else {
print("Error uploading: \(uploadRequest?.key) Error: \(error)")
}
return nil
}
let uploadOutput = task.result
print("Upload complete for: \(uploadRequest?.key)")
return nil
})
and am getting the error:
AWSiOSSDK v2.5.1 [Debug] AWSInfo.m line:122 | -[AWSServiceInfo initWithInfoDictionary:checkRegion:] | Couldn't read the region configuration from Info.plist for the client. Please check your `Info.plist` if you are providing the SDK configuration values through `Info.plist`.
2017-02-20 19:29:21.748997 [2210:1152801] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The service configuration is `nil`. You need to configure `Info.plist` or set `defaultServiceConfiguration` before using this method.'
I am using the downloaded plist configuration from AWS Mobiel HUB and am therfore a bit surprised that it does not work (as all other components do).
Any ideas what the issue might be? The plist actually contains the bucket ID & region.
For me, I had to configure the credentials with the following code, before uploading:
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,identityPoolId:PoolID)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
Where PoolID is my Cognito identity. I hope this helps others.
Your info.plist needs to have S3TransferManager in it.
So, **AWS -> S3TransferManager -> Default -> Region -> ...**
You can find an example of one here
Swift 3 - Xcode 8.3.3
For people still having this issue, I just spent 3h fighting against this annoying setup issue.
I added both these chunks in my Info.plist (replace the variables between ** ** in the second bloc) and now it's working again.
Amazon's documentation isn't updated properly I think. I hope this can save some people some time.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>amazonaws.com</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>amazonaws.com.cn</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
and:
<key>AWS</key>
<dict>
<key>CredentialsProvider</key>
<dict>
<key>CognitoIdentity</key>
<dict>
<key>Default</key>
<dict>
<key>PoolId</key>
<string>**YourCognitoIdentityPoolId**</string>
<key>Region</key>
<string>**AWSRegionUnknown**</string>
</dict>
</dict>
</dict>
<key>S3TransferManager</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>**AWSRegionUnknown**</string>
</dict>
</dict>
</dict>
I had the same issue instead of S3TransferManager you should put DynamoDBObjectMapper
e.g..
<key>DynamoDBObjectMapper</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>us-east-1</string>
</dict>
</dict>
The problem was, region should be:
us-east-1
Instead of; US_EAST_1
Add Service configuration file with the data
If the above marked solution is not working for some cases. Try the below solution.
As the error says, the service configuration file is nil. So, we need to add the awsconfiguration.json file with the below data.
{
"Version": "1.0",
"IdentityManager": {
"Default": {}
},
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "REPLACE_ME",
"Region": "REPLACE_ME"
}
}
},
"S3TransferUtility": {
"Default": {
"Bucket": "REPLACE_ME",
"Region": "REPLACE_ME"
}
}
}
Replace the bucket, region, poolId of your project.
I am facing a strange issue with AWSS3.
Setup:
AWS Mobile HUB
Cognito
DynamoDB
S3
--> Cognito, Dynamo & even S3 (through cognito user data) work.
However I now tried to connect directly to AWS3 with the following code:"
let transferManager = AWSS3TransferManager.default()
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = "XXXXXXXXXXXX"
uploadRequest?.key = "user-data/" + awsId! + "/primary_profile_picture.png"
uploadRequest?.body = imgUrl as URL
transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as? NSError {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error uploading: \(uploadRequest?.key) Error: \(error)")
}
} else {
print("Error uploading: \(uploadRequest?.key) Error: \(error)")
}
return nil
}
let uploadOutput = task.result
print("Upload complete for: \(uploadRequest?.key)")
return nil
})
and am getting the error:
AWSiOSSDK v2.5.1 [Debug] AWSInfo.m line:122 | -[AWSServiceInfo initWithInfoDictionary:checkRegion:] | Couldn't read the region configuration from Info.plist for the client. Please check your `Info.plist` if you are providing the SDK configuration values through `Info.plist`.
2017-02-20 19:29:21.748997 [2210:1152801] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The service configuration is `nil`. You need to configure `Info.plist` or set `defaultServiceConfiguration` before using this method.'
I am using the downloaded plist configuration from AWS Mobiel HUB and am therfore a bit surprised that it does not work (as all other components do).
Any ideas what the issue might be? The plist actually contains the bucket ID & region.
For me, I had to configure the credentials with the following code, before uploading:
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,identityPoolId:PoolID)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
Where PoolID is my Cognito identity. I hope this helps others.
Your info.plist needs to have S3TransferManager in it.
So, **AWS -> S3TransferManager -> Default -> Region -> ...**
You can find an example of one here
Swift 3 - Xcode 8.3.3
For people still having this issue, I just spent 3h fighting against this annoying setup issue.
I added both these chunks in my Info.plist (replace the variables between ** ** in the second bloc) and now it's working again.
Amazon's documentation isn't updated properly I think. I hope this can save some people some time.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>amazonaws.com</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>amazonaws.com.cn</key>
<dict>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
and:
<key>AWS</key>
<dict>
<key>CredentialsProvider</key>
<dict>
<key>CognitoIdentity</key>
<dict>
<key>Default</key>
<dict>
<key>PoolId</key>
<string>**YourCognitoIdentityPoolId**</string>
<key>Region</key>
<string>**AWSRegionUnknown**</string>
</dict>
</dict>
</dict>
<key>S3TransferManager</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>**AWSRegionUnknown**</string>
</dict>
</dict>
</dict>
I had the same issue instead of S3TransferManager you should put DynamoDBObjectMapper
e.g..
<key>DynamoDBObjectMapper</key>
<dict>
<key>Default</key>
<dict>
<key>Region</key>
<string>us-east-1</string>
</dict>
</dict>
The problem was, region should be:
us-east-1
Instead of; US_EAST_1
Add Service configuration file with the data
If the above marked solution is not working for some cases. Try the below solution.
As the error says, the service configuration file is nil. So, we need to add the awsconfiguration.json file with the below data.
{
"Version": "1.0",
"IdentityManager": {
"Default": {}
},
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "REPLACE_ME",
"Region": "REPLACE_ME"
}
}
},
"S3TransferUtility": {
"Default": {
"Bucket": "REPLACE_ME",
"Region": "REPLACE_ME"
}
}
}
Replace the bucket, region, poolId of your project.
This question already has answers here:
NSURLSession/NSURLConnection HTTP load failed on iOS 9
(13 answers)
Closed 6 years ago.
Here the code:
let url = NSURL(string: "http://a337.phobos.apple.com/us/r30/Music/d5/a8/6b/mzi.msfqeogi.aac.p.m4a")
let downloadTask = self.downloadSession.downloadTaskWithURL(url!)
downloadTask.resume()
-> OK
Replace the URL with:
http://s1mp3.r41s223.vcdn.vn/d3f549abeeef07b15efe/2937387453157939420?key=c2WuQChtsmR9yfdz9I-jnw&expires=1480695588&filename=Only%20Love%20-%20Trademark.mp3
-> it doesn't work; nothing happens
By default starting in iOS 9 all connections must be https. If the website for the second URL doesn't support https:// then the download would fail.
I suggest checking the error code you get back from the Download
If you want to use url with HTTP connection, then in your info.plist file add the following
App Transport Security Setting and in that add subsection Allow Arbitrary Loads and set the value to YES.
Please add in Plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>akamaihd.net</key>
<dict>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>facebook.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>graph.facebook.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Then using Alamofire 4.0 Swift 3.0
Using below method, you can download any thing, ex. PDF
// Mark:- Download File
func DownloadFile(strFileUrl : String, strFileName : String, success:#escaping (_ strResultURL: String) -> Void , failure:#escaping (_ responseObject:AnyObject) -> Void) {
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("\(strFileName)")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(strFileUrl, to: destination).response { response in
if SVProgressHUD.isVisible() {
SVProgressHUD.dismiss()
}
if response.error == nil {
success("\(response.destinationURL!)")
}
else {
failure(response.error as AnyObject)
}
}.downloadProgress { (progress) in
print("Download Progress: \(progress.fractionCompleted)")
SVProgressHUD.showProgress(Float(progress.fractionCompleted), status: AppAlertMsg.KDownloadingPDFs)
}
}
I am trying to get data from Soap request but getting my app crashed with this error:
2016-02-08 11:12:11.524 Example[567:128898] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
Response: nil
fatal error: unexpectedly found nil while unwrapping an Optional value
I have enabled Security settings in plist and calling other services successfully.
Note: The service is on HTTPS, am i getting this error because of this? is this required any certificate?
i am calling soap service for the first time, please also guide me on how to add email parameter in service: (https://[IP]:9897/JLIPolicyinfo.asmx?op=GetEmail_Poldetail)
here is my code that i get from here: (IOS Swift Call Web Service using SOAP)
let is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>"
let is_URL: String = "https://58.27.241.203:9897/JLIPolicyinfo.asmx"
let lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
let session = NSURLSession.sharedSession()
let err: NSError?
lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue("58.27.241.203", forHTTPHeaderField: "Host")
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String((is_SoapMessage.characters.count)), forHTTPHeaderField: "Content-Length")
//lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
lobj_Request.addValue("http://tempuri.org/GetEmail_Poldetail", forHTTPHeaderField: "SOAPAction")
let task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)")
if error != nil
{
print("Error: " + error!.description)
}
})
task.resume()
Here is my plist addition:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>https://58.27.241.203:9897/JLIPolicyinfo.asmx</key>
<dict>
<key>NSIncludesSubdomains</key>
<false/>
<key>NSExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
You might have a self signed certificate that can be also a problem .
Solution:-
It will work if you renew your server's ssl certificate to use SHA2 and enabling TLS v1.2.
Possibly a solution:-
You have to add this in your info.plist file, so that it can allow non-secure connection:-
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Or even you can add like this:-
Edit:-
Add this in your info.plist for specific exception.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>testdomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<false/>
<key>NSExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
I'm trying to bypass Application Transport Security(ATS), its a new feature of IOS 9 and Xcode 7. However, I tried the info.plist bypass and I am still having problems. I tried the exact same code in Xcode 6 and the request does get sent successfully, so the request should be correct. This could just be a bug on the new Xcode but I was wondering if anyone else ran into the same issue. I'm pretty sure I'm following proper documentation: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html#//apple_ref/doc/uid/TP40016240
Info.plist(not complete, just part on ATS)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict/>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>http://127.0.0.1:5000</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
Request:
let postData = NSMutableData(data: "username=bobbyz".dataUsingEncoding(NSUTF8StringEncoding)!)
postData.appendData("&password=form".dataUsingEncoding(NSUTF8StringEncoding)!)
let request = NSMutableURLRequest(URL: NSURL(string: "http://127.0.0.1:5000/register")!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 10.0)
request.HTTPMethod = "POST"
request.HTTPBody = postData
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? NSHTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
This just happened to me. Turns out I accidentally added the bypass information to my Unit Test Info.plist. Putting it in the correct Info.plist fixed the issue, as expected. I also used "localhost" instead of "127.0.0.1" and did not provide the port.
Using Xcode 7 Beta 4.