HTTPS Error 401 in Watch OS 3

Good Morning ,

We have implemented apple watch app to run independently, So all web services made in Watch itself. It seems to be working fine in watch OS 2 But When request made in watch OS 3 we are getting Status code as 401. Any body facing same issues or any solution for this ? .

The flow would be :

1. Making login follwed by get detials web services request in iPhone and share login details to watch app to initiate same request in watch os.

Watch OS 1 & 2.x - No issues

Watch OS 3 - Getting sucess for Login request and 401 error for get details service call.

Please Note : There is no code change made for Watch OS 2 & 3 , also there is no session mangement in back end.

Sample Source as below

func makeServiceCallNative(withURL urlStr:String ,withHTTPMethod method:String, withBodyContent bodyContent : NSDictionary? ,withHeaderFields headerFields : NSDictionary?,withSuccessBlock:(successBlock), withFailureBlock:(failureBlock) ,withSessionFailureBlock:(sessionfailureBlock))

{

let url = NSURL(string: urlStr)

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url!)

request.HTTPMethod = method

if bodyContent != nil

{

do {

let data = try NSJSONSerialization.dataWithJSONObject(bodyContent!, options: [])

request.HTTPBody = data

}

catch let error as NSError {

print("json error: \(error.localizedDescription)")

}

}else if headerFields != nil

{

request.allHTTPHeaderFields = headerFields! as? [String : String]

}

request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

configuration.timeoutIntervalForRequest = 30

let urlSession = NSURLSession.init(configuration: configuration)

let task = urlSession.dataTaskWithRequest(request) { (data : NSData?, response :NSURLResponse?, error :NSError?) -> Void in

if error != nil {

withFailureBlock!(error: error)

}else if error == nil && data != nil && response != nil{

do {

let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSDictionary

if let responseDict:NSDictionary = jsonResult{

/

if let errorCode = responseDict["errorCode"] as? Int{

if errorCode == TOKEN_CREATION_ERROR{

withSessionFailureBlock!()

}

else{

withSuccessBlock!(responseData: responseDict , response: response!)

}

}

else{

withSuccessBlock!(responseData: responseDict , response: response!)

}

}

}

catch let parseError as NSError{

if let dataSrt = String(data: data!, encoding: NSUTF8StringEncoding){

/

/

if dataSrt == GlobalConstants.invalid_Session_Error_Message{

withSessionFailureBlock!()

}

else{

withSuccessBlock!(responseData: dataSrt , response: response!)

}

}

else{

withFailureBlock!(error: parseError)

}

/

/

/

}

}

else{

withFailureBlock!(error: nil)

}

}

task.resume()

}

Replies

401 is Unauthorized, meaning that the request made it to the server properly and the server rejected it for security reasons. It’s hard to know for sure why the server did this without knowing more about the server. However, given that it works on watchOS 2 but fails on watchOS 3, it’s very likely that the client is doing something wrong.

My general advice on this front would be to find out what the client is doing wrong by comparing the watchOS 2 and 3 requests. For more info on how to do this, check out my Debugging HTTP Server-Side Errors post.

However, in this case it’s likely that the problem is related to this:

… and share login details to watch app to initiate same request in watch os.

How do you do that credential sharing?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo , Thanks for the quick response .


How do you do that credential sharing?

Ans: As we are using Connectivity framewrok whenever user login or logout apple watch will be notified .


Also we developed app for Watch OS 2 and above in Swift 2.2 . So there is no code level change for Watch OS 2 & 3.


As per your advice let us debug Server side to get exact error , also I'll try to convert soruce code to latest version Swift 3. Please advcise me if anything going wrong.

also I'll try to convert soruce code to latest version Swift 3.

This is unlikely to be related to the version of Swift you’re using, so I recommend you stick with Swift 2 while debugging this issue (that avoids adding an extra axis to your debugging matrix).

As per your advice let us debug Server side to get exact error

OK. Let us know what you uncover.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"