How to represent NSUUID as a string? - ios

How can I convert a NSUUID to NSString?
NSString *url = [self mysql_process:ESTIMOTE_PROXIMITY_UUID];
- (NSString*)mysql_process:(NSUUID *)beacon_id
{
NSString *strURL = [NSString stringWithFormat:#"http://path_to_php_file/mysql.php?id=%#", beacon_id];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
return strResult;
}
When I try to NSLog the URL im getting a pointer, which shows me my NSUUID, ESTIMOTE_PROXIMITY_UUID in this case.

NSUUID has a method: -(NSString*)UUIDString;.
So:
NSString *uuidString = [yourNSUUID UUIDString]

swift 3 version
Ex:
let uuidStringValue = "12345678-1111-2222-3333-4567891012B4"
let proximityUUID = NSUUID(uuidString: uuidStringValue)

If you want NSString from NSUUID. Use UUIDString of NSUUID...
NSUUID *uuidStringRef = [[UIDevice currentDevice] identifierForVendor];
NSString *stringRef = [uuidStringRef UUIDString];//you can replace uuidStringRef with your ESTIMOTE_PROXIMITY_UUID
Hope it helps you...!

NSString *url = [self mysql_process:ESTIMOTE_PROXIMITY_UUID];
- (NSString*)mysql_process:(NSUUID *)beacon_id {
**NSString *strDeviceUUID = [beacon_id UUIDString];** // Converts NSUUID to UUIDString format
NSLog(#"beacon_id : %# strDeviceUUID : %# ", beacon_id,strDeviceUUID);
NSString *strURL = [NSString stringWithFormat:#"http://path_to_php_file/mysql.php?id=%#", strDeviceUUID];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
return strResult;
}
This function is present in NSUUID.h file of Foundation frameworks.
/* Return a string description of the UUID, such as "E621E1F8-C36C-495A-93FC-0C247A3E6E5F" */
- (NSString *) UUIDString;

Related

how to decode base64 NSString(using euc-kr) .. T.T

I have string
this string is encoded with euc-kr..
I try this code
NSString *str = MjAxObPitbUgx9C7/bq4yKPAzrfCKLnov/LFzcH2xbTAzCkguPDB/SCw+LDt
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingEUC_KR];
but result is null..
what is wrong..? and I try
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingDOSKorean];
and
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingMacKorean];
but result is same..
encoding:kCFStringEncodingMacKorean's result is not korean.;;;;;
Here is a working solution:
Starting with:
NSString *str = #"MjAxObPitbUgx9C7/bq4yKPAzrfCKLnov/LFzcH2xbTAzCkguPDB/SCw+LDt";
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
What you missed is that you can't really use kCFStringEncodingEUC_KR which is a CFStringEncodings with initWithData:encoding: with expects a NSStringEncoding.
You can use CFStringConvertEncodingToNSStringEncoding() to convert it:
NSString *result = [[NSString alloc] initWithData:decodedData encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_KR)];
NSLog(#"result: %#", result);
Output:
$>result: 2019년도 학생보호인력(배움터지킴이) 모집 공고
If you want to use "natively" the kCFStringEncodingEUC_KR, and all the CFDataRef, CFStringRef, and use the bridge then to convert CFStringRef to NSString:
CFStringRef ref = CFStringCreateWithBytes(nil, [decodedData bytes], [decodedData length], kCFStringEncodingEUC_KR, false);
NSString *result = (__bridge NSString *)ref;
NSLog(#"result: %#", result);
Output:
$>result: 2019년도 학생보호인력(배움터지킴이) 모집 공고
You might need to do some changes, I'm not really familiar with CFStringCreateWithBytes(), check the documentation.

special character like è,á are not converting their respective unicodes in Ios for other platform like android and app website

I am trying this.
-(NSString *)convertEmojiIntoUnicode:(NSString *)emojiStr{
NSData *data = [emojiStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return goodValue;
}
For Decoding
-(NSString *)convertUnicodeToEmoji:(NSString *)unicodeStr{
NSString *newString = unicodeStr;
NSData *data1 = [newString dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue1 = [[NSString alloc] initWithData:data1 encoding:NSNonLossyASCIIStringEncoding];
return goodValue1;
}
for characters like è is converting to \350 with above method but I am getting unicode for è- \u00E8 from android and website.
I can decode \350 and \u00E8 both with above decode method(convertUnicodeToEmoji) to è
Simply try out this solution :
NSString *myString = #"Here is conversion of char ë, é";
NSData *mData = [myString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *strResult = [[NSString alloc] initWithData:mData encoding:NSASCIIStringEncoding];
NSLog(#"Result: %#", strResult);
Here is Output :
Result: Here is conversion of char e, e
Hope it will work for you.

XCDYouTubeVideoPlayerViewController videoURL coming as nil

In this method:
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error = nil;
NSURL *videoURL = [self videoURLWithData:self.connectionData error:&error];
if (videoURL)
self.moviePlayer.contentURL = videoURL;
else if (self.elFields.count > 0)
[self startVideoInfoRequest];
else
[self finishWithError:error];
}
videoURL is returned as nil and hence, it is going to the error block. Youtube video id that I am using is "5Uls9v1nnss". What seems to be the issue?
the videoURLWithData method which is used to retrieve the videoURL is this :
- (NSURL *) videoURLWithData:(NSData *)data error:(NSError * __autoreleasing *)error
{
NSString *videoQuery = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSStringEncoding queryEncoding = NSUTF8StringEncoding;
NSDictionary *video = DictionaryWithQueryString(videoQuery, queryEncoding);
NSMutableArray *streamQueries = [[video[#"url_encoded_fmt_stream_map"] componentsSeparatedByString:#","] mutableCopy];
[streamQueries addObjectsFromArray:[video[#"adaptive_fmts"] componentsSeparatedByString:#","]];
NSMutableDictionary *streamURLs = [NSMutableDictionary new];
for (NSString *streamQuery in streamQueries)
{
NSDictionary *stream = DictionaryWithQueryString(streamQuery, queryEncoding);
NSString *type = stream[#"type"];
NSString *urlString = stream[#"url"];
if (urlString && [AVURLAsset isPlayableExtendedMIMEType:type])
{
NSURL *streamURL = [NSURL URLWithString:urlString];
NSString *signature = stream[#"sig"];
if (signature)
streamURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#&signature=%#", urlString, signature]];
if ([[DictionaryWithQueryString(streamURL.query, queryEncoding) allKeys] containsObject:#"signature"])
streamURLs[#([stream[#"itag"] integerValue])] = streamURL;
}
}
for (NSNumber *videoQuality in self.preferredVideoQualities)
{
NSURL *streamURL = streamURLs[videoQuality];
if (streamURL)
{
NSString *title = video[#"title"];
NSString *thumbnailSmall = video[#"thumbnail_url"];
NSString *thumbnailMedium = video[#"iurlsd"];
NSString *thumbnailLarge = video[#"iurlmaxres"];
NSMutableDictionary *userInfo = [NSMutableDictionary new];
if (title)
userInfo[XCDMetadataKeyTitle] = title;
if (thumbnailSmall)
userInfo[XCDMetadataKeySmallThumbnailURL] = [NSURL URLWithString:thumbnailSmall];
if (thumbnailMedium)
userInfo[XCDMetadataKeyMediumThumbnailURL] = [NSURL URLWithString:thumbnailMedium];
if (thumbnailLarge)
userInfo[XCDMetadataKeyLargeThumbnailURL] = [NSURL URLWithString:thumbnailLarge];
[[NSNotificationCenter defaultCenter] postNotificationName:XCDYouTubeVideoPlayerViewControllerDidReceiveMetadataNotification object:self userInfo:userInfo];
return streamURL;
}
}
if (error)
{
NSMutableDictionary *userInfo = [#{ NSURLErrorKey: self.connection.originalRequest.URL } mutableCopy];
NSString *reason = video[#"reason"];
if (reason)
{
reason = [reason stringByReplacingOccurrencesOfString:#"<br\\s*/?>" withString:#" " options:NSRegularExpressionSearch range:NSMakeRange(0, reason.length)];
NSRange range;
while ((range = [reason rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
reason = [reason stringByReplacingCharactersInRange:range withString:#""];
userInfo[NSLocalizedDescriptionKey] = reason;
}
NSInteger code = [video[#"errorcode"] integerValue];
*error = [NSError errorWithDomain:XCDYouTubeVideoErrorDomain code:code userInfo:userInfo];
}
return nil;
}
You should use the latest version (2.0.2 as of writing) of XCDYouTubeKit, the successor of XCDYouTubeVideoPlayerViewController. The video 5Uls9v1nnss should play fine.

How to determine the file extension of a file from a URL (Objective-C)

I have a URL of type: http://example.com/files/
And I need to determine what file is there (extension and name of file). Depending on extensions, I'll do something.
How can I determine it?
Try
NSString *ext = [[url lastPathComponent] pathExtension];
See https://developer.apple.com/documentation/foundation/nsstring/1407801-pathextension
You can use this code directly just passing your NSUrl and get File Extension in return.
- (NSString *)findExtensionOfFileInUrl:(NSURL *)url{
NSString *urlString = [url absoluteString];
NSArray *componentsArray = [urlString componentsSeparatedByString:#"."];
NSString *fileExtension = [componentsArray lastObject];
return fileExtension;
}
Try This,
NSURL *urll = [NSURL URLWithString:#"http://example.com/files/firstFile.com"];
NSString *filenameWithExtension = [urll lastPathComponent];
NSRange *range = [filenameWithExtension rangeOfString:#"."];
USing the substringWithRange just substring of those ....
NSString *fileName = [string substringWithRange:NSMakeRange(0, range.location)];
NSString *extension = [string substringWithRange:NSMakeRange(10, string.length - range.location-1)];
NSLog(#"%# %#", fileName, extension);

#"23.0984" USD to #"23.09 USD"

I am sending URLRequest as mentioned below.
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *balanceString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
And I am getting #"23.0984 USD" in balanceString
Then I would like to display it on uilabel.tex= 23.09 USD Please help me
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *balanceString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
float amount = [balanceString floatValue]; // This will return 23.0984 as a float
NSString *formattedAmount = [NSString stringWithFormat:#"%01.2f USD", amount];
Then something like this should work:
NSString * inp = #"29.2994 USD";
// Initialize a scanner to process the input
NSScanner * scanner = [NSScanner scannerWithString:inp];
// Pull out a floating point value
float v;
[scanner scanFloat:&v];
NSCharacterSet * ws = [NSCharacterSet whitespaceAndNewlineCharacterSet];
// scan through the whitespace after the float
[scanner scanCharactersFromSet:ws intoString:nil];
NSString * currency;
[scanner scanUpToCharactersFromSet:ws intoString:&currency];
// write out value:
uilabel.text= [NSString stringWithFormat:#"%.2f %#",v,currency];
There might be easier ways.. But this should work..
NSString *originalString = #"23.0984 USD";
NSString *numberString = [originalString stringByReplacingOccurrencesOfString:#" USD"
withString:#""];
float number = [numberString floatValue];
NSString *changedString = [NSString stringWithFormat:#"%.2f",number];
NSLog(#"ChangedString : %# USD",changedString);
EDIT: This answer is already accepted. But I think a better method to get number from string is to search for spaces rather than replacing hardcoded string #" USD".
NSString *originalString = #"23.0984 USD";
NSString *numberString = nil;
NSArray *parts = [originalString componentsSeparatedByString:#" "]; //space
if(parts && [parts lenght]){
numberString = [parts objectAtIndex:0];
}
float number = [numberString floatValue];
NSString *changedString = [NSString stringWithFormat:#"%.2f",number];
NSLog(#"ChangedString : %# USD",changedString);

Resources