Last active
August 10, 2025 13:18
-
-
Save benms/afe6491f2d0e16ece61a2383baf8d1b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # build "clang -framework Foundation ipinfo.m -o ipinfo" remove this line after pasting | |
| #import <Foundation/Foundation.h> | |
| static NSDictionary* fetchIPInfo(void) { | |
| NSURL *url = [NSURL URLWithString:@"https://ipinfo.io/json"]; | |
| NSData *data = [NSData dataWithContentsOfURL:url]; | |
| if (!data) { | |
| NSLog(@"Failed to fetch data"); | |
| return nil; | |
| } | |
| NSError *error = nil; | |
| id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | |
| if (error || ![jsonObject isKindOfClass:[NSDictionary class]]) { | |
| NSLog(@"JSON parsing error: %@", error); | |
| return nil; | |
| } | |
| return (NSDictionary *)jsonObject; | |
| } | |
| int main(int argc, const char * argv[]) { | |
| @autoreleasepool { | |
| NSDictionary *json = fetchIPInfo(); | |
| if (!json) { | |
| return 1; | |
| } | |
| // Parse args fields (skip argv[0]) | |
| NSMutableArray<NSString*> *fields = [NSMutableArray array]; | |
| for (int i = 1; i < argc; i++) { | |
| NSString *arg = [NSString stringWithUTF8String:argv[i]]; | |
| if ([arg hasPrefix:@"--"]) { | |
| NSString *field = [arg substringFromIndex:2]; | |
| if (field.length > 0) { | |
| [fields addObject:field]; | |
| } | |
| } | |
| } | |
| if (fields.count == 0) { | |
| // No args, print full JSON pretty | |
| NSError *encodingError = nil; | |
| NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&encodingError]; | |
| if (!jsonData) { | |
| NSLog(@"JSON encode error: %@", encodingError); | |
| return 1; | |
| } | |
| NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; | |
| printf("%s\n", [jsonString UTF8String]); | |
| } else if (fields.count == 1) { | |
| // Single field: print the value as string if exists | |
| id value = json[fields[0]]; | |
| if ([value isKindOfClass:[NSString class]]) { | |
| printf("%s\n", [((NSString *)value) UTF8String]); | |
| } else { | |
| NSLog(@"Field '%@' not found or not a string", fields[0]); | |
| return 1; | |
| } | |
| } else { | |
| // Multiple fields: build dictionary with those fields | |
| NSMutableDictionary *result = [NSMutableDictionary dictionary]; | |
| for (NSString *key in fields) { | |
| id val = json[key]; | |
| if (val) { | |
| result[key] = val; | |
| } | |
| } | |
| NSError *encodingError = nil; | |
| NSData *jsonData = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:&encodingError]; | |
| if (!jsonData) { | |
| NSLog(@"JSON encode error: %@", encodingError); | |
| return 1; | |
| } | |
| NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; | |
| printf("%s\n", [jsonString UTF8String]); | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment