现在仍可以使用这个MAC还有一个生成iOS设备唯一标示符的方法是使用iOS设备的Media AccessControl(MAC)地址。一个MAC地址是一个唯一的号码,它是物理网络层级方面分配给网络适配器的。这个地址苹果还有其他的名字,比如说是硬件地址(HardwareAddress)或是Wifi地址,都是指同样的东西。有很多工程和框架都使用这个方法来生成唯一的设备ID。比如说ODIN。然而,苹果并不希望有人通过MAC地址来分辨用户,所以如果你在iOS7系统上查询MAC地址,它现在只会返回02:00:00:00:00:00。现在苹果明确的表明你应该使用-[UIDeviceidentifierForVendor]或是-[ASIdentifierManageradvertisingIdentifier]来作为你框架和应用的唯一标示符。坦白的来说,应对这些变化也不是那么的难,见以下代码片段:
- NSString*identifierForVendor=[[UIDevicecurrentDevice].identifierForVendorUUIDString];
- NSString*identifierForAdvertising=[[ASIdentifierManagersharedManager].advertisingIdentifierUUIDString];
- //第一次调用这个方法的时候,系统会提示用户让他同意你的app获取麦克风的数据
- //其他时候调用方法的时候,则不会提醒用户
- //而会传递之前的值来要求用户同意
- [[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOLgranted){
- if(granted){
- //用户同意获取数据
- }else{
- //可以显示一个提示框告诉用户这个app没有得到允许?
- }
- }];
好消息:性能提高以及被遗弃的功能以上就是一些重要的更新,他们可能会让你现在的app崩溃。然后,还有一些变化可能会对你的app造成影响,但是你不会在第一时间发现这些变化。5.-[NSArrayfirstObject]的实现-[NSArray firstObject]可能是Objective-C中被调用做多的API。在OpenRadar上一个简单的调查显示有一些需求苹果已经做了记录。好消息是现在这些需求已经得到了解决。.firstObject的使用可以追溯到iOS4.0,但是那时仅仅是一个私有方法。在iOS7以前,工程师用下面的方式来使用它:
- NSArray*arr=@[];
- iditem=[arrfirstObject];
- //之前你需要做以下工作
- iditem=[arrcount]>0?arr[0]:nil;
- UIImageRenderingModeAutomatic//根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
- UIImageRenderingModeAlwaysOriginal//始终绘制图片原始状态,不使用TintColor。
- UIImageRenderingModeAlwaysTemplate//始终根据TintColor绘制图片,忽略图片的颜色信息。
以下的代码说明了使用一个既定的rendering模式创建图片是多么简单:
- UIImage*img=[UIImageimageNamed:@"myimage"];
- img=[imgimageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
- UINavigationBar*bar=self.navigationController.navigationBar;
- UIColor*color=[UIColorgreenColor];
- if([barrespondsToSelector:@selector(setBarTintColor:)]){//iOS7+
- bar.barTintColor=color;
- }else{//whatyearisthis?2012?
- bar.tintColor=color;
- }
纹理颜色?对,不再使用他们了。不能再创建可以展现纹理的颜色。根据UIInterface.h文件中的注释,-[UIColorgroupTableViewBackgroundColor]应该是要在iOS6当中即被删除了,但是它仅仅只是不像之前那样返回纹理颜色了。然而,以下的颜色在iOS7当中被删除了:
- +(UIColor*)viewFlipsideBackgroundColor;
- +(UIColor*)scrollViewTexturedBackgroundColor;
- +(UIColor*)underPageBackgroundColor;
11.检查无线路由是否可用定制一个视频播放器的能力在iOS版本每次的发布中一直有所进步。比如说,在iOS6之前,你不能在MPVolumeView中改变AirPlay的icon。在iOS7当中,你可以通过AirPlay,蓝牙或是其他的虚线机制了解是否有一个远程的设备可用。了解它的话,就可以让你的app在恰当的时候做恰当的事,比如说,在没有远程设备的时候就不显示AirPlay的icon。以下是新增加到MPVolumeView的新属性和推送
- @property(nonatomic,readonly)BOOLwirelessRoutesAvailable;//是否有设备可以连接的无线线路?
- @property(nonatomic,readonly)BOOLwirelessRouteActive;//设备现在是否连接上了网络
- NSString*constMPVolumeViewWirelessRoutesAvailableDidChangeNotification;
- NSString*constMPVolumeViewWirelessRouteActiveDidChangeNotification;
- @importCoreTelephony.CTTelephonyNetworkInfo;//newmodulessyntax!
- @interfaceAppDelegate()
- //weneedtokeepareferencetotheCTTelephonyNetworkInfoobject,otherwisethenotificationswon'tbefired!
- @property(nonatomic,strong)CTTelephonyNetworkInfo*networkInfo;
- @end
- @implementationViewController
- -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
- //whateverstuffyourmethoddoes...
- self.networkInfo=[[CTTelephonyNetworkInfoalloc]init];
- NSLog(@"Initialcellconnection:%@",self.networkInfo.currentRadioAccessTechnology);
- [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(radioAccessChanged)name:
- CTRadioAccessTechnologyDidChangeNotificationobject:nil];
- //whateverstuffyourmethoddoes...
- }
- -(void)radioAccessChanged{
- NSLog(@"Nowyou'reconnectedvia%@",self.networkInfo.currentRadioAccessTechnology);
- }
- @end
- #import
- -(BOOL)saveCredentials:(NSError**)error{
- SSKeychainQuery*query=[[SSKeychainQueryalloc]init];
- query.password=@"MySecretPassword";
- query.service=@"MyAwesomeService";
- query.account=@"JohnDoe";
- query.synchronizable=YES;
- return[querysave:&error];
- }
- -(NSString*)savedPassword:(NSError**)error{
- SSKeychainQuery*query=[[SSKeychainQueryalloc]init];
- query.service=@"MyAwesomeService";
- query.account=@"JohnDoe";
- query.synchronizable=YES;
- query.password=nil;
- if([queryfetch:&error]){
- returnquery.password;
- }
- returnnil;
- }
- NSString*html=@"Wow!NowiOScancreate
NSAttributedString
fromHTMLs!"; - NSDictionary*options=@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
- NSAttributedString*attrString=[[NSAttributedStringalloc]initWithData:[htmldataUsingEncoding:NSUTF8StringEncoding]
- options:optionsdocumentAttributes:nilerror:nil];
- #import
- -(BOOL)saveCredentials:(NSError**)error{
- SSKeychainQuery*query=[[SSKeychainQueryalloc]init];
- query.password=@"MySecretPassword";
- query.service=@"MyAwesomeService";
- query.account=@"JohnDoe";
- query.synchronizable=YES;
- return[querysave:&error];
- }
- -(NSString*)savedPassword:(NSError**)error{
- SSKeychainQuery*query=[[SSKeychainQueryalloc]init];
- query.service=@"MyAwesomeService";
- query.account=@"JohnDoe";
- query.synchronizable=YES;
- query.password=nil;
- if([queryfetch:&error]){
- returnquery.password;
- }
- returnnil;
- NSAttributedString*attrString;//frompreviouscode
- NSDictionary*options=@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
- NSData*htmlData=[attrStringdataFromRange:NSMakeRange(0,[attrStringlength])documentAttributes:optionserror:nil];
- NSString*htmlString=[[NSStringalloc]initWithData:htmlDataencoding:NSUTF8StringEncoding];
- //FromNSData.h
- -(id)initWithBase64EncodedString:(NSString*)base64Stringoptions:(NSDataBase64DecodingOptions)options;
- -(NSString*)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options;
- -(id)initWithBase64EncodedData:(NSData*)base64Dataoptions:(NSDataBase64DecodingOptions)options;
- -(NSData*)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options;
- NSData*sampleData=[@"Somesampledata"dataUsingEncoding:NSUTF8StringEncoding];
- NSString*base64String=[sampleDatabase64EncodedStringWithOptions:0];
- NSLog(@"Base64-encodedstringis%@",base64String);//prints"U29tZSBzYW1wbGUgZGF0YQ=="
- NSData*dataFromString=[[NSDataalloc]initWithBase64EncodedString:base64Stringoptions:0];
- NSLog(@"Stringis%@",[NSStringstringWithUTF8String:[dataFromStringbytes]]);//prints"StringisSomesampledata"
- -(id)initWithBase64Encoding:(NSString*)base64String;
- -(NSString*)base64Encoding;
- AVSpeechSynthesizer*synthesizer=[[AVSpeechSynthesizeralloc]init];
- AVSpeechUtterance*utterance=
- [AVSpeechUtterancespeechUtteranceWithString:@"Wow,Ihavesuchanicevoice!"];
- utterance.rate=AVSpeechUtteranceMaximumSpeechRate/4.0f;
- utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-US"];//defaultstoyoursystemlanguage
- [synthesizerspeakUtterance:utterance];
- UIScreenEdgePanGestureRecognizer*recognizer=[[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:
- @selector(handleScreenEdgeRecognizer:)];
- recognizer.edges=UIRectEdgeLeft;//acceptgesturesthatstartfromtheleft;we'reprobablybuildinganotherhamburgermenu!
- [self.viewaddGestureRecognizer:recognizer];
- UIScrollViewKeyboardDismissModeNone//thekeyboardisnotdismissedautomaticallywhenscrolling
- UIScrollViewKeyboardDismissModeOnDrag//dismissesthekeyboardwhenadragbegins
- UIScrollViewKeyboardDismissModeInteractive//thekeyboardfollowsthedraggingtouchoffscreen,andmaybe
- pulledupwardagaintocancelthedismiss
20.使用CoreImage来检测眨眼以及微笑iOS给CoreImage增加了两种人脸检测功能:CIDetectorEyeBlink以及CIDetectorSmile。这也就是说你现在可以在照片中检测微笑以及眨眼。以下是在app中使用它的方法:
- UIImage*image=[UIImageimageNamed:@"myImage"];
- CIDetector*detector=[CIDetectordetectorOfType:CIDetectorTypeFace
- context:nil
- options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
- NSDictionary*options=@{CIDetectorSmile:@YES,CIDetectorEyeBlink:@YES};
- NSArray*features=[detectorfeaturesInImage:image.CIImageoptions:options];
- for(CIFaceFeature*featureinfeatures){
- NSLog(@"Bounds:%@",NSStringFromCGRect(feature.bounds));
- if(feature.hasSmile){
- NSLog(@"Nicesmile!");
- }else{
- NSLog(@"Whysoserious?");
- }
- if(feature.leftEyeClosed||feature.rightEyeClosed){
- NSLog(@"Openyoureyes!");
- }
- }
- NSMutableAttributedString*attributedString=[[NSMutableAttributedStringalloc]initWithString:@"Thisisanexampleby@marcelofabri_"];
- [attributedStringaddAttribute:NSLinkAttributeName
- value:@"username://marcelofabri_"
- range:[[attributedStringstring]rangeOfString:@"@marcelofabri_"]];
- NSDictionary*linkAttributes=@{NSForegroundColorAttributeName:[UIColorgreenColor],
- NSUnderlineColorAttributeName:[UIColorlightGrayColor],
- NSUnderlineStyleAttributeName:@(NSUnderlinePatternSolid)};
- //assumethattextViewisaUITextViewpreviouslycreated(eitherbycodeorInterfaceBuilder)
- textView.linkTextAttributes=linkAttributes;//customizestheappearanceoflinks
- textView.attributedText=attributedString;
- textView.delegate=self;
- -(BOOL)textView:(UITextView*)textViewshouldInteractWithURL:(NSURL*)URLinRange:(NSRange)characterRange{
- if([[URLscheme]isEqualToString:@"username"]){
- NSString*username=[URLhost];
- //dosomethingwiththisusername
- //...
- returnNO;
- }
- returnYES;//letthesystemopenthisURL
- }