iOS App 开发问题汇总(五)

1.On iOS 7 and later, how do I take a snapshot of my view and save the result in a UIImage?

Solution:

1
2
3
4
5
6
7
8
9
- (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

Reference:View Snapshots on iOS 7

2.How to present a view controller on iOS7 without the status bar overlapping?

Solution:The easiest workaround I’ve found is to wrap the view controller you want to present inside a navigation controller, and then present that navigation controller.

1
2
3
4
MyViewController *vc = [MyViewController new];
UINavigationController *nvc = [[UINavigationController alloc] 
    initWithRootViewController:vc];
[self presentViewController:nvc animated:YES completion:nil];

3.代码创建 UITableView 时如何使用各种系统样式的 UITableViewCell?

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sCellID = @"CellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sCellID];
    }
    // Configure the cell...
    
    return cell;
}
继续阅读

用 Wireshark 抓 iPhone 网络请求的数据包

开发过程中经常可能遇到网络问题,这时候我们需要抓包来定位问题所在。那么如何用 Wireshark 抓取 iPhone 真机网络请求的数据包呢?

iOS does not support packet tracing directly. However, if you’re developing for iOS you can take a packet trace of your app in a number of different ways:

If the problem you’re trying to debug occurs on Wi-Fi, you can put your iOS device on a test Wi-Fi network. See Wi-Fi Capture for details.
If your app uses HTTP, you can configure your iOS device to use a debugging HTTP proxy (such as Charles HTTP Proxy).
In iOS 5 and later you can use the remote virtual interface facility.

继续阅读

Web 开发问题汇总(一)

1. What is href=“#” and why is it used?

Solution:

1
2
3
4
5
6
A hashtag - # within a hyperlink specifies an html element id to which the window should be scrolled.  
href="#some-id" would scroll to an element on the current page such as <div id="some-id">.

href="//site.com/#some-id" would go to site.com and scroll to the id on that page.  

href="#" doesn't specify an id name, but does have a corresponding location - the top of the page. Clicking an anchor with href="#" will move the scroll position to the top.

Reference:http://stackoverflow.com/questions/4855168/what-is-href-and-why-is-it-used

2. How to debug your web page, technology stack is LAMP.

Solution:View log, figure out what cause problems.

1
2
3
4
$ tail -f /var/log/apache2/error_log
[Fri Dec 11 21:34:57.003657 2015] [:error] [pid 17465] [client ::1:50505] PHP Parse error:  parse error, expecting `','' or `';'' in /Users/dongmeiliang/Sites/knowledgeispower/includes/header.html on line 7
[Fri Dec 11 21:34:57.003701 2015] [:error] [pid 17465] [client ::1:50505] PHP Stack trace:
[Fri Dec 11 21:34:57.003723 2015] [:error] [pid 17465] [client ::1:50505] PHP   1. {main}() /Users/dongmeiliang/Sites/knowledgeispower/index.php:0
继续阅读