AFN 上傳檔案的方法,AFN 監測網路狀態,Reachability 監測網路狀態

來源:互聯網
上載者:User

標籤:

AFN 上傳檔案的方法

{

    // 1. 建立管理者

    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    

    // 2. 發送請求

    [mgr POST:@"http://localhost/upload/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    

        // formData :設定上傳檔案所需要的參數,兩種上傳方法:

        // <1> 通過本地檔案的 url 上傳

        {

            NSString *fromFile = @"/Users/likaining/Desktop/meinv.jpg";

            

            NSURL *url = [NSURL URLWithString:@"file:///Users/likaining/Desktop/meinv.jpg"];

            // url :需要上傳檔案的檔案路徑

            // name :伺服器接收的檔案名稱.

            // fileName: 檔案在伺服器中儲存的名字

            // mimeType : 檔案類型

            [formData appendPartWithFileURL:url name:@"userfile" fileName:@"meinv" mimeType:@"image/jpg" error:NULL];

        }

        // <2> 通過檔案的 位元據 上傳

        {

            NSData *data = [NSData dataWithContentsOfFile:zipFile];

            

            [formData appendPartWithFileData:data name:@"userfile" fileName:@"meinv.zip" mimeType:@"gzip"];

        }

        

        } success:^(AFHTTPRequestOperation *operation, id responseObject) {

            // 上傳成功之後的回調

            NSLog(@"%@",responseObject);

        

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            //  上傳失敗之後的回調

            NSLog(@"失敗");

    }];

 

}

/*-------------------------------------- 監測網路狀態 -----------------------------------------*/

 1.AFN 監測網路狀態  2.瞭解 Reachability 監測網路狀態

{

    1. AFN 監測網路狀態

    {

        // 建立 網路狀態管理者

        AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];

        

        // 監測網路狀態的改變

        [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

            // 當網路狀態發生改變的時候調用這個block

            switch (status) {

                case AFNetworkReachabilityStatusReachableViaWiFi:

                    NSLog(@"WIFI網路");

                    break;

                    

                case AFNetworkReachabilityStatusReachableViaWWAN:

                    NSLog(@"蜂窩網路");

                    break;

                    

                case AFNetworkReachabilityStatusNotReachable:

                    NSLog(@"沒有網路");

                    break;

                    

                case AFNetworkReachabilityStatusUnknown:

                    NSLog(@"未知網路");

                    break;

                default:

                    break;

            }

        }];

        // 開始監控

        [mgr startMonitoring];

    }

    2.Reachability 監測網路狀態

    {

        // 註冊通知觀察者,網路狀態改變時,接收通知!

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InternetStatusChanged) name:kReachabilityChangedNotification object:nil];

 

        // 控制器銷毀時,移除通知觀察者.

        -(void)dealloc

        {

            [[NSNotificationCenter defaultCenter] removeObserver:self];

        }

        

        // 根據當前網路狀態,做出不同的響應.

        - (void)InternetStatusChanged

        {

            NSLog(@"網路狀態改變了");

            

            if ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == ReachableViaWiFi) {

                NSLog(@"Wifi 網路");

            }

            if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == ReachableViaWWAN) {

                NSLog(@"蜂窩移動網路");

            }

            if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable)

            {

                NSLog(@"沒有網路");

            }

        }

        // 建立 Reachability 對象,開始監測網路狀態的改變

        - (void)MonitorInternetStatus

        {

            Reachability *CZReachability = [Reachability reachabilityForInternetConnection];

            

            [CZReachability startNotifier];

            

            self.reachability = CZReachability;

        }

 

        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

        {

            [self MonitorInternetStatus];

        }

    }

}

AFN 上傳檔案的方法,AFN 監測網路狀態,Reachability 監測網路狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.