nsdata

How can I convert my device token (NSData) into an NSString?

谁说我不能喝 提交于 2019-11-27 10:10:59
I am implementing push notifications. I'd like to save my APNS Token as a String. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken { NSString *tokenString = [NSString stringWithUTF8String:[newDeviceToken bytes]]; //[[NSString alloc]initWithData:newDeviceToken encoding:NSUTF8StringEncoding]; NSLog(@"%@", tokenString); NSLog(@"%@", newDeviceToken); } The first line of code prints null. the second prints the token. How can I get my newDeviceToken as an NSString? kulss use this : NSString * deviceTokenString = [[[

NSDictionary to NSData and NSData to NSDictionary in Swift

半城伤御伤魂 提交于 2019-11-27 10:06:15
问题 I am not sure if I am using the dictionary or the data object or both incorrectly. I m trying to get used to the switch to swift but I'm having a little trouble. var dictionaryExample : [String:AnyObject] = ["user":"UserName", "pass":"password", "token":"0123456789", "image":0] // image should be either NSData or empty let dataExample : NSData = dictionaryExample as NSData I need the NSDictionary to encode to an NSData object as well as taking that NSData object and decode it into a

NSData won't accept valid base64 encoded string

旧城冷巷雨未停 提交于 2019-11-27 09:02:10
I'm implementing JSON Web Token authentication on the iOS (7) cient-side. It's working nicely. My app rceives tokens, and can make authenticated calls to my server with them. Now, I want my client side code to check for an expiration date on the token so it can know when to re-authenticate. Checking for the expiration date on a JWT auth token is straightforward. The authorization token is 3 base64 encoded JSON blobs, separated by a '.' - The expiration timestamp is in the middle blob, in a field called ext . It's seconds since unix epoch. So my code's looking like so: - (NSDate*)

Swift structs to NSData and back

匆匆过客 提交于 2019-11-27 08:39:14
I have a struct containing a struct and an NSObject that I want to serialize into an NSData object: struct Packet { var name: String var index: Int var numberOfPackets: Int var data: NSData } var thePacket = Packet(name: name, index: i, numberOfPackets: numberOfPackets, data: packetData) How do I best serialize the Packet into an NSData , and how do I best deserialize it? Using var bufferData = NSData(bytes: & thePacket, length: sizeof(Packet)) of only gives me the pointers of name and data. I was exploring NSKeyedArchiver , but then I'd have to make Packet an object, and I'd prefer to keep it

Need to convert NSData to NSArray

大城市里の小女人 提交于 2019-11-27 08:15:26
问题 I am pulling down a plist from a URL using an NSURLConnection, which returns an NSData object. I would like to convert this to an NSArray. Can anyone help me? I am refactoring a method that currently does a synchronous request to the URL, using arrayWithContentsOfURL: to get the NSArray. Thanks!! 回答1: Use +[NSPropertyListSerialization dataWithPropertyList:format:options:error:] to convert the data, then check if the result -isKindOfClass:[NSArray class] . 回答2: Use NSKeyedArchiver for

NSImage to NSData as PNG Swift

笑着哭i 提交于 2019-11-27 07:36:10
问题 I am writing a Mac app based on an iOS app. The code below converts a UIImage to NSData to upload to Parse.com. I would like to do the same for Mac but I do not seem to be able to convert it to NSData. What should I be doing? Thanks var image = UIImage(named: "SmudgeInc") let imageData = UIImagePNGRepresentation(image) let imageFile = PFFile(name:"image.png", data:imageData) 回答1: You can use the NSImage property TIFFRepresentation to convert your NSImage to NSData : let imageData = yourImage

How can i get original nsdata of uiimage?

坚强是说给别人听的谎言 提交于 2019-11-27 07:29:37
问题 In my app, user can select a pic from album or camera where i can get uiimage representation. Since the album may have pic from web, the file type is not only jpg. Then i need to sent it to the server without convertion. Here i can only use nsdata. I know UIImageJPEGRepresentation and UIImagePNGRepresentation, but i think this two method may convert the original image. Maybe when quality set to 1 UIImageJPEGRepresentation can get original pic? Is there any method to get the original uiimage

Convert NSAttributedString into Data for storage

蹲街弑〆低调 提交于 2019-11-27 06:59:43
问题 I have a UITextView with attributed text and allowsEditingTextAttributes set to true . I'm trying to convert the attributed string into a Data object, using the following code: let text = self.textView.attributedText let data = try text.data(from: NSMakeRange(0, text.length), documentAttributes: [:]) However, this is throwing the following error: Error Domain=NSCocoaErrorDomain Code=66062 "(null)" Any ideas what this error means or what could cause this? I'm on the latest Xcode and iOS.

Split NSData objects into other NSData objects of a given size

情到浓时终转凉″ 提交于 2019-11-27 06:59:12
I have an NSData object of approximately 1000kB in size. Now I want to transfer this via Bluetooth. It would be better if I have, let's say, 10 objects of 100kB. It comes to mind that I should use the -subdataWithRange: method of NSData. I haven't really worked with NSRange. Well, I know how it works, but I can't figure out how to read from a given location with the length: 'to end of file'... I've no idea how to do that. Some code on how to split this into multiple 100kB NSData objects would really help me out here. (it probably involves the -length method to see how many objects should be

Decode Base-64 encoded PNG in an NSString

这一生的挚爱 提交于 2019-11-27 05:21:35
I have some NSData which is Base-64 encoded and I would like to decode it, I have seen an example that looks like this NSData* myPNGData = [xmlString dataUsingEncoding:NSUTF8StringEncoding]; [Base64 initialize]; NSData *data = [Base64 decode:img]; cell.image.image = [UIImage imageWithData:myPNGData]; However this gives me a load of errors, I would like to know what to do in order to get this to work. Is there some type of file I need to import into my project or do I have to include a framework? These are the errors I get Use of undeclared identifier 'Base64' Use of undeclared identifier