iOS Network connectivity optimization

Source: Internet
Author: User

How to make the user's networking experience better on the app side?

Initially in the writing network interface, consider the problem is less, up to a delay, to the corresponding point in time (such as 5 seconds, 10 seconds), if not to the information, direct popup prompt: Server no response!!!

No response at one time, two times no response N Times no response. If the boss also has this experience, hehe, quickly to optimize. Of course, such a basic user experience problem can not wait until the boss angry to deal with, he first seriously think how to solve.

optimize the first trick: Detect network status before networking

Apple provides reachability to detect network conditions, and the test device can be networked before each call to the interface:

reachability *r = [reachability reachabilitywithhostname:@ "www.baidu.com"];    if (R.currentreachabilitystatus = = notreachable) {        NSLog (@ "Cannot connect server.");        return;    }

Reachability has no effect on performance because it detects whether the network is connected or not, and does not verify that it is truly connected.


Optimize the second trick: detect real network connectivity after network delay

If you want to make sure you can connect to the Internet, you can use the third-party library "realreachability" to search on GitHub. It does this by pinging, like a server initiating a request, to confirm that the network is connected. Using a reachability-like, it returns network information through a block because the request network is time-consuming:

[Globalrealreachability reachabilitywithblock:^ (reachabilitystatus status) {switch (status) {case realstatusnotreachable: {[[] [[Uialertview Alloc ] initwithtitle:@ "realreachability" message:@ "nothing to do!                Offlinemode "delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil] show];            Break } case Realstatusviawifi: {[[[Uialertview alloc] initwithtitle:@ "Rea Lreachability "message:@" do-what-want!                free! "delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil] show];            Break } case Realstatusviawwan: {[[[Uialertview alloc] initwithtitle:@ "Rea Lreachability "message:@" take care of your money!                               You is in charge! "delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil] show];            Break                            }Default:break; }    }];
In the project's interface, 5 seconds after the request is issued, the method is called to detect the network condition, and the request is re-initiated if there is a network.

In the actual testing process, it is found that the results of detection in the network are often notreachabel, which seriously affects the actual use. Later carefully read the source code, found that the delay of the detection network only 2 seconds (PINGHELPER.M), in the user network is not fluent, 2 seconds is too short, and then changed to 4 seconds, the effect of detection has improved.

[Strongself performselector: @selector (pingtimeout) Withobject:nil afterdelay:4.0f]; [Self performselector: @selector (pingtimeout) Withobject:nil afterdelay:4.0f];
However, there are problems with this library. In the initial test, the direct hint Notreachabel, and not after 4 seconds of waiting, need to go to GitHub to raise this issue.


iOS Network connectivity optimization

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.