UnsafeMutablePointer to NSString

Converting UnsafeMutablePointer<UInt8> to NSString.

Today I was using a function that was bridging from a third party library in Objective-C.

The function has two parameters, first parameter has a type of UnsafeMutablePointer<UInt8>, second one the type is Int32, which is the length of the char array.  My goal is convert it to NSString for logging information.

UnsafeMutablePointer<UInt8> is an array of char(or bytes) in the library API.

In order to do this, use NSString function:

convenience init?(bytes bytes: a href=”” UnsafePointer <Void >,
length length: Int,
encoding encoding: UInt )

Example:

Read More

Preventing Your IOS app from sleeping mode

Sometime, apps may need to prevent the sleeping mode to enhance user experience.  For example, an app that play video would not go into sleep while the movie is playing.  In IOS, a simple task can be done to prevent the app run into a sleep mode.

Just remember to turn on the sleeping mode whenever the task finish.  (for example, when your app stop playing video, you should turn the sleeping mode on again.

Read More