3

I have Objective-C method

- (void)updateRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;

I was calling successfully in swift 2.3

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

 Moxtra.sharedClient().updateRemoteNotificationsWithDeviceTokens(deviceToken)

} 

but in swift 3 method change (from NSdata to data)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

   Moxtra.sharedClient().updateRemoteNotifications(withDeviceToken: deviceToken)

}

But swift 3 device token of Data type return -> 32 bytes and swift 2.3 device token of NSdata type was return - token string - fffeaa1e 5aaaba7d a0e75e33 c139839f 6c906ae5 2b20f834 5a290c3d 20dc447c

so in swift 3 I am not able to pass deviceToken to server

is there any way to pass expected device token of parameter type "Data" in swift 3?

what is diff between both swift 2.3 and swift 3 methods in details

Swift 2.3 -

Moxtra.sharedClient().updateRemoteNotificationsWithDeviceTokens(fffeaa1e 5aaaba7d a0e75e33 c139839f 6c906ae5 2b20f834 5a290c3d 20dc447c)

swift 3

Moxtra.sharedClient().updateRemoteNotifications(withDeviceToken: 32)
New iOS Dev
  • 1,937
  • 7
  • 33
  • 67
  • 1
    Your swift 2.3 code looks wrong; you are passing a string, not NSData. Your swift 3 code looks correct; Swift 3 `Data` is automatically bridged to `NSData`. – Paulw11 Nov 05 '16 at 12:03
  • @Paulw11 Right! it was my typing mistake but in code I am passing NSData..I update my question – New iOS Dev Nov 05 '16 at 12:07
  • I still don't understand your problem. What error or other problem do you get with the Swift 3.0 version? Data is the same as NSData – Paulw11 Nov 05 '16 at 19:47
  • @Paulw11I am not getting pushnotification.....all was working fine before conversion..I have done all required setting in Xcode 8....testing on iOS 9 – New iOS Dev Nov 07 '16 at 04:48
  • 1
    Your confusion is only about the way how Swift **prints** the data. Swift 3 prints *xx bytes*, Swift 2 prints **, but the result is the same. There is only one significant difference in the code: `...WithDeviceTokens` vs `...WithDeviceToken` (see trailing s) – vadian Nov 08 '16 at 05:52
  • @vadian Thank You for comment. But the method is suggested by swift/xcode itself. If I add "S" it gives error – New iOS Dev Nov 08 '16 at 06:15
  • Instead of passing Device token as Data, Why can't you convert data to string and pass as just String? – Sanju Nov 11 '16 at 11:01
  • What is your actual issue? – shallowThought Nov 14 '16 at 11:18

4 Answers4

1

Your swift 3 code is perfectly correct, I have done exactly same thing as you done, and working fine. I think there is some other problem you have.

If you still have doubt that it is changing NSData values, then you can try other experiment like this:

add method following in your objective-c code:

-(void)stringWithData:(NSData *)data
{
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str = %@",str);
}

now call this method from your swift 3 code like this:

let str = "Hello StackOverflow!";
let data:Data! = str.data(using: String.Encoding.utf8);
Moxtra.sharedClient().string(with: data);

Now, here you can see that Objective-c has NSData param and swift has Data as param.

Now run the code, see the log, Do you get the same string at Log in objective-c? Yes? Does your doubt about NSData and Data get solved?

There is no any problem with this, The problem must be with your Objective-C code, or may be backend code. Check from Backend that are they getting proper deviceToken. Check that, have you used proper development/distribution APNS certificates. Debug each point. There is no any problem with Data or NSData

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • Push-notification is working perfect with swift 2.3 with same APNS, certificate and provision...but not in Swift 3, in Xcode 8 I change "push-notification " setting in "capabilities" is there any other changes I need to do in swift 3/Xocde 8 – New iOS Dev Nov 08 '16 at 06:30
  • Is both bottom options are checkmarked as shown in this image: http://www.tiikoni.com/tis/view/?id=dc5bd9c – Mehul Thakkar Nov 08 '16 at 06:34
  • Check that from server side, development profile is used for current development purpose – Mehul Thakkar Nov 08 '16 at 06:37
  • YES. Both are checked. AppID and provision profile I am using showing push-notification enabled ...also with same AppID, certificate, provision profile ..working fine is Swift 2.3 – New iOS Dev Nov 08 '16 at 06:45
  • Sorry..but i don't understand what should I check on server end..And I am using development certificate and provision profile are talking about same? – New iOS Dev Nov 08 '16 at 06:47
1

In Swift 3 you can simply use:

Moxtra.sharedClient().updateRemoteNotifications(withDeviceToken: deviceToken as Data)
iGenio
  • 398
  • 1
  • 8
1

There is no difference in Data and NSData.

let data:Data = ....
var ndata:NSData = data as! NSData

So use it as

Moxtra.sharedClient().updateRemoteNotifications(withDeviceToken: deviceToken as! Data)
Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
1

you need to typecast to NSData and then use Encoding methods as per you want.

coreDeviOS
  • 1,468
  • 2
  • 14
  • 27