IOS development tips 1

Source: Internet
Author: User

I haven't written a blog for a long time. Now I want to summarize some problems and solutions I encountered during development. The following is recorded as a Q &

1. When the Plain Style of UITableView is used, when the number of cells occupies less than one screen, useless cell lines will appear. How can we remove them?

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 0.01f;}- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    return [UIView new];        // If you are not using ARC:    // return [[UIView new] autorelease];}

2. How to obtain the idfa and mac addresses of iOS

//for mac#include <sys/socket.h>#include <sys/sysctl.h>#include <net/if.h>#include <net/if_dl.h>//for idfa#import <AdSupport/AdSupport.h>- (NSString * )macString{    int mib[6];size_t len;char *buf;unsigned char *ptr;struct if_msghdr *ifm;struct sockaddr_dl *sdl;    mib[0] = CTL_NET;mib[1] = AF_ROUTE;mib[2] = 0;mib[3] = AF_LINK;mib[4] = NET_RT_IFLIST;    if ((mib[5] = if_nametoindex("en0")) == 0) {printf("Error: if_nametoindex error\n");return NULL;}    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {printf("Error: sysctl, take 1\n");return NULL;}    if ((buf = malloc(len)) == NULL) {printf("Could not allocate memory. error!\n");return NULL;}    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {printf("Error: sysctl, take 2");free(buf);return NULL;}    ifm = (struct if_msghdr *)buf;sdl = (struct sockaddr_dl *)(ifm + 1);ptr = (unsigned char *)LLADDR(sdl);NSString *macString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];free(buf);    return macString;}- (NSString *)idfaString {        NSBundle *adSupportBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AdSupport.framework"];    [adSupportBundle load];        if (adSupportBundle == nil) {        return @"";    }    else{                Class asIdentifierMClass = NSClassFromString(@"ASIdentifierManager");                if(asIdentifierMClass == nil){            return @"";        }        else{                        //for no arc            //ASIdentifierManager *asIM = [[[asIdentifierMClass alloc] init] autorelease];            //for arc            ASIdentifierManager *asIM = [[asIdentifierMClass alloc] init];                        if (asIM == nil) {                return @"";            }            else{                                if(asIM.advertisingTrackingEnabled){                    return [asIM.advertisingIdentifier UUIDString];                }                else{                    return [asIM.advertisingIdentifier UUIDString];                }            }        }    }}- (NSString *)idfvString{    if([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {        return [[UIDevice currentDevice].identifierForVendor UUIDString];    }        return @"";}
However, note that the mac address cannot be obtained after iOS7. Reference (transfer ):

In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02: 00: 00: 00: 00: 00. if you need to identify the device, use the identifierForVendor property of UIDevice instead. (Apps that need an identifier for their own advertising purposes shold consider using the advertisingIdentifier property of ASIdentifierManager instead .) translation: After iOS7 and later versions, If you request to get the ma from the ios device The system returns a fixed value "02: 00: 00: 00: 00: 00: 00: 00". To identify the uniqueness of the device, use the identifierForVendor attribute of the UIDevice. (To identify devices for advertising purposes, consider using the advertisingIdentifier attribute of ASIdentifierManager as an alternative)

3. Are you worried about UITextFiled being blocked in TableView? Try the following code. Scroll the UITextFiled in TableViewCell to the visible position with the click

// Textfile uitableview scroll UITableViewCell * cell; if (! IS_ OS _7_OR_LATER) {// Load resources for iOS 6.1 or earlier cell = (UITableViewCell *) textField. superview. superview;} else {// Load resources for iOS 7 or later cell = (UITableViewCell *) textField. superview. superview. superview; // TextField-> UITableVieCellContentView-> (in iOS 7 !) ScrollView-> Cell! }

4. Disable Cell reuse of UITableView

Sometimes our UITableview cells are limited to 10 and 8, and there is no need to reuse them. Reuse causes many problems. The idea is to give different labels to the limited 10 cells.

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {NSString * CellIdentifier = [NSString stringWithFormat: @ "Cell % d", [indexPath section], [indexPath row]; // use indexPath to uniquely determine the cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; // output reusable cell if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];}

5. on iOS 7, how does one detect that the system's built-in ViewController gesture returns an end?

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{        id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator;        [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {            NSLog(@"7: %i", [context isCancelled]);    }];}

This check requires self. navigationController. delegate = self; the current viewController needs UINavigationBarDelegate to implement this protocol.

Another key setting is to set self. navigationController. delegate = nil where appropriate in the current ViewController; otherwise, the system will crash. I set it like this,

In viewDidAppear, Set self. navigationController. delegate = self; In viewDidDisAppear, Set self. navigationController. delegate = nil;

Make sure that the settings are paired.

Reference: http://stackoverflow.com/questions/20639006/getting-interactivepopgesturerecognizer-dismiss-callback-event


6. How to set the HeaderView of the Section of UITableView in Plain Style not floating on UITableview

    CGFloat dummyViewHeight = 40;    UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];    self.tableView.tableHeaderView = dummyView;    self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);

The value of dummyViewHeight varies according to the height of the headerView. The height of your headerView.


7. solve the problem that the certificate cannot be installed in the 7.1 system.

Idea: the server certificate or self-signed certificate does not work. The file access to the DropBox network disk is https. To solve this problem, you can transfer the plist file to the DropBox Network Disk and access it.

Put the plist file of manifest in Dropbox and copy the share link, such as: https://www.dropbox.com/s/sdljgdlsj24343j.plist

As for the manifest ipa package, you can put the file on your server. In this way, the access speed and upload size are not limited. I have tried this method and it is very useful.

It is said that domestic https://portal.qiniu.com/signin qiniu storage also has such a function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.