1 NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString]; 2 NSString *identifierForAdvertising = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
1 //第一次調用這個方法的時候,系統會提示用戶讓他贊成你的app獲取麥克風的數據 2 // 其餘時候調用方法的時候,則不會提醒用戶 3 // 而會傳遞以前的值來要求用戶贊成 4 [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) { 5 if (granted) { 6 // 用戶贊成獲取數據 7 } else { 8 // 能夠顯示一個提示框告訴用戶這個app沒有獲得容許? 9 } 10 }];
1 NSArray *arr = @[]; 2 id item = [arr firstObject]; 3 // 以前你須要作如下工做 4 id item = [arr count] > 0 ? arr[0] : nil;
1 UIImageRenderingModeAutomatic // 根據圖片的使用環境和所處的繪圖上下文自動調整渲染模式。 2 UIImageRenderingModeAlwaysOriginal // 始終繪製圖片原始狀態,不使用Tint Color。 3 UIImageRenderingModeAlwaysTemplate // 始終根據Tint Color繪製圖片,忽略圖片的顏色信息。
1 UIImage *img = [UIImage imageNamed:@"myimage"]; 2 img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
1 UINavigationBar *bar = self.navigationController.navigationBar; 2 UIColor *color = [UIColor greenColor]; 3 if ([bar respondsToSelector:@selector(setBarTintColor:)]) { // iOS 7+ 4 bar.barTintColor = color; 5 } else { // what year is this? 2012? 6 bar.tintColor = color; 7 }
1 + (UIColor *)viewFlipsideBackgroundColor; 2 + (UIColor *)scrollViewTexturedBackgroundColor; 3 + (UIColor *)underPageBackgroundColor;
1 @property (nonatomic, readonly) BOOL wirelessRoutesAvailable; // 是否有設備能夠鏈接的無線線路? 2 @property (nonatomic, readonly) BOOL wirelessRouteActive; // 設備如今是否鏈接上了網絡 3 NSString *const MPVolumeViewWirelessRoutesAvailableDidChangeNotification; 4 NSString *const MPVolumeViewWirelessRouteActiveDidChangeNotification;
1 @import CoreTelephony.CTTelephonyNetworkInfo; // new modules syntax! 2 @interface AppDelegate () 3 // we need to keep a reference to the CTTelephonyNetworkInfo object, otherwise the notifications won't be fired! 4 @property (nonatomic, strong) CTTelephonyNetworkInfo *networkInfo; 5 @end 6 7 @implementation ViewController 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 // whatever stuff your method does... 11 12 self.networkInfo = [[CTTelephonyNetworkInfo alloc] init]; 13 NSLog(@"Initial cell connection: %@", self.networkInfo.currentRadioAccessTechnology); 14 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioAccessChanged) name: 15 CTRadioAccessTechnologyDidChangeNotification object:nil]; 16 17 // whatever stuff your method does... 18 } 19 20 - (void)radioAccessChanged { 21 NSLog(@"Now you're connected via %@", self.networkInfo.currentRadioAccessTechnology); 22 } 23 24 @end
1 #import <SSKeychain.h> 2 3 - (BOOL)saveCredentials:(NSError **)error { 4 SSKeychainQuery *query = [[SSKeychainQuery alloc] init]; 5 query.password = @"MySecretPassword"; 6 query.service = @"MyAwesomeService"; 7 query.account = @"John Doe"; 8 query.synchronizable = YES; 9 return [query save:&error]; 10 } 11 12 - (NSString *)savedPassword:(NSError **)error { 13 SSKeychainQuery *query = [[SSKeychainQuery alloc] init]; 14 query.service = @"MyAwesomeService"; 15 query.account = @"John Doe"; 16 query.synchronizable = YES; 17 query.password = nil; 18 if ([query fetch:&error]) { 19 return query.password; 20 } 21 return nil; 22 }
1 NSString *html = @"<bold>Wow!</bold> Now <em>iOS</em> can create <h3>NSAttributedString</h3> from HTMLs!"; 2 NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; 3 4 NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] 5 options:options documentAttributes:nil error:nil]; 6
1 #import <SSKeychain.h> 2 3 - (BOOL)saveCredentials:(NSError **)error { 4 SSKeychainQuery *query = [[SSKeychainQuery alloc] init]; 5 query.password = @"MySecretPassword"; 6 query.service = @"MyAwesomeService"; 7 query.account = @"John Doe"; 8 query.synchronizable = YES; 9 return [query save:&error]; 10 } 11 12 - (NSString *)savedPassword:(NSError **)error { 13 SSKeychainQuery *query = [[SSKeychainQuery alloc] init]; 14 query.service = @"MyAwesomeService"; 15 query.account = @"John Doe"; 16 query.synchronizable = YES; 17 query.password = nil; 18 if ([query fetch:&error]) { 19 return query.password; 20 } 21 return nil;
1 NSAttributedString *attrString; // from previous code 2 NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; 3 4 NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:options error:nil]; 5 NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
1 // From NSData.h 2 3 /* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized 4 as valid Base-64. 5 */ 6 - (id)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options; 7 8 /* Create a Base-64 encoded NSString from the receiver's contents using the given options. 9 */ 10 - (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options; 11 12 /* Create an NSData from a Base-64, UTF-8 encoded NSData. By default, returns nil when the input is not recognized as valid Base-64. 13 */ 14 - (id)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options; 15 16 /* Create a Base-64, UTF-8 encoded NSData from the receiver's contents using the given options. 17 */ 18 - (NSData *)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options;
1 NSData* sampleData = [@"Some sample data" dataUsingEncoding:NSUTF8StringEncoding]; 2 3 NSString * base64String = [sampleData base64EncodedStringWithOptions:0]; 4 NSLog(@"Base64-encoded string is %@", base64String); // prints "U29tZSBzYW1wbGUgZGF0YQ==" 5 6 NSData* dataFromString = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; 7 NSLog(@"String is %@",[NSString stringWithUTF8String:[dataFromString bytes]]); // prints "String is Some sample data"
1 /* These methods first appeared in NSData.h on OS X 10.9 and iOS 7.0. They are deprecated in the same releases in favor of the methods in the <code>NSDataBase64Encoding</code> category. However, these methods have existed for several releases, so 2 they may be used for applications targeting releases prior to OS X 10.9 and iOS 7.0. 3 */ 4 - (id)initWithBase64Encoding:(NSString *)base64String; 5 - (NSString *)base64Encoding;
1 AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; 2 AVSpeechUtterance *utterance = 3 [AVSpeechUtterance speechUtteranceWithString:@"Wow, I have such a nice voice!"]; 4 utterance.rate = AVSpeechUtteranceMaximumSpeechRate / 4.0f; 5 utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; // defaults to your system language 6 [synthesizer speakUtterance:utterance];
1 UIScreenEdgePanGestureRecognizer *recognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action: 2 @selector(handleScreenEdgeRecognizer:)]; 3 recognizer.edges = UIRectEdgeLeft; // accept gestures that start from the left; we're probably building another hamburger menu! 4 [self.view addGestureRecognizer:recognizer];
1 UIScrollViewKeyboardDismissModeNone // the keyboard is not dismissed automatically when scrolling 2 UIScrollViewKeyboardDismissModeOnDrag // dismisses the keyboard when a drag begins 3 UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be 4 pulled upward again to cancel the dismiss
1 UIImage *image = [UIImage imageNamed:@"myImage"]; 2 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace 3 context:nil 4 options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}]; 5 6 NSDictionary *options = @{ CIDetectorSmile: @YES, CIDetectorEyeBlink: @YES }; 7 8 NSArray *features = [detector featuresInImage:image.CIImage options:options]; 9 10 for (CIFaceFeature *feature in features) { 11 NSLog(@"Bounds: %@", NSStringFromCGRect(feature.bounds)); 12 13 if (feature.hasSmile) { 14 NSLog(@"Nice smile!"); 15 } else { 16 NSLog(@"Why so serious?"); 17 } 18 if (feature.leftEyeClosed || feature.rightEyeClosed) { 19 NSLog(@"Open your eyes!"); 20 } 21 }
1 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"]; 2 [attributedString addAttribute:NSLinkAttributeName 3 value:@"username://marcelofabri_" 4 range:[[attributedString string] rangeOfString:@"@marcelofabri_"]]; 5 6 7 NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], 8 NSUnderlineColorAttributeName: [UIColor lightGrayColor], 9 NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)}; 10 11 // assume that textView is a UITextView previously created (either by code or Interface Builder) 12 textView.linkTextAttributes = linkAttributes; // customizes the appearance of links 13 textView.attributedText = attributedString; 14 textView.delegate = self;
1 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 2 if ([[URL scheme] isEqualToString:@"username"]) { 3 NSString *username = [URL host]; 4 // do something with this username 5 // ... 6 return NO; 7 } 8 return YES; // let the system open this URL 9 }